/// <summary> /// 移除发送心跳包的对象 /// </summary> public void RemoveHeartBeatObject(PlayerSocket target) { if (HeartBeatList.ContainsKey(target)) { HeartBeatList[target].Dispose();//释放资源 HeartBeatList.Remove(target);//移除队列 } }
public GameRoom(int roomID, PlayerSocket playerSocketA, PlayerSocket playerSocketB) { this.roomID = roomID; this.playerSocketA = playerSocketA; this.playerSocketB = playerSocketB; this.playerDataA = new GamePlayerData(this, PlayerPosition.A); this.playerDataB = new GamePlayerData(this, PlayerPosition.B); }
/// <summary> /// 添加发送心跳包的对象 /// </summary> public void AddHeartBeatObject(PlayerSocket target) { if (!HeartBeatList.ContainsKey(target)) { Timer timer = new Timer(SendHeartBeatPackage, (object)target, 0, 10 * 1000); HeartBeatList.Add(target, timer);//添加到队列 } }
/// <summary> /// 绑定连接 /// </summary> public void BindSocket(PlayerInfoData playerInfo, Socket socket) { try { int index = unknownSocket.IndexOf(socket); //添加到已绑定的socket列表 PlayerSocket playerSocket = new PlayerSocket(playerInfo, socket); freedomPlayer.Add(playerSocket); //从未知连接列表中删除 unknownSocket.Remove(socket); //日志记录 LogsSystem.Instance.Print(string.Format("绑定成功[{0},{1}]", playerInfo.playerName, socket.RemoteEndPoint.ToString())); //绑定后尝试分配房间 TryAllocRoom(); } catch (Exception ex) { LogsSystem.Instance.Print("绑定连接失败,可能是不存在该连接:" + ex.ToString(), LogLevel.WARN); } }
/// <summary> /// 玩家登陆游戏服务器 /// </summary> public void PlayerLoginGameServer(int uid, string playerName, string UUID, Socket socket) { //检查玩家是否已经登陆。如果已登陆则踢出之前的 foreach (PlayerSocket onlinePlayer in gamePlayerList) { string onlinePlayerUUID = onlinePlayer.playerInfo.playerUUID; if (onlinePlayerUUID == UUID) { GameData data = new GameData(); data.operateCode = OperateCode.Offline; data.returnCode = ReturnCode.Refuse; TcpServer.Instance.Send(onlinePlayer.socket, data); //TcpServer.Instance. //UdpServer.Instance.SendMsg(JsonCoding<GameData>.encode(model), onlinePlayer.IPed.Address.ToString(), onlinePlayer.IPed.Port);//发送断线消息 this.gamePlayerList.Remove(onlinePlayer); } } //登陆系统 PlayerInfoData pid = new PlayerInfoData(); pid.playerUid = uid; pid.playerName = playerName; pid.playerUUID = UUID; PlayerSocket ps = new PlayerSocket(pid, socket); this.gamePlayerList.Add(ps); }
/// <summary> /// 创建房间 /// </summary> public GameRoom CreateRoom(PlayerSocket playerSocketA, PlayerSocket playerSocketB) { int roomID = availableRoomID; GameRoom newroom = new GameRoom(roomID, playerSocketA, playerSocketB); rooms.Add(newroom); availableRoomID++; /* //对playerA发送的卡片信息 TcpServer.Instance.Send(playerSocketA.socket, TcpServer.Instance.GetTCPDataSender().SendPlayerOwnCard(newroom, GameRoom.PlayerPosition.A)); //对playerB发送的卡片信息 TcpServer.Instance.Send(playerSocketB.socket, TcpServer.Instance.GetTCPDataSender().SendPlayerOwnCard(newroom, GameRoom.PlayerPosition.B)); */ //发送数据,让客户端建立房间 //通用数据 GameData roomData = new GameData(); roomData.operateCode = OperateCode.AllocRoom; roomData.returnCode = ReturnCode.Success; //对playerA发送的房间信息 AllocRoomData roomDataToPlayerA = new AllocRoomData(); roomDataToPlayerA.roomID = roomID; roomDataToPlayerA.allocPosition = AllocRoomData.Position.A; roomDataToPlayerA.rivalName = playerSocketB.playerInfo.playerName; roomDataToPlayerA.rivalUid = playerSocketB.playerInfo.playerUid; roomDataToPlayerA.rivalUUID = playerSocketB.playerInfo.playerUUID; string messageToA = JsonCoding<AllocRoomData>.encode(roomDataToPlayerA); //对playerB发送的房间信息 AllocRoomData roomDataToPlayerB = new AllocRoomData(); roomDataToPlayerB.roomID = roomID; roomDataToPlayerB.allocPosition = AllocRoomData.Position.B; roomDataToPlayerB.rivalName = playerSocketA.playerInfo.playerName; roomDataToPlayerB.rivalUid = playerSocketA.playerInfo.playerUid; roomDataToPlayerB.rivalUUID = playerSocketA.playerInfo.playerUUID; string messageToB = JsonCoding<AllocRoomData>.encode(roomDataToPlayerB); //对A发送信息 roomData.operateData = messageToA; TcpServer.Instance.Send(playerSocketA.socket, roomData); //对B发送信息 roomData.operateData = messageToB; TcpServer.Instance.Send(playerSocketB.socket, roomData); LogsSystem.Instance.Print(string.Format("房间{0}创建完毕;对战玩家[{1},{2}]", roomID, playerSocketA.playerInfo.playerName, playerSocketB.playerInfo.playerName)); return newroom; }