/// <summary> /// 从客户端接收数据 /// </summary> /// <param name="clientAddress"></param> /// <param name="data"></param> public override void Receive(string clientAddress, byte[] data) { try { //解析头部信息,转换成统一的流格式:head[MsgType(1)+uid(4)+gameId(4)+serverId(4)+gzip(1)] + (len(4)+data) BufferReader reader = new BufferReader(data); string paramString = reader.ReadPacketString(); paramString = HttpUtility.UrlDecode(paramString, Encoding.UTF8); int index = paramString.IndexOf("?d="); if (index != -1) { index += 3; paramString = paramString.Substring(index, paramString.Length - index); } PacketMessage packet = ParsePacketMessage(clientAddress, paramString, ConnectType.Tcp); var token = new UserToken(); token.GameId = packet.Head.GameId; token.ServerId = packet.Head.ServerId; token.Uid = packet.Head.Uid; ClientConnectManager.Push(clientAddress, token); if (ReceiveCompleted != null) { //分发送到游戏服 byte[] packData = packet.ToByte(); string successMsg = string.Format("{0}>>{1}接收到{2}字节!", DateTime.Now.ToString("HH:mm:ss:ms"), clientAddress, data.Length); ReceiveCompleted.BeginInvoke(clientAddress, packData, OnReceiveCompleted, successMsg); } } catch (Exception ex) { TraceLog.WriteError("Receive form client {0} error:{1}", clientAddress, ex); } }
/// <summary> /// /// </summary> /// <param name="remotePoint"></param> /// <param name="token"></param> public static void Push(string remotePoint, UserToken token) { string key = GameSession.GenerateSid(token.GameId, token.ServerId); if (_clientDict.ContainsKey(remotePoint)) { return; } _clientDict.TryAdd(remotePoint, token); List<string> list; if (!_groupDict.TryGetValue(key, out list)) { list = new List<string>(); list.Add(remotePoint); _groupDict.TryAdd(key, list); } else { lock (list) { list.Add(remotePoint); } } }