private void Send(byte[] bytes) { if(!client.Send(bytes, bytes.Length)) { Console.WriteLine("发送失败。"); } }
public bool sendMseeage(string str) { if (!m_isConnecting) { return(false); } try { byte[] bytes = Encoding.UTF8.GetBytes(str); // 发送 if (m_tcpClient.Send(bytes, bytes.Length)) { LogUtil.getInstance().addDebugLog("发送给数据库服务器成功:" + str); return(true); } else { LogUtil.getInstance().addDebugLog("发送给数据库服务器失败:" + str); return(false); } } catch (Exception ex) { LogUtil.getInstance().addDebugLog("发送给数据库服务器异常:" + ex.Message); return(false); } }
public void Send(byte[] data, int offset, int length) { client.Send(data, offset, length); }
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); }