void connectinInThread() { while (!m_tcpClient.Connect(NetConfig.s_mySqlService_ip, (ushort)NetConfig.s_mySqlService_port, false)) { // 连接数据库服务器失败的话会一直尝试连接 LogUtil.getInstance().addDebugLog("连接数据库服务器失败"); Thread.Sleep(1000); } m_isConnecting = true; LogUtil.getInstance().addDebugLog("连接数据库服务器成功"); // 游戏在线统计 Request_OnlineStatistics.doRequest("", 0, "", true, (int)Request_OnlineStatistics.OnlineStatisticsType.OnlineStatisticsType_clear); // 数据清空 { PVPGameRoomDataScript.clear(); } { // 拉取机器人列表 Request_GetAIList.doRequest(); } return; }
public Pack(int receiveBufferSize, string ip, int port, uint headerFlag) { client = new TcpPackClient(receiveBufferSize, headerFlag); client.OnConnect += Client_OnConnect; client.OnReceive += Client_OnReceive; client.OnSend += Client_OnSend; client.OnClose += Client_OnClose; client.Connect(ip, port); }
void connectinInThread() { while (!m_tcpClient.Connect(NetConfig.s_mySqlService_ip, (ushort)NetConfig.s_mySqlService_port, false)) { // 连接数据库服务器失败的话会一直尝试连接 LogUtil.getInstance().addDebugLog("连接数据库服务器失败"); Thread.Sleep(1000); } m_isConnecting = true; LogUtil.getInstance().addDebugLog("连接数据库服务器成功"); return; }
public bool Connect(string IP, ushort port) { client.Address = IP; client.Port = port; if (!client.Connect()) return false; for(int i = 0; i < maxtimeout / 100; i++) { if (!client.IsConnected) { Thread.Sleep(100); } else { return true; } } return false; }
private static int MainInternal(IPEndPoint serverend, ushort agentport) { dict.Clear(); server.OnReceive += delegate(IServer sender, IntPtr connId, byte[] bytes) { if (!dict.Values.Contains(connId)) { Message message = new Message(); message.MergeFrom(bytes); //这里需不需要拷贝? if (message.PacketType == PacketType.MessageToServer) //是否有其他可能?如何处理? { MessageToServer msg = message.Content as MessageToServer; UInt64 key = ((UInt64)msg.PlayerID | ((UInt64)msg.TeamID << 32)); if (dict.ContainsKey(key)) { Console.WriteLine($"More than one client claims to have the same ID {msg.TeamID} {msg.PlayerID}."); return(HandleResult.Error); } dict.TryAdd(key, connId);//不可能false } } client.Send(bytes, bytes.Length); return(HandleResult.Ok); }; //去年是Client退出前还要专门发一个消息,今年不太清楚,暂且如此 server.OnClose += delegate(IServer sender, IntPtr connId, SocketOperation socketOperation, int errorCode) { foreach (UInt64 id in dict.Keys) { if (dict[id] == connId) { if (!dict.TryRemove(id, out IntPtr temp)) { return(HandleResult.Error); } Console.WriteLine($"Player {id>>32} {id&0xffffffff} closed the connection."); break; } } return(HandleResult.Ok); }; client.OnReceive += delegate(IClient sender, byte[] bytes) { MemoryStream istream = new MemoryStream(bytes); BinaryReader br = new BinaryReader(istream); PacketType type = (PacketType)br.PeekChar(); br.Close();//debug期间加的,也许有用也许没用 istream.Dispose(); if (type == PacketType.MessageToOneClient) { Message m = new Message(); m.MergeFrom(bytes); MessageToOneClient message = m.Content as MessageToOneClient; UInt64 key = ((UInt64)message.PlayerID | ((UInt64)message.TeamID << 32)); if (!dict.ContainsKey(key)) { Console.WriteLine($"Error: No such player corresponding to ID {message.TeamID} {message.PlayerID}"); return(HandleResult.Error); } if (!server.Send(dict[key], bytes, bytes.Length)) { Console.WriteLine($"向{dict[key]}发送失败。"); } return(HandleResult.Ok); } foreach (IntPtr connId in server.GetAllConnectionIds()) { if (!server.Send(connId, bytes, bytes.Length)) { Console.WriteLine($"向{connId}发送失败。"); } } return(HandleResult.Ok); }; Console.WriteLine("Connecting......"); if (!client.Connect(serverend.Address.ToString(), (ushort)serverend.Port)) { Console.WriteLine("Failed to connect with the game server."); } else { int i; for (i = 0; i < 300; i++) { if (client.IsConnected) { Console.WriteLine("Connect with the game server successfully."); break; } else { Thread.Sleep(100); } } if (i == 300) { Console.WriteLine("Failed to connect with the game server."); } } server.Port = agentport; if (server.Start()) { Console.WriteLine("Agent starts listening."); } else { Console.WriteLine("Agent starts unsuccessfully"); } Thread.Sleep(int.MaxValue); dict.Clear(); server.Stop(); server.Dispose(); client.Stop(); client.Dispose(); return(0); }