package server import ( "bytes" "encoding/binary" "errors" "time" "github.com/panjf2000/gnet" "github.com/zmicro-team/zmicro/core/log" "github.com/zchat-team/zim/app/conn/protocol" ) type TcpServer struct { gnet.EventHandler addr string codec gnet.ICodec srv *Server } func NewTcpServer(srv *Server, addr string) *TcpServer { ts := new(TcpServer) ts.addr = addr ts.codec = &TcpCodec{} ts.srv = srv return ts } func (s *TcpServer) Start() error { return gnet.Serve(s, s.addr, gnet.WithMulticore(true), gnet.WithTCPKeepAlive(time.Minute*5), gnet.WithCodec(s.codec)) } func (s *TcpServer) Stop() error { //return gnet.Stop(context.Background(), s.addr) return nil } func (s *TcpServer) OnInitComplete(srv gnet.Server) (action gnet.Action) { log.Infof("tcp server is listening on %s (multi-cores: %t, loops: %d)", srv.Addr.String(), srv.Multicore, srv.NumEventLoop) return } func (s *TcpServer) OnOpened(c gnet.Conn) (out []byte, action gnet.Action) { log.Info("TCP OnOpened ...") conn := &Connection{ Status: AuthPending, Conn: c, } c.SetContext(conn) s.srv.OnOpen(conn) return } func (s *TcpServer) OnClosed(c gnet.Conn, err error) (action gnet.Action) { log.Info("TCP OnClose ...") conn, ok := c.Context().(*Connection) if !ok { return } s.srv.OnClose(conn) return } func (s *TcpServer) React(data []byte, c gnet.Conn) (out []byte, action gnet.Action) { conn, ok := c.Context().(*Connection) if !ok { return } s.srv.OnMessage(data, conn) return } // ==================================== Codec ============================================== type TcpCodec struct { } func (_ *TcpCodec) Encode(c gnet.Conn, buf []byte) ([]byte, error) { return buf, nil } func (_ *TcpCodec) Decode(c gnet.Conn) ([]byte, error) { if size, header := c.ReadN(protocol.HeaderLen); size == protocol.HeaderLen { byteBuffer := bytes.NewBuffer(header) var p protocol.Packet if err := binary.Read(byteBuffer, binary.BigEndian, &p.HeaderLen); err != nil { return nil, err } if err := binary.Read(byteBuffer, binary.BigEndian, &p.Version); err != nil { return nil, err } if err := binary.Read(byteBuffer, binary.BigEndian, &p.Cmd); err != nil { return nil, err } if err := binary.Read(byteBuffer, binary.BigEndian, &p.Seq); err != nil { return nil, err } if err := binary.Read(byteBuffer, binary.BigEndian, &p.BodyLen); err != nil { return nil, err } protocolLen := int(protocol.HeaderLen + p.BodyLen) if size, data := c.ReadN(protocolLen); size == protocolLen { c.ShiftN(protocolLen) return data, nil } return nil, errors.New("not enough payload data") } return nil, errors.New("not enough header data") }
时间: 2024-03-04 14:52:23 浏览: 164
这段代码是一个使用 gnet 框架实现的 TCP 服务器,代码中定义了一个 TcpServer 结构体,并实现了 gnet.EventHandler 接口的相关方法。在 TcpServer 中定义了一个 gnet.ICodec 接口的实现 TcpCodec,用于对消息进行编解码。代码中通过读取消息头中的信息来解析出消息体的长度,然后再读取消息体并返回。同时,TcpServer 中还定义了 Start() 和 Stop() 方法来启动和停止服务器。
相关问题
WARNING: Implying --no-binary=:all: due to the presence of --build-option / --global-option / --install-option. Consider using --config-settings for more flexibility. DEPRECATION: --install-option is deprecated because it forces pip to use the 'setup.py install' command which is itself deprecated. pip 23.1 will enforce this behaviour change. A possible replacement is to use --config-settings. Discussion can be found at https://github.com/pypa/pip/issues/11358 DEPRECATION: --no-binary currently disables reading from the cache of locally built wheels. In the future --no-binary will not influence the wheel cache. pip 23.1 will enforce this behaviour change. A possible replacement is to use the --no-cache-dir option. You can use the flag --use-feature=no-binary-enable-wheel-cache to test the upcoming behaviour. Discussion can be found at https://github.com/pypa/pip/issues/11453 Collecting git+https://github.com/vallis/libstempo.git Cloning https://github.com/vallis/libstempo.git to /tmp/pip-req-build-yxnze2mi Running command git clone --filter=blob:none --quiet https://github.com/vallis/libstempo.git /tmp/pip-req-build-yxnze2mi
您好!欢迎来到CSDN知道!您的问题是关于使用pip克隆GitHub上的库到本地的问题。对于这个问题,您可以尝试以下步骤:
1. 确认您已经安装了Git工具。如果没有安装,请先安装Git。
2. 打开命令行终端,并导航到您想要克隆库的目录。
3. 运行以下命令进行克隆:
```
git clone https://github.com/vallis/libstempo.git
```
这将克隆库到当前目录下的一个名为"libstempo"的文件夹中。
完成上述步骤后,您将在本地成功克隆GitHub上的库。希望对您有所帮助!如果您有任何其他问题,请随时提问。
git clone https://github.com/binary-husky/chatgpt_academic.git
As an AI language model, I do not have the capability to execute commands. However, if you want to clone the repository, you can run the above command in your terminal or command prompt if you have Git installed on your system. This command will clone the repository to your local machine.
阅读全文