small fixups

Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
Василий Толстов 2017-06-26 17:31:00 +03:00
parent 930badb789
commit a56d5e188c
2 changed files with 12 additions and 0 deletions

View File

@ -39,7 +39,9 @@ func Connect(ctx context.Context, c net.Conn, cfg *ClientConfig) (*ClientConn, e
} }
for _, h := range cfg.Handlers { for _, h := range cfg.Handlers {
fmt.Printf("%#+v\n", h)
if err := h.Handle(conn); err != nil { if err := h.Handle(conn); err != nil {
fmt.Printf("rrr %v\n", err)
conn.Close() conn.Close()
cfg.ErrorCh <- err cfg.ErrorCh <- err
return nil, err return nil, err

View File

@ -294,14 +294,19 @@ func (*DefaultServerMessageHandler) Handle(c Conn) error {
} }
wg.Add(2) wg.Add(2)
quit := make(chan struct{})
// server // server
go func() { go func() {
defer wg.Done() defer wg.Done()
for { for {
select { select {
case <-quit:
return
case msg := <-cfg.ServerMessageCh: case msg := <-cfg.ServerMessageCh:
if err = msg.Write(c); err != nil { if err = msg.Write(c); err != nil {
cfg.errorCh <- err cfg.errorCh <- err
close(quit)
return return
} }
} }
@ -313,20 +318,25 @@ func (*DefaultServerMessageHandler) Handle(c Conn) error {
defer wg.Done() defer wg.Done()
for { for {
select { select {
case <-quit:
return
default: default:
var messageType ClientMessageType var messageType ClientMessageType
if err := binary.Read(c, binary.BigEndian, &messageType); err != nil { if err := binary.Read(c, binary.BigEndian, &messageType); err != nil {
cfg.errorCh <- err cfg.errorCh <- err
close(quit)
return return
} }
msg, ok := clientMessages[messageType] msg, ok := clientMessages[messageType]
if !ok { if !ok {
cfg.errorCh <- fmt.Errorf("unsupported message-type: %v", messageType) cfg.errorCh <- fmt.Errorf("unsupported message-type: %v", messageType)
close(quit)
return return
} }
parsedMsg, err := msg.Read(c) parsedMsg, err := msg.Read(c)
if err != nil { if err != nil {
cfg.errorCh <- err cfg.errorCh <- err
close(quit)
return return
} }
cfg.ClientMessageCh <- parsedMsg cfg.ClientMessageCh <- parsedMsg