forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
47 lines (40 loc) · 1.24 KB
/
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
39
40
41
42
43
44
45
46
47
package main
import (
"github.com/bitly/go-nsq"
"github.com/bitly/nsq/util"
"io"
"log"
"net"
)
type tcpServer struct {
context *Context
}
func (p *tcpServer) Handle(clientConn net.Conn) {
log.Printf("TCP: new client(%s)", clientConn.RemoteAddr())
// The client should initialize itself by sending a 4 byte sequence indicating
// the version of the protocol that it intends to communicate, this will allow us
// to gracefully upgrade the protocol away from text/line oriented to whatever...
buf := make([]byte, 4)
_, err := io.ReadFull(clientConn, buf)
if err != nil {
log.Printf("ERROR: failed to read protocol version - %s", err.Error())
return
}
protocolMagic := string(buf)
log.Printf("CLIENT(%s): desired protocol magic '%s'", clientConn.RemoteAddr(), protocolMagic)
var prot util.Protocol
switch protocolMagic {
case " V2":
prot = &ProtocolV2{context: p.context}
default:
util.SendFramedResponse(clientConn, nsq.FrameTypeError, []byte("E_BAD_PROTOCOL"))
clientConn.Close()
log.Printf("ERROR: client(%s) bad protocol magic '%s'", clientConn.RemoteAddr(), protocolMagic)
return
}
err = prot.IOLoop(clientConn)
if err != nil {
log.Printf("ERROR: client(%s) - %s", clientConn.RemoteAddr(), err.Error())
return
}
}