66 "io"
77 "net"
88 "sync"
9+ "sync/atomic"
910
1011 "github.com/Cai-ki/cinx/ciface"
1112 "github.com/Cai-ki/cinx/cutils"
@@ -18,24 +19,23 @@ type Connection struct {
1819 Conn * net.TCPConn
1920 //当前连接的ID 也可以称作为SessionID,ID全局唯一
2021 ConnID uint32
21- //当前连接的关闭状态
22- isClosed bool
22+
23+ // 正常退出
24+ IsClosed atomic.Bool
25+
26+ // 异常退出
27+ IsAborted atomic.Bool
2328
2429 //消息管理MsgId和对应处理方法的消息管理模块
2530 MsgHandler ciface.IMsgHandle
2631
27- //告知该链接已经退出/停止的channel
28- ExitBuffChan chan bool
29- //无缓冲管道,用于读、写两个goroutine之间的消息通信
30- msgChan chan []byte
31- //有缓冲管道,用于读、写两个goroutine之间的消息通信
32+ ExitBuffChan chan struct {}
33+
34+ msgChan chan []byte
3235 msgBuffChan chan []byte
33- // ================================
34- //链接属性
35- property map [string ]interface {}
36- //保护链接属性修改的锁
36+
37+ property map [string ]interface {}
3738 propertyLock sync.RWMutex
38- // ================================
3939}
4040
4141// 创建连接的方法
@@ -44,15 +44,16 @@ func NewConntion(server ciface.IServer, conn *net.TCPConn, connID uint32, msgHan
4444 TcpServer : server , //将隶属的server传递进来
4545 Conn : conn ,
4646 ConnID : connID ,
47- isClosed : false ,
4847 MsgHandler : msgHandler ,
49- ExitBuffChan : make (chan bool , 1 ),
48+ ExitBuffChan : make (chan struct {} , 1 ),
5049 msgChan : make (chan []byte ), //msgChan初始化
5150 msgBuffChan : make (chan []byte , cutils .GlobalObject .MaxMsgChanLen ),
5251 property : make (map [string ]interface {}), //对链接属性map初始化
5352 }
54- //将新创建的Conn添加到链接管理中
55- c .TcpServer .GetConnMgr ().Add (c ) //将当前新创建的连接添加到ConnManager中
53+ c .IsClosed .Store (false )
54+ c .IsAborted .Store (false )
55+
56+ c .TcpServer .GetConnMgr ().Add (c )
5657 return c
5758}
5859
@@ -61,13 +62,17 @@ func NewClientConn(client ciface.IClient, conn *net.TCPConn) ciface.IConn {
6162 TcpServer : NewServer (), // TODO: 临时创建一个server,后续需要修改
6263 Conn : conn ,
6364 ConnID : 0 , // ignore
64- isClosed : false ,
6565 MsgHandler : client .GetMsgHandler (),
66- ExitBuffChan : make (chan bool , 1 ),
66+ ExitBuffChan : make (chan struct {} , 1 ),
6767 msgChan : make (chan []byte ), //msgChan初始化
6868 msgBuffChan : make (chan []byte , cutils .GlobalObject .MaxMsgChanLen ),
6969 property : make (map [string ]interface {}), //对链接属性map初始化
7070 }
71+ c .IsClosed .Store (false )
72+ c .IsAborted .Store (false )
73+
74+ c .TcpServer .SetOnConnStart (client .GetOnConnStart ())
75+ c .TcpServer .SetOnConnStop (client .GetOnConnStop ())
7176 return c
7277}
7378
@@ -84,13 +89,15 @@ func (c *Connection) StartReader() {
8489 headData := make ([]byte , dp .GetHeadLen ())
8590 if _ , err := io .ReadFull (c .GetTCPConn (), headData ); err != nil {
8691 fmt .Println ("read msg head error " , err )
92+ c .IsAborted .Store (true )
8793 break
8894 }
8995
9096 //拆包,得到msgid 和 datalen 放在msg中
9197 msg , err := dp .Unpack (headData )
9298 if err != nil {
9399 fmt .Println ("unpack error " , err )
100+ c .IsAborted .Store (true )
94101 break
95102 }
96103
@@ -100,23 +107,20 @@ func (c *Connection) StartReader() {
100107 data = make ([]byte , msg .GetDataLen ())
101108 if _ , err := io .ReadFull (c .GetTCPConn (), data ); err != nil {
102109 fmt .Println ("read msg data error " , err )
103- // continue
110+ c . IsAborted . Store ( true )
104111 break
105112 }
106113 }
107114 msg .SetData (data )
108115
109- //得到当前客户端请求的Request数据
110116 req := Request {
111117 conn : c ,
112- msg : msg , //将之前的buf 改成 msg
118+ msg : msg ,
113119 }
114120
115121 if cutils .GlobalObject .WorkerPoolSize > 0 {
116- //已经启动工作池机制,将消息交给Worker处理
117122 c .MsgHandler .SendMsgToTaskQueue (& req )
118123 } else {
119- //从绑定好的消息和对应的处理方法中执行对应的Handle方法
120124 go c .MsgHandler .DoMsgHandler (& req )
121125 }
122126 }
@@ -132,10 +136,15 @@ func (c *Connection) StartWriter() {
132136
133137 for {
134138 select {
135- case data := <- c .msgChan :
136- //有数据要写给客户端
137- if _ , err := c .Conn .Write (data ); err != nil {
138- fmt .Println ("Send Data error:, " , err , " Conn Writer exit" )
139+ case data , ok := <- c .msgChan :
140+ if ok {
141+ //有数据要写给客户端
142+ if _ , err := c .Conn .Write (data ); err != nil {
143+ fmt .Println ("Send Data error:, " , err , " Conn Writer exit" )
144+ return
145+ }
146+ } else {
147+ fmt .Println ("msgChan is Closed" )
139148 return
140149 }
141150 //针对有缓冲channel需要些的数据处理
@@ -148,11 +157,8 @@ func (c *Connection) StartWriter() {
148157 }
149158 } else {
150159 fmt .Println ("msgBuffChan is Closed" )
151- break
160+ return
152161 }
153- case <- c .ExitBuffChan :
154- //conn已经关闭
155- return
156162 }
157163 }
158164}
@@ -171,37 +177,32 @@ func (c *Connection) Start() {
171177 for {
172178 select {
173179 case <- c .ExitBuffChan :
174- //得到退出消息,不再阻塞
180+ // 得到退出消息,不再阻塞
175181 return
176182 }
177183 }
178184}
179185
180- // 停止连接,结束当前连接状态M
186+ // 停止连接,结束当前连接状态
181187func (c * Connection ) Stop () {
182- //1. 如果当前链接已经关闭
183- if c .isClosed == true {
188+ if c .IsClosed .Load () {
184189 return
185190 }
186- c .isClosed = true
187-
188- //TODO Connection Stop() 如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用
191+ c .IsClosed .Store (true )
189192
190- //如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用
191193 c .TcpServer .CallOnConnStop (c )
192194
193195 // 关闭socket链接
194196 c .Conn .Close ()
195197
196- //通知从缓冲队列读数据的业务,该链接已经关闭
197- c .ExitBuffChan <- true
198+ c .ExitBuffChan <- struct {}{}
198199
199- //将链接从连接管理器中删除
200- c .TcpServer .GetConnMgr ().Remove (c ) //删除conn从ConnManager中
200+ // 将链接从连接管理器中删除
201+ c .TcpServer .GetConnMgr ().Remove (c )
201202
202- //关闭该链接全部管道
203203 close (c .ExitBuffChan )
204204 close (c .msgBuffChan )
205+ close (c .msgChan )
205206}
206207
207208// 从当前连接获取原始的socket TCPConn
@@ -221,7 +222,8 @@ func (c *Connection) RemoteAddr() net.Addr {
221222
222223// 直接将Message数据发送数据给远程的TCP客户端
223224func (c * Connection ) SendMsg (msgId uint32 , data []byte ) error {
224- if c .isClosed == true {
225+ IsClosed := c .IsClosed .Load ()
226+ if IsClosed {
225227 return errors .New ("Connection closed when send msg" )
226228 }
227229 //将data封包,并且发送
@@ -239,7 +241,8 @@ func (c *Connection) SendMsg(msgId uint32, data []byte) error {
239241}
240242
241243func (c * Connection ) SendBuffMsg (msgId uint32 , data []byte ) error {
242- if c .isClosed == true {
244+ IsClosed := c .IsClosed .Load ()
245+ if IsClosed {
243246 return errors .New ("Connection closed when send buff msg" )
244247 }
245248 //将data封包,并且发送
0 commit comments