zoukankan      html  css  js  c++  java
  • tcp.go

    package nsqlookupd

    import (
        "io"
        "net"

        "github.com/nsqio/nsq/internal/protocol"
    )

    type tcpServer struct {
        ctx *Context
    }

    func (p *tcpServer) Handle(clientConn net.Conn) {
        p.ctx.nsqlookupd.logf("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.nsqlookupd.logf("ERROR: failed to read protocol version - %s", err)
            return
        }
        protocolMagic := string(buf)

        p.ctx.nsqlookupd.logf("CLIENT(%s): desired protocol magic '%s'",
            clientConn.RemoteAddr(), protocolMagic)

        var prot protocol.Protocol
        switch protocolMagic {
        case "  V1":
            prot = &LookupProtocolV1{ctx: p.ctx}
        default:
            protocol.SendResponse(clientConn, []byte("E_BAD_PROTOCOL"))
            clientConn.Close()
            p.ctx.nsqlookupd.logf("ERROR: client(%s) bad protocol magic '%s'",
                clientConn.RemoteAddr(), protocolMagic)
            return
        }

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

  • 相关阅读:
    [hdu-2048] 神、上帝以及老天爷
    or1200中IMMU分析(续)
    Java Applet读写client串口——终极篇
    树莓派_Linux串口编程_实现自发自收
    2014百度实习生面试题(部分)具体解释
    Leetcode
    eclipse+webservice开发实例
    现有一些开源ESB总线的比較
    《HTML5 从入门到精通--7.6.3 单元格垂直跨度——rowspan》
    百度究竟是哪国的公司
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7457142.html
Copyright © 2011-2022 走看看