Example #1
0
func (p *tcpServer) Handle(conn net.Conn) {
	buf := make([]byte, 4)

	_, err := io.ReadFull(conn, buf)
	if err != nil {
		p.ctx.s.logf("error: failed to read protocol - %s", err)
		return
	}

	protoMagic := string(buf)
	p.ctx.s.logf("client(%s) send protocol: %s", conn.RemoteAddr(), protoMagic)
	var protocol proto.Protocol
	switch protoMagic {
	case "  V1":
		protocol = &LookupProtocolV1{ctx: p.ctx}
	default:
		proto.SendResponse(conn, []byte("E_BAD_PROTOCOL"))
		p.ctx.s.logf("client(%s) wrong protocol: %s", conn.RemoteAddr(), protoMagic)
		conn.Close()
		return
	}

	err = protocol.IOLoop(conn)
	if err != nil {
		p.ctx.s.logf("client(%s) error - %s", conn.RemoteAddr(), err)
		return
	}
}
Example #2
0
func (t *tcpServer) Handle(clientConn net.Conn) {
	t.ctx.s.logf("new client(%s)", clientConn.RemoteAddr())

	buf := make([]byte, 4)
	_, err := io.ReadFull(clientConn, buf)
	if err != nil {
		t.ctx.s.logf("ERROR: failed to read client(%s) protocol version - %s",
			clientConn.RemoteAddr(), err)
		return
	}
	protocolMagic := string(buf)
	var prot proto.Protocol
	t.ctx.s.logf("version: %s", protocolMagic)
	switch protocolMagic {
	case "  V1": //when upgrade, V2, V3..., and can also support multi-version
		prot = &protocolV1{ctx: t.ctx}
	default:
		proto.SendFrameResponse(clientConn, 3, []byte("E_BAD_PROTOCOL"))
		clientConn.Close()
		t.ctx.s.logf("ERROR: client(%s) bad version")
	}

	err = prot.IOLoop(clientConn)
	if err != nil {
		t.ctx.s.logf("ERROR: client(%s) - %s", clientConn.RemoteAddr(), err)
		return
	}
}