forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
38 lines (31 loc) · 877 Bytes
/
tcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"../nsq"
"../util"
"log"
"net"
)
type TcpProtocol struct {
util.TcpHandler
protocols map[int32]nsq.Protocol
}
func (p *TcpProtocol) Handle(clientConn net.Conn) {
log.Printf("TCP: new client(%s)", clientConn.RemoteAddr())
protocolMagic, err := nsq.ReadMagic(clientConn)
if err != nil {
log.Printf("ERROR: failed to read protocol version - %s", err.Error())
return
}
log.Printf("CLIENT(%s): desired protocol %d", clientConn.RemoteAddr(), protocolMagic)
prot, ok := p.protocols[protocolMagic]
if !ok {
nsq.SendFramedResponse(clientConn, nsq.FrameTypeError, []byte("E_BAD_PROTOCOL"))
log.Printf("ERROR: client(%s) bad protocol version %d", clientConn.RemoteAddr(), protocolMagic)
return
}
err = prot.IOLoop(clientConn)
if err != nil {
log.Printf("ERROR: client(%s) - %s", clientConn.RemoteAddr(), err.Error())
return
}
}