<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                #### 9.2 鏈接管理模塊集成到Zinx中 ##### A\)ConnManager集成到Server中 現在需要將`ConnManager`添加到`Server`中 > zinx/znet/server.go ```go //iServer 接口實現,定義一個Server服務類 type Server struct { //服務器的名稱 Name string //tcp4 or other IPVersion string //服務綁定的IP地址 IP string //服務綁定的端口 Port int //當前Server的消息管理模塊,用來綁定MsgId和對應的處理方法 msgHandler ziface.IMsgHandle //當前Server的鏈接管理器 ConnMgr ziface.IConnManager } /* 創建一個服務器句柄 */ 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(), ConnMgr:NewConnManager(), //創建ConnManager } return s } ``` 那么,既然server具備了ConnManager成員,在獲取的時候需要給抽象層提供一個獲取ConnManager方法 > zinx/ziface/iserver.go ```go type IServer interface{ //啟動服務器方法 Start() //停止服務器方法 Stop() //開啟業務服務方法 Serve() //路由功能:給當前服務注冊一個路由業務方法,供客戶端鏈接處理使用 AddRouter(msgId uint32, router IRouter) //得到鏈接管理 GetConnMgr() IConnManager } ``` > zinx/znet/server.go ```go //得到鏈接管理 func (s *Server) GetConnMgr() ziface.IConnManager { return s.ConnMgr } ``` 因為我們現在在server中有鏈接的管理,有的時候conn也需要得到這個ConnMgr的使用權,那么我們需要將`Server`和`Connection`建立能夠互相索引的關系,我們在`Connection`中,添加Server當前conn隸屬的server句柄。 > zinx/znet/connection.go ```go type Connection struct { //當前Conn屬于哪個Server TcpServer ziface.IServer //當前conn屬于哪個server,在conn初始化的時候添加即可 //當前連接的socket TCP套接字 Conn *net.TCPConn //當前連接的ID 也可以稱作為SessionID,ID全局唯一 ConnID uint32 //當前連接的關閉狀態 isClosed bool //消息管理MsgId和對應處理方法的消息管理模塊 MsgHandler ziface.IMsgHandle //告知該鏈接已經退出/停止的channel ExitBuffChan chan bool //無緩沖管道,用于讀、寫兩個goroutine之間的消息通信 msgChan chan []byte //有關沖管道,用于讀、寫兩個goroutine之間的消息通信 msgBuffChan chan []byte } ``` ##### B\) 鏈接的添加 那么我們什么選擇將創建好的連接添加到`ConnManager`中呢,這里我們選擇在初始化一個新鏈接的時候,加進來就好了 > zinx/znet/connection.go ```go //創建連接的方法 func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection{ //初始化Conn屬性 c := &Connection{ TcpServer:server, //將隸屬的server傳遞進來 Conn: conn, ConnID: connID, isClosed: false, MsgHandler: msgHandler, ExitBuffChan: make(chan bool, 1), msgChan:make(chan []byte), msgBuffChan:make(chan []byte, utils.GlobalObject.MaxMsgChanLen), } //將新創建的Conn添加到鏈接管理中 c.TcpServer.GetConnMgr().Add(c) //將當前新創建的連接添加到ConnManager中 return c } ``` ##### C\) Server中添加鏈接數量的判斷 在server的`Start()`方法中,在Accept與客戶端鏈接建立成功后,可以直接對鏈接的個數做一個判斷 > zinx/znet/server.go ```go //開啟網絡服務 func (s *Server) Start() { fmt.Printf("[START] Server name: %s,listenner at IP: %s, Port %d is starting\n", s.Name, s.IP, s.Port) fmt.Printf("[Zinx] Version: %s, MaxConn: %d, MaxPacketSize: %d\n", utils.GlobalObject.Version, utils.GlobalObject.MaxConn, utils.GlobalObject.MaxPacketSize) //開啟一個go去做服務端Linster業務 go func() { // .... //3 啟動server網絡連接業務 for { //3.1 阻塞等待客戶端建立連接請求 conn, err := listenner.AcceptTCP() if err != nil { fmt.Println("Accept err ", err) continue } //============= //3.2 設置服務器最大連接控制,如果超過最大連接,那么則關閉此新的連接 if s.ConnMgr.Len() >= utils.GlobalObject.MaxConn { conn.Close() continue } //============= //3.3 處理該新連接請求的 業務 方法, 此時應該有 handler 和 conn是綁定的 dealConn := NewConntion(s, conn, cid, s.msgHandler) cid ++ //3.4 啟動當前鏈接的處理業務 go dealConn.Start() } }() } ``` 當然,我們應該在配置文件`zinx.json`或者在`GlobalObject`全局配置中,定義好我們期望的連接的最大數目限制`MaxConn`。 ##### D\) 連接的刪除 我們應該在連接停止的時候,將該連接從ConnManager中刪除,所以在`connection`的`Stop()`方法中添加。 > zinx/znet/connecion.go ```go func (c *Connection) Stop() { fmt.Println("Conn Stop()...ConnID = ", c.ConnID) //如果當前鏈接已經關閉 if c.isClosed == true { return } c.isClosed = true // 關閉socket鏈接 c.Conn.Close() //關閉Writer Goroutine c.ExitBuffChan <- true //將鏈接從連接管理器中刪除 c.TcpServer.GetConnMgr().Remove(c) //刪除conn從ConnManager中 //關閉該鏈接全部管道 close(c.ExitBuffChan) close(c.msgBuffChan) } ``` 當然,我們也應該在`server`停止的時候,將全部的連接清空 > zinx/znet/server.go ```go func (s *Server) Stop() { fmt.Println("[STOP] Zinx server , name " , s.Name) //將其他需要清理的連接信息或者其他信息 也要一并停止或者清理 s.ConnMgr.ClearConn() } ``` 現在我們已經將連接管理成功的集成到了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>

                              哎呀哎呀视频在线观看