Esempio n. 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
	}
}
Esempio n. 2
0
func (p *LookupProtocolV1) IOLoop(conn net.Conn) error {
	var err error
	//var line string

	err = nil
	client := NewClientV1(conn)
	reader := bufio.NewReader(conn)
	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			break
		}

		line = strings.TrimSpace(line)
		params := strings.Split(line, " ")

		resp, err := p.Exec(client, reader, params)
		if err != nil {
			p.ctx.s.logf("client(%s) exec error- %s", client, err)
			break
		}

		if resp != nil {
			_, err := proto.SendResponse(client, resp)
			if err != nil {
				p.ctx.s.logf("send response to client(%) error - %s", client, err)
				break
			}
		}
	}
	p.ctx.s.logf("closing client(%s)", client)
	//client may download client or server
	if client.peerInfo != nil {
		p.ctx.s.Hold.RemoveProducer(client.peerInfo)
	}
	client.Close()
	return err
}