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服务器 }