Beispiel #1
0
Datei: tcp.go Projekt: ufosky/nsq
func (p *tcpServer) Handle(clientConn net.Conn) {
	p.ctx.l.Output(2, fmt.Sprintf("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 {
		p.ctx.l.Output(2, fmt.Sprintf("ERROR: failed to read protocol version - %s", err))
		return
	}
	protocolMagic := string(buf)

	p.ctx.l.Output(2, fmt.Sprintf(
		"CLIENT(%s): desired protocol magic '%s'", clientConn.RemoteAddr(), protocolMagic))

	var prot util.Protocol
	switch protocolMagic {
	case "  V2":
		prot = &protocolV2{ctx: p.ctx}
	default:
		util.SendFramedResponse(clientConn, frameTypeError, []byte("E_BAD_PROTOCOL"))
		clientConn.Close()
		p.ctx.l.Output(2, fmt.Sprintf(
			"ERROR: client(%s) bad protocol magic '%s'", clientConn.RemoteAddr(), protocolMagic))
		return
	}

	err = prot.IOLoop(clientConn)
	if err != nil {
		p.ctx.l.Output(2, fmt.Sprintf("ERROR: client(%s) - %s", clientConn.RemoteAddr(), err))
		return
	}
}
Beispiel #2
0
func (p *ProtocolV2) Send(client *ClientV2, frameType int32, data []byte) error {
	client.Lock()

	client.SetWriteDeadline(time.Now().Add(time.Second))
	_, err := util.SendFramedResponse(client.Writer, frameType, data)
	if err != nil {
		client.Unlock()
		return err
	}

	if frameType != nsq.FrameTypeMessage {
		err = client.Flush()
	}

	client.Unlock()

	return err
}
Beispiel #3
0
func (p *protocolV2) Send(client *clientV2, frameType int32, data []byte) error {
	client.Lock()

	var zeroTime time.Time
	if client.HeartbeatInterval > 0 {
		client.SetWriteDeadline(time.Now().Add(client.HeartbeatInterval))
	} else {
		client.SetWriteDeadline(zeroTime)
	}

	_, err := util.SendFramedResponse(client.Writer, frameType, data)
	if err != nil {
		client.Unlock()
		return err
	}

	if frameType != frameTypeMessage {
		err = client.Flush()
	}

	client.Unlock()

	return err
}