/// <summary> /// 断开连接 /// </summary> public void Close(ConnectID connectID) { if (mConnectionDic.ContainsKey((int)connectID)) { var c = mConnectionDic[(int)connectID]; mConnectionDic.Remove((int)connectID); c.Close(true); } }
public Connection GetConnection(ConnectID clientID) { int id = (int)clientID; if (mConnectionDic.ContainsKey(id)) { return(mConnectionDic[id]); } return(null); }
/// <summary> /// 通过TCP协议发送 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="messageId"></param> /// <param name="data"></param> public void Send(ConnectID clientID, int id, NetPacket packet) //where T : class, ProtoBuf.IExtensible { var client = GetConnection(clientID); if (client == null) { return; } byte[] bytes = packet.data; // ProtoTransfer.SerializeProtoBuf<T>(data); client.Send(bytes, (ushort)bytes.Length); }
public Connection(ConnectID id) { ID = id; IsConnecting = false; IsConnected = false; mRecvData = new byte[MAX_NET_BUFFER]; mCopyData = new byte[MAX_NET_BUFFER]; mDataNowLength = 0; mLengthData = new byte[sizeof(ushort)]; mStream = new MemoryStream(); }
public void Send(ConnectID connectid) { memoryStream.Seek(0, SeekOrigin.Begin); ProtoBuf.Serializer.Serialize <T>(memoryStream, message); int length = 4 + (int)memoryStream.Position; Packet packet = Packet.CreatePacket(length); packet.Write(BitConverter.GetBytes((int)id), 0, 4); packet.Write(memoryStream, (int)memoryStream.Position); NetworkManager.Instance.Send((int)connectid, packet); }
private void OnMessage(ConnectID id, byte[] data) { if (data == null) { return; } NetPacket packet = new NetPacket(data); lock (mLock) { mPacketQueue.Enqueue(packet); } //if (Debuger.EnableLog) //{ // OnDebug(string.Format("<color=#{0}>{1}</color>", ColorUtility.ToHtmlStringRGB(Color.green), "Receive:" + (ClientMsg.CLIENT_MSG_ID)msg.ID())); //} //MessageDispatch.Dispatch(msg); }
public void Connect(ConnectID varConnectID, string ip, int port, OnConnectionHandler onConnect, OnConnectionHandler onDisconnect) { int id = (int)varConnectID; if (mConnectionDic.ContainsKey(id)) { Connection client = mConnectionDic[id]; mConnectionDic.Remove(id); client.Close(true); } Connection c = new Connection(varConnectID); c.onConnect += onConnect; c.onDisconnect += onDisconnect; c.onDebug += OnDebug; c.onReceive += OnMessage; c.Connect(ip, port); mConnectionDic.Add(id, c); }