Zinx-V0.7
This commit is contained in:
parent
0498027955
commit
9e2811d103
@ -54,7 +54,7 @@ func init() {
|
||||
// 初始化全局配置,并设置默认值
|
||||
ConfigInstance = &Config{
|
||||
Name: "ZinxServerApp",
|
||||
Version: "V0.6",
|
||||
Version: "V0.7",
|
||||
Host: "0.0.0.0",
|
||||
TcpPort: 7777,
|
||||
MaxConn: 12000,
|
||||
|
@ -19,6 +19,8 @@ type Connection struct {
|
||||
|
||||
// 该连接的退出消息通知 channel
|
||||
ExitBuffChan chan struct{}
|
||||
// 无缓冲管道,用于读、写两个 Goroutine 之间的消息通信
|
||||
msgChan chan []byte
|
||||
}
|
||||
|
||||
// NewConnection 创建连接的方法
|
||||
@ -29,6 +31,7 @@ func NewConnection(conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandl
|
||||
IsClosed: false,
|
||||
MsgHandler: msgHandler,
|
||||
ExitBuffChan: make(chan struct{}, 1),
|
||||
msgChan: make(chan []byte),
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,10 +83,32 @@ func (c *Connection) StartReader() {
|
||||
}
|
||||
}
|
||||
|
||||
// StartWriter 处理连接写数据的业务方法
|
||||
func (c *Connection) StartWriter() {
|
||||
fmt.Println("Writer Goroutine is running")
|
||||
defer fmt.Println(c.RemoteAddr().String(), "conn Writer exit!")
|
||||
|
||||
for {
|
||||
select {
|
||||
case data := <-c.msgChan:
|
||||
// 有数据要写回客户端
|
||||
if _, err := c.Conn.Write(data); err != nil {
|
||||
fmt.Println("Send data error:", err)
|
||||
return
|
||||
}
|
||||
case <-c.ExitBuffChan:
|
||||
// 连接已经关闭
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start 启动连接,让当前连接开始工作
|
||||
func (c *Connection) Start() {
|
||||
// 启动当前连接的读数据业务
|
||||
go c.StartReader()
|
||||
// 启动当前连接的写数据业务
|
||||
go c.StartWriter()
|
||||
|
||||
for {
|
||||
select {
|
||||
@ -143,11 +168,7 @@ func (c *Connection) SendMsg(msgID uint32, data []byte) error {
|
||||
}
|
||||
|
||||
// 写回客户端
|
||||
if _, err := c.Conn.Write(binaryMsg); err != nil {
|
||||
fmt.Println("Write msg id", msgID, "error:", err)
|
||||
c.ExitBuffChan <- struct{}{}
|
||||
return fmt.Errorf("write msg id =%d, err => %s", msgID, err.Error())
|
||||
}
|
||||
c.msgChan <- binaryMsg
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user