func (gate *TCPGate) Run(closeSig chan bool) { server := new(network.TCPServer) server.Addr = gate.Addr server.MaxConnNum = gate.MaxConnNum server.PendingWriteNum = gate.PendingWriteNum server.LenMsgLen = gate.LenMsgLen server.MinMsgLen = gate.MinMsgLen server.MaxMsgLen = gate.MaxMsgLen server.LittleEndian = gate.LittleEndian server.NewAgent = func(conn *network.TCPConn) network.Agent { a := new(TCPAgent) a.conn = conn a.gate = gate if gate.AgentChanRPC != nil { gate.AgentChanRPC.Go("NewAgent", a) } return a } server.Start() <-closeSig server.Close() }
//实现了Module接口的Run func (gate *TCPGate) Run(closeSig chan bool) { server := new(network.TCPServer) //创建TCP服务器 //设置TCP服务器相关参数 server.Addr = gate.Addr server.MaxConnNum = gate.MaxConnNum server.PendingWriteNum = gate.PendingWriteNum server.LenMsgLen = gate.LenMsgLen server.MinMsgLen = gate.MinMsgLen server.MaxMsgLen = gate.MaxMsgLen server.LittleEndian = gate.LittleEndian server.NewAgent = func(conn *network.TCPConn) network.Agent { //设置创建代理函数 a := new(TCPAgent) //创建TCP代理 a.conn = conn //保存TCP连接 a.gate = gate //保存TCP网关 if gate.AgentChanRPC != nil { //代理RPC服务器,用于接受NewAgent和CloseAgentRPC调用 gate.AgentChanRPC.Go("NewAgent", a) } return a } server.Start() //启动TCP服务器 <-closeSig //等待关闭信号 server.Close() //关闭TCP服务器 }
func (gate *Gate) Run(closeSig chan bool) { var wsServer *network.WSServer if gate.WSAddr != "" { wsServer = new(network.WSServer) wsServer.Addr = gate.WSAddr wsServer.MaxConnNum = gate.MaxConnNum wsServer.PendingWriteNum = gate.PendingWriteNum wsServer.MaxMsgLen = gate.MaxMsgLen wsServer.HTTPTimeout = gate.HTTPTimeout wsServer.NewAgent = func(conn *network.WSConn) network.Agent { a := &agent{conn: conn, gate: gate} if gate.AgentChanRPC != nil { gate.AgentChanRPC.Go("NewAgent", a) } return a } } var tcpServer *network.TCPServer if gate.TCPAddr != "" { tcpServer = new(network.TCPServer) tcpServer.Addr = gate.TCPAddr tcpServer.MaxConnNum = gate.MaxConnNum tcpServer.PendingWriteNum = gate.PendingWriteNum tcpServer.LenMsgLen = gate.LenMsgLen tcpServer.MaxMsgLen = gate.MaxMsgLen tcpServer.LittleEndian = gate.LittleEndian tcpServer.NewAgent = func(conn *network.TCPConn) network.Agent { a := &agent{conn: conn, gate: gate} if gate.AgentChanRPC != nil { gate.AgentChanRPC.Go("NewAgent", a) } return a } } if wsServer != nil { wsServer.Start() } if tcpServer != nil { tcpServer.Start() } <-closeSig if wsServer != nil { wsServer.Close() } if tcpServer != nil { tcpServer.Close() } }