Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
raspi committed Jul 17, 2022
1 parent 402c7c3 commit 0f3063c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
24 changes: 21 additions & 3 deletions example/cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"errors"
"fmt"
"github.com/raspi/jumiks/pkg/client"
"io"
"net"
"os"
"syscall"
"time"
)

Expand Down Expand Up @@ -36,18 +40,32 @@ func (c *ExampleClient) on_msg(b []byte) {
c.delay += time.Millisecond * 50
}

func (c *ExampleClient) Close() error {
return c.c.Close()
}

func main() {
errors := make(chan error)
errorch := make(chan error)

c, err := New("@test", errors)
c, err := New("@test", errorch)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, `error: %v`, err)
os.Exit(1)
}

go c.Listen()

for err := range errors {
for err := range errorch {
fmt.Printf(`got error: %v`, err)

if errors.Is(err, io.EOF) {
break
} else if errors.Is(err, syscall.EPIPE) {
break
} else if errors.Is(err, net.ErrClosed) {
break
}
}

c.Close()
}
2 changes: 2 additions & 0 deletions example/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ func main() {
}
}

l.Close()

}
11 changes: 11 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/raspi/jumiks/pkg/server/header"
"io"
"net"
"syscall"
)

type Client struct {
Expand Down Expand Up @@ -54,6 +55,12 @@ func (c *Client) Listen() {
rb, err := c.conn.Read(buffer)
if err != nil {
if errors.Is(err, io.EOF) {
// EOF
break
} else if errors.Is(err, net.ErrClosed) {
break
} else if errors.Is(err, syscall.EPIPE) {
// Broken pipe
break
}

Expand Down Expand Up @@ -127,6 +134,10 @@ func (c *Client) handleMsg(b []byte) {
c.hfn(b)
}

func (c *Client) Close() error {
return c.conn.Close()
}

// handshake determines if both server.Server and Client are speaking the same protocol
func handshake(conn *net.UnixConn) (err error) {
var serverHs header.Handshake
Expand Down
5 changes: 5 additions & 0 deletions pkg/server/internal/serverclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (c *ServerClient) Listen() {
defer c.conn.Close()

for {
c.logger.Printf(`reading message header `)
var hdr header.MessageHeaderFromClient
err := binary.Read(c.conn, binary.LittleEndian, &hdr)
if err != nil {
Expand All @@ -60,3 +61,7 @@ func (c *ServerClient) GetId() uint64 {
func (c *ServerClient) Write(b []byte) (int, error) {
return c.conn.Write(b)
}

func (c *ServerClient) Close() error {
return c.conn.Close()
}
44 changes: 23 additions & 21 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,24 @@ func (s *Server) Listen() {
s.logger.Printf(`error: %v`, err)
case msg := <-s.messagesCh: // new message
s.logger.Printf(`received message from channel`)
s.lock.Lock()
pId := s.packetId
s.lock.Unlock()

for clientId, client := range s.clients {
s.logger.Printf(`client %v`, client.GetId())
if client == nil {
s.logger.Printf(`client is nil!`)
s.connectionClose <- clientId

client.Close()
delete(s.clients, clientId)
continue
}

// Send the buffer to client
s.logger.Printf(`writing to client`)
wb, err := client.Write(generateMsg(pId, msg))
wb, err := client.Write(msg)
if err != nil {
if errors.Is(err, syscall.EPIPE) {
s.connectionClose <- clientId
client.Close()
delete(s.clients, clientId)
continue
}

Expand All @@ -183,18 +184,6 @@ func (s *Server) Listen() {
s.clients[c.GetId()] = c
s.logger.Printf(`client added`)

case cId := <-s.connectionClose:
s.logger.Printf(`!!!! disconnecting client #%v`, cId)

/*
err := s.clients[cId].Close()
if err != nil {
s.errch <- error2.New(err)
}
*/

delete(s.clients, cId)
default:

}
Expand All @@ -203,11 +192,24 @@ func (s *Server) Listen() {

// SendToAll sends a message to every connected client
func (s *Server) SendToAll(msg []byte) {
if len(msg) > 0 {
s.messagesCh <- msg
}
s.messagesCh <- generateMsg(s.packetId, msg)

s.lock.Lock()
s.packetId++
s.lock.Unlock()
}

func (s *Server) Close() error {
var del []uint64

for i, c := range s.clients {
c.Close()
del = append(del, i)
}

for _, i := range del {
delete(s.clients, i)
}

return s.listener.Close()
}

0 comments on commit 0f3063c

Please sign in to comment.