<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                #### 6.2 Zinx-V0.6代碼實現 首先`iserver`的`AddRouter()`的接口要稍微改一下,增添MsgId參數 > zinx/ziface/iserver.go ```go package ziface //定義服務器接口 type IServer interface{ //啟動服務器方法 Start() //停止服務器方法 Stop() //開啟業務服務方法 Serve() //路由功能:給當前服務注冊一個路由業務方法,供客戶端鏈接處理使用 AddRouter(msgId uint32, router IRouter) } ``` 其次,`Server`類中 之前有一個`Router`成員 ,代表唯一的處理方法,現在應該替換成`MsgHandler`成員 > zinx/znet/server.go ```go type Server struct { //服務器的名稱 Name string //tcp4 or other IPVersion string //服務綁定的IP地址 IP string //服務綁定的端口 Port int //當前Server的消息管理模塊,用來綁定MsgId和對應的處理方法 msgHandler ziface.IMsgHandle } ``` 初始化Server自然也要更正,增加msgHandler初始化 ```go /* 創建一個服務器句柄 */ func NewServer () ziface.IServer { utils.GlobalObject.Reload() s:= &Server { Name :utils.GlobalObject.Name, IPVersion:"tcp4", IP:utils.GlobalObject.Host, Port:utils.GlobalObject.TcpPort, msgHandler: NewMsgHandle(), //msgHandler 初始化 } return s } ``` 然后當Server在處理conn請求業務的時候,創建conn的時候也需要把msgHandler作為參數傳遞給Connection對象 ```go //... dealConn := NewConntion(conn, cid, s.msgHandler) //... ``` 那么接下來就是Connection對象了。固然在Connection對象中應該有MsgHandler的成員,來查找消息對應的回調路由方法 > zinx/znet/connection.go ```go type Connection struct { //當前連接的socket TCP套接字 Conn *net.TCPConn //當前連接的ID 也可以稱作為SessionID,ID全局唯一 ConnID uint32 //當前連接的關閉狀態 isClosed bool //消息管理MsgId和對應處理方法的消息管理模塊 MsgHandler ziface.IMsgHandle //告知該鏈接已經退出/停止的channel ExitBuffChan chan bool } //創建連接的方法 func NewConntion(conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection{ c := &Connection{ Conn: conn, ConnID: connID, isClosed: false, MsgHandler: msgHandler, ExitBuffChan: make(chan bool, 1), } return c } ``` 最后,在conn已經拆包之后,需要調用路由業務的時候,我們只需要讓conn調用MsgHandler中的`DoMsgHander()`方法就好了 > zinx/znet/connection.go ```go func (c *Connection) StartReader() { fmt.Println("[Reader Goroutine is running]") defer fmt.Println(c.RemoteAddr().String(), "[conn Reader exit!]") defer c.Stop() for { // 創建拆包解包的對象 dp := NewDataPack() //讀取客戶端的Msg head headData := make([]byte, dp.GetHeadLen()) if _, err := io.ReadFull(c.GetTCPConnection(), headData); err != nil { fmt.Println("read msg head error ", err) break } //拆包,得到msgid 和 datalen 放在msg中 msg , err := dp.Unpack(headData) if err != nil { fmt.Println("unpack error ", err) break } //根據 dataLen 讀取 data,放在msg.Data中 var data []byte if msg.GetDataLen() > 0 { data = make([]byte, msg.GetDataLen()) if _, err := io.ReadFull(c.GetTCPConnection(), data); err != nil { fmt.Println("read msg data error ", err) continue } } msg.SetData(data) //得到當前客戶端請求的Request數據 req := Request{ conn:c, msg:msg, } //從綁定好的消息和對應的處理方法中執行對應的Handle方法 go c.MsgHandler.DoMsgHandler(&req) } } ``` 好了,大功告成,我們來測試一下Zinx的多路由設置功能吧。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看