private void PacketProcess(Connect connect) { //消息长度小于一个消息头的长度,消息出错 if (connect.buffCount < sizeof(Int32)) { return; } Array.Copy(connect.buff, connect.lengthBytes, sizeof(Int32)); connect.msgLength = BitConverter.ToInt32(connect.lengthBytes, 0); if (connect.buffCount < connect.msgLength + sizeof(Int32)) { return; } //处理 消息 BaseProtocol proto = this.protocol.Decode(connect.buff, sizeof(Int32), connect.msgLength); HandleMsg(connect, proto); //消息已处理 下标更新 int count = connect.buffCount - connect.msgLength - sizeof(Int32); Array.Copy(connect.buff, sizeof(Int32) + connect.msgLength, connect.buff, 0, count); connect.buffCount = count; if (connect.buffCount > 0) { PacketProcess(connect); } }
/// <summary> /// 玩家特殊魔法,带终点 /// </summary> public void MsgPlayerMagicEndpoint(Player player, BaseProtocol baseProtocol) { //玩家特殊魔法,终点生成 //消息结构: (string)PlayerMagic + (string)playerName + (int)magicItemID + (float)endposX+(float)endposY+(float)endposZ if (player.playerStatus.status != PlayerStatus.Status.Gaming) { return; } Room room = player.playerStatus.room; int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); string playerName = p.GetString(startIndex, ref startIndex); int magicItemID = p.GetInt(startIndex, ref startIndex); float posX = p.GetFloat(startIndex, ref startIndex); float posY = p.GetFloat(startIndex, ref startIndex); float posZ = p.GetFloat(startIndex, ref startIndex); //转发魔法消息 BytesProtocol p_broadcast = new BytesProtocol(); p_broadcast.SpliceString("PlayerMagicEndpoint"); p_broadcast.SpliceString(player.name); p_broadcast.SpliceInt(magicItemID); p_broadcast.SpliceFloat(posX); p_broadcast.SpliceFloat(posY); p_broadcast.SpliceFloat(posZ); room.Broadcast(p_broadcast); }
private void HandleMsg(Connect connect, BaseProtocol baseProtocol) { string protocolName = baseProtocol.GetProtocolName(); string methodName = "Msg" + protocolName; //当连接并未有玩家使用 或 接收方法为心跳消息(连接协议) if (connect.player == null || methodName == "HeartBeat" || methodName == "Disconnect") { MethodInfo methodInfo = handleConnectMsg.GetType().GetMethod(methodName); if (methodInfo == null) { Console.WriteLine("[警告](连接消息)没有此方法:" + methodName + "已忽略"); return; } object[] param = new object[] { connect, baseProtocol }; Console.WriteLine("[客户端 " + connect.GetAddress() + " ](连接消息):" + methodName + " 处理"); methodInfo.Invoke(handleConnectMsg, param); } else { //玩家协议: MethodInfo methodInfo = handlePlayerMsg.GetType().GetMethod(methodName); if (methodInfo == null) { Console.WriteLine("[警告](玩家消息)没有此方法:" + methodName + "已忽略"); return; } object[] param = new object[] { connect.player, baseProtocol }; Console.WriteLine("[客户端 " + connect.player.name + " ](玩家消息):" + methodName + " 处理"); methodInfo.Invoke(handlePlayerMsg, param); } }
/// <summary> /// 玩家一般魔法,起点位置,起点旋转度 /// </summary> /// <param name="player"></param> /// <param name="baseProtocol"></param> public void MsgPlayerMagic(Player player, BaseProtocol baseProtocol) { //玩家魔法 //消息结构: (string)PlayerMagic + (string)magicName + (float)posX + (float)posY +(float)posZ+ (float)rotX + (float)rotY + (float)rotZ //if (player.playerStatus.status != PlayerStatus.Status.Gaming) //{ // return; //} Room room = player.playerStatus.room; int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); string magicName = p.GetString(startIndex, ref startIndex); float posX = p.GetFloat(startIndex, ref startIndex); float posY = p.GetFloat(startIndex, ref startIndex); float posZ = p.GetFloat(startIndex, ref startIndex); float rotX = p.GetFloat(startIndex, ref startIndex); float rotY = p.GetFloat(startIndex, ref startIndex); float rotZ = p.GetFloat(startIndex, ref startIndex); //转发魔法消息 BytesProtocol p_broadcast = new BytesProtocol(); p_broadcast.SpliceString("PlayerMagic"); p_broadcast.SpliceString(magicName); p_broadcast.SpliceFloat(posX); p_broadcast.SpliceFloat(posY); p_broadcast.SpliceFloat(posZ); p_broadcast.SpliceFloat(rotX); p_broadcast.SpliceFloat(rotY); p_broadcast.SpliceFloat(rotZ); room.Broadcast(p_broadcast); }
/// <summary> /// 玩家被毒圈伤害 /// </summary> public void MsgPlayerPoison(Player player, BaseProtocol baseProtocol) { //玩家被毒圈伤害协议 //消息结构:(string)PlayerPoison Room room = player.playerStatus.room; int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); if (player.playerStatus.HP > 0) { if (room.circlefieldIndex <= room.circlefieldInfo.Circlefields.Count - 1) { player.playerStatus.HP -= room.circlefieldInfo.Circlefields[room.circlefieldIndex].DamagePerSec; } } else { //玩家死亡协议 //消息结构:(string)PlayerDead + (string)playerName BytesProtocol deadProtool = new BytesProtocol(); deadProtool.SpliceString("PlayerDead"); deadProtool.SpliceString(player.name); room.Broadcast(deadProtool); RoomManager.instance.LeaveRoom(player); } room.PlayerSuccess(); }
/// <summary> /// 开启服务器 /// </summary> /// <param name="host">主机</param> /// <param name="port">端口号</param> public void Start(string host, int port, int MaxConnectCount, long HeartBeatTime) { maxConnectCount = MaxConnectCount; heartBeatTime = HeartBeatTime; //初始化协议 protocol = new BytesProtocol(); //初始化计时器 timer.Elapsed += new ElapsedEventHandler(HandleMainTimer); timer.AutoReset = false; timer.Enabled = true; //初始化连接池 connects = new Connect[maxConnectCount]; for (int i = 0; i < maxConnectCount; i++) { connects[i] = new Connect(); } //新建Socket Listenfd = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Bind IPAddress iPAddress = IPAddress.Parse(host); IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, port); Listenfd.Bind(iPEndPoint); //Listen(最大连接数) Listenfd.Listen(maxConnectCount); //异步Accept Listenfd.BeginAccept(AcceptCallBack, null); Console.WriteLine("[服务器]:启动成功 连接类型TCP"); }
public void MsgConnect(Connect connect, BaseProtocol Protocol) { int startIndex = 0; BytesProtocol bytesProtocol = (BytesProtocol)Protocol; string protocolName = bytesProtocol.GetString(startIndex, ref startIndex); string name = bytesProtocol.GetString(startIndex, ref startIndex); Console.WriteLine("[客户端 " + connect.GetAddress() + " ](连接消息) 以用户名:" + name + " 连接"); BytesProtocol bytesProtocolReturn = new BytesProtocol(); bytesProtocolReturn.SpliceString("Connect"); //名字已被使用,无法连接,返回-1,连接失败 if (Player.NameIsUsed(name)) { bytesProtocolReturn.SpliceInt(-1); connect.Send(bytesProtocolReturn); return; } //为连接初始化角色数据 connect.player = new Player(name, connect); //触发连接事件 Server.instance.handlePlayerEvent.OnConnect(connect.player); //返回0,即连接成功 bytesProtocolReturn.SpliceInt(0); connect.Send(bytesProtocolReturn); return; }
/// <summary> /// 玩家获得有害状态,转发 /// </summary> public void MsgPlayerGetBuff(Player player, BaseProtocol baseProtocol) { //消息结构: (string)PlayerName + (int)BuffType +(float)buffTime int startIndex = 0; BytesProtocol get = baseProtocol as BytesProtocol; Room room = player.playerStatus.room; get.GetString(startIndex, ref startIndex); string PlayerName = get.GetString(startIndex, ref startIndex); int bufftype = get.GetInt(startIndex, ref startIndex); float bufftime = get.GetFloat(startIndex, ref startIndex); if (room.playerDict.ContainsKey(PlayerName)) { room.playerDict[PlayerName].playerStatus.buffType = bufftype; room.playerDict[PlayerName].playerStatus.buffTime = bufftime; } BytesProtocol p = new BytesProtocol(); p.SpliceString("PlayerGetBuff"); p.SpliceString(PlayerName); p.SpliceInt(bufftype); p.SpliceFloat(bufftime); foreach (Player pr in room.playerDict.Values) { p.SpliceString(pr.name); } player.Send(p); }
/// <summary> /// 广播 /// </summary> public void Broadcast(BaseProtocol protocol) { foreach (Player player in playerDict.Values) { player.Send(protocol); } }
public void Send(BaseProtocol Protocol) { if (connect == null) { return; } Server.instance.Send(connect, Protocol); }
/// <summary> /// 获取房间消息 /// </summary> /// <param name="player"></param> /// <param name="baseProtocol"></param> public void MsgGetRoomInfo(Player player, BaseProtocol baseProtocol) { if (player.playerStatus.status != PlayerStatus.Status.InRoom) { Console.WriteLine("[客户端 " + player.name + " ]" + "请求获取房间消息(MsgGetRoomInfo):玩家不在房间中,不需获取"); return; } //返回所在房间信息 player.Send(player.playerStatus.room.GetRoomInfo()); }
/// <summary> /// 拾取物品 /// </summary> public void MsgPickItem(Player player, BaseProtocol baseProtocol) { //拾取物品 //消息结构: (string)PickItem + (int)GroundItemID int startIndex = 0; BytesProtocol get = baseProtocol as BytesProtocol; get.GetString(startIndex, ref startIndex); int GroundItemID = get.GetInt(startIndex, ref startIndex); BytesProtocol p = new BytesProtocol(); p.SpliceString("PickItem"); p.SpliceInt(GroundItemID); player.playerStatus.room.Broadcast(p); }
/// <summary> /// 往一个连接发送消息 /// </summary> /// <param name="connect"></param> /// <param name="protocol"></param> public void Send(Connect connect, BaseProtocol protocol) { byte[] bytes = protocol.Encode(); byte[] length = BitConverter.GetBytes(bytes.Length); byte[] sendbuff = length.Concat(bytes).ToArray(); try { connect.socket.BeginSend(sendbuff, 0, sendbuff.Length, SocketFlags.None, null, null); } catch (Exception e) { Console.WriteLine("[发送消息]" + connect.GetAddress() + " : " + e.Message); } }
/// <summary> /// 广播消息 /// </summary> /// <param name="protocol"></param> public void Broadcast(BaseProtocol protocol) { for (int i = 0; i < connects.Length; i++) { if (connects[i].isUse == false) { continue; } if (connects[i].player == null) { continue; } Send(connects[i], protocol); } }
public void MsgRequestStartGame(Player player, BaseProtocol baseProtocol) { BytesProtocol protocol = new BytesProtocol(); protocol.SpliceString("RequestStartGame"); /* if (!player.playerStatus.room.CanStart()) { protocol.SpliceInt(-1); player.Send(protocol); return; } */ protocol.SpliceInt(0); player.Send(protocol); player.playerStatus.room.StartGame(); }
/// <summary> /// 房间门打开转发 /// </summary> /// <param name="player"></param> /// <param name="baseProtocol"></param> public void MsgDoorOpen(Player player, BaseProtocol baseProtocol) { //开门 //消息结构: (string)DoorOpen + (int)DoorID Room room = player.playerStatus.room; int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); int DoorID = p.GetInt(startIndex, ref startIndex); BytesProtocol doorProtocol = new BytesProtocol(); doorProtocol.SpliceString("DoorOpen"); doorProtocol.SpliceInt(DoorID); room.Broadcast(doorProtocol); }
/// <summary> /// 击中玩家 /// </summary> public void MsgPlayerHitSomeone(Player player, BaseProtocol baseProtocol) { //击中玩家协议 //消息结构:(string)PlayerHitSomeone + (string)PlayerName + (float)Damage //玩家不在游戏状态,当没事发生 Room room = player.playerStatus.room; int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); string HitplayerName = p.GetString(startIndex, ref startIndex); float Damage = p.GetFloat(startIndex, ref startIndex); if (room.playerDict.ContainsKey(HitplayerName)) { lock (room.playerDict) { //扣血操作 room.playerDict[HitplayerName].playerStatus.HP -= Damage; //死亡后自动离开房间 if (room.playerDict[HitplayerName].playerStatus.HP <= 0) { //玩家死亡协议 //消息结构:(string)PlayerKilled + (string)Killer + (string)KilledPlayer BytesProtocol deadProtool = new BytesProtocol(); deadProtool.SpliceString("PlayerKilled"); deadProtool.SpliceString(player.name); deadProtool.SpliceString(HitplayerName); room.Broadcast(deadProtool); RoomManager.instance.LeaveRoom(room.playerDict[HitplayerName]); } } } else { //玩家不在房间中,返回 return; } room.PlayerSuccess(); }
/// <summary> /// 获取下落点 /// </summary> public void MsgDroppoint(Player player, BaseProtocol baseProtocol) { int startIndex = 0; BytesProtocol get = baseProtocol as BytesProtocol; get.GetString(startIndex, ref startIndex); Room room = player.playerStatus.room; BytesProtocol p = new BytesProtocol(); p.SpliceString("Droppoint"); Random rand = new Random(); lock (room.playerDroppoints) { int index = rand.Next(0, room.playerDroppoints.Count - 1); room.playerDroppoints.RemoveAt(index); p.SpliceInt(room.playerDroppoints[index]); } player.Send(p); }
public void MsgDisconnect(Connect connect, BaseProtocol Protocol) { BytesProtocol bytesProtocol = new BytesProtocol(); bytesProtocol.SpliceString("Disconnect"); bytesProtocol.SpliceInt(0); if (connect.player == null) { connect.Send(bytesProtocol); connect.Close(); } else { connect.Send(bytesProtocol); connect.player.Disconnect(); } }
/// <summary> /// 玩家死亡,自杀时客户端上报 /// </summary> public void MsgPlayerDead(Player player, BaseProtocol baseProtocol) { int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; Room room = player.playerStatus.room; p.GetString(startIndex, ref startIndex); BytesProtocol ret = new BytesProtocol(); if (room.playerDict.ContainsKey(player.name)) { ret.SpliceString("PlayerDead"); ret.SpliceString(player.name); room.Broadcast(ret); RoomManager.instance.LeaveRoom(room.playerDict[player.name]); } room.PlayerSuccess(); }
/// <summary> /// 获取房间玩家信息 /// </summary> public void MsgGetPlayersInfo(Player player, BaseProtocol baseProtocol) { //消息结构: (string)GetPlayersInfo + (int)PlayerNum +(string)PlayerName1 + ... +(string)PlayerName# int startIndex = 0; BytesProtocol get = baseProtocol as BytesProtocol; get.GetString(startIndex, ref startIndex); Room room = player.playerStatus.room; BytesProtocol p = new BytesProtocol(); p.SpliceString("GetPlayersInfo"); p.SpliceInt(room.playerDict.Count); foreach (Player pr in room.playerDict.Values) { p.SpliceString(pr.name); } player.Send(p); }
/// <summary> /// 玩家加载完毕,定时器开始计时 /// </summary> public void MsgPlayerReady(Player player, BaseProtocol baseProtocol) { int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); string playerName = p.GetString(startIndex, ref startIndex); //int playerstatus = p.GetInt(startIndex, ref startIndex); Room room = player.playerStatus.room; ////测试单人使用 //if (room.playerDict.Count == 1) //{ // BytesProtocol protocol = new BytesProtocol(); // protocol.SpliceString("AllPlayerLoaded"); // room.Broadcast(protocol); // room.NewTimer(); // room.beginTimer = true; // room.LastCirclefieldTime = Utility.GetTimeStamp(); // return; //} if (room.playerReadyDict.Count < room.playerDict.Count) { lock (room.playerReadyDict) { room.playerReadyDict.Add(playerName, true); } Console.WriteLine("PlayerReadyCount:{0} ", room.playerReadyDict.Count); } if (room.playerReadyDict.Count == room.playerDict.Count) { //广播-所有玩家加载完毕信息 BytesProtocol protocol = new BytesProtocol(); protocol.SpliceString("AllPlayerLoaded"); room.Broadcast(protocol); room.NewTimer(); room.beginTimer = true; room.LastCirclefieldTime = Utility.GetTimeStamp(); } }
public void MsgCreateRoom(Player player, BaseProtocol baseProtocol) { BytesProtocol bytesProtocol = new BytesProtocol(); bytesProtocol.SpliceString("CreateRoom"); //玩家的状态不是在房间中或战局中时,创建失败,返回-1 if (player.playerStatus.status != PlayerStatus.Status.Null) { bytesProtocol.SpliceInt(-1); player.Send(bytesProtocol); Console.WriteLine("MsgCreateRoom 失败,创建提出者:" + player.name); return; } RoomManager.instance.CreateRoom(player); //创建成功 返回0 bytesProtocol.SpliceInt(0); player.Send(bytesProtocol); Console.WriteLine("MsgCreateRoom 创建房间完成, 房主是: " + player.name); }
/// <summary> /// 获取地图物品数据 /// </summary> public void MsgGetMapItemData(Player player, BaseProtocol baseProtocol) { int startIndex = 0; BytesProtocol protocol = baseProtocol as BytesProtocol; protocol.GetString(startIndex, ref startIndex); Random ran = new Random((int)Utility.GetTimeStamp()); if (randomItemCode == 0) { randomItemCode = ran.Next(1, 999); } //Console.WriteLine("RandomItemCode Set: " + randomItemCode.ToString()); BytesProtocol p = new BytesProtocol(); p.SpliceString("GetMapItemData"); p.SpliceInt(randomItemCode); player.playerStatus.room.Broadcast(p); //player.Send(player.playerStatus.room.ItemsProtocol); }
/// <summary> /// 玩家扔物品 /// </summary> public void MsgDropItem(Player player, BaseProtocol baseProtocol) { //拾取物品 //消息结构: (string)PickItem + (int)GroundItemID int startIndex = 0; BytesProtocol p = baseProtocol as BytesProtocol; p.GetString(startIndex, ref startIndex); int GroundItemID = p.GetInt(startIndex, ref startIndex); float posX = p.GetFloat(startIndex, ref startIndex); float posY = p.GetFloat(startIndex, ref startIndex); float posZ = p.GetFloat(startIndex, ref startIndex); //转发魔法消息 BytesProtocol p_broadcast = new BytesProtocol(); p_broadcast.SpliceString("DropItem"); p_broadcast.SpliceInt(GroundItemID); p_broadcast.SpliceFloat(posX); p_broadcast.SpliceFloat(posY); p_broadcast.SpliceFloat(posZ); player.playerStatus.room.Broadcast(p); }
public void MsgJoinRoom(Player player, BaseProtocol baseProtocol) { int startIndex = 0; BytesProtocol Protocol = baseProtocol as BytesProtocol; string methodName = Protocol.GetString(startIndex, ref startIndex); int RoomIndex = Protocol.GetInt(startIndex, ref startIndex); Console.WriteLine("[客户端 " + player.name + " ]" + "请求加入房间(MsgJoinRoom):index:" + RoomIndex); Protocol = new BytesProtocol(); Protocol.SpliceString("JoinRoom"); if (RoomIndex < 0 || RoomIndex >= RoomManager.instance.RoomList.Count) { Console.WriteLine("[客户端 " + player.name + " ]" + "请求加入房间(MsgJoinRoom):index:" + RoomIndex + " 超出列表范围"); Protocol.SpliceInt(-1); player.Send(Protocol); return; } Room room = RoomManager.instance.RoomList[RoomIndex]; if (room.status != Room.Status.Preparing) { Console.WriteLine("[客户端 " + player.name + " ]" + "请求加入房间(MsgJoinRoom):index:" + RoomIndex + " 房间正在游玩"); Protocol.SpliceInt(-1); player.Send(Protocol); return; } if (room.AddPlayer(player)) { room.Broadcast(room.GetRoomInfo()); Protocol.SpliceInt(0); player.Send(Protocol); } else { Console.WriteLine("[客户端 " + player.name + " ]" + "请求加入房间(MsgJoinRoom):index:" + RoomIndex + " 房间人数已满"); Protocol.SpliceInt(-1); player.Send(Protocol); } }
public void MsgLeaveRoom(Player player, BaseProtocol baseProtocol) { BytesProtocol protocol = new BytesProtocol(); protocol.SpliceString("LeaveRoom"); if (player.playerStatus.status != PlayerStatus.Status.InRoom) { Console.WriteLine("[客户端 " + player.name + " ]" + "请求离开房间(MsgLeaveRoom):玩家不在房间中,离开失败"); protocol.SpliceInt(-1); player.Send(protocol); return; } protocol.SpliceInt(0); player.Send(protocol); Room room = player.playerStatus.room; RoomManager.instance.LeaveRoom(player); //离开房间时,房间仍有人,则广播新的房间玩家信息 if (room != null) { room.Broadcast(room.GetRoomInfo()); } }
/// <summary> /// 更新玩家信息 /// </summary> public void MsgUpdatePlayerInfo(Player player, BaseProtocol baseProtocol) { //if (player.playerStatus.status != PlayerStatus.Status.Gaming) //{ // return; //} BytesProtocol p = baseProtocol as BytesProtocol; int startIndex = 0; p.GetString(startIndex, ref startIndex); float posX = p.GetFloat(startIndex, ref startIndex); float posY = p.GetFloat(startIndex, ref startIndex); float posZ = p.GetFloat(startIndex, ref startIndex); float rotX = p.GetFloat(startIndex, ref startIndex); float rotY = p.GetFloat(startIndex, ref startIndex); float rotZ = p.GetFloat(startIndex, ref startIndex); string CurrentAction = p.GetString(startIndex, ref startIndex); player.playerStatus.posX = posX; player.playerStatus.posY = posY; player.playerStatus.posZ = posZ; player.playerStatus.CurrentAction = CurrentAction; player.playerStatus.LastUpdateTime = Utility.GetTimeStamp(); BytesProtocol protocolReturn = new BytesProtocol(); protocolReturn.SpliceString("UpdatePlayerInfo"); protocolReturn.SpliceString(player.name); protocolReturn.SpliceFloat(player.playerStatus.HP); protocolReturn.SpliceFloat(posX); protocolReturn.SpliceFloat(posY); protocolReturn.SpliceFloat(posZ); protocolReturn.SpliceFloat(rotX); protocolReturn.SpliceFloat(rotY); protocolReturn.SpliceFloat(rotZ); protocolReturn.SpliceString(CurrentAction); player.playerStatus.room.Broadcast(protocolReturn); }
public void MsgGetRoomList(Player player, BaseProtocol protocol) { player.Send(RoomManager.instance.GetRoomList()); }
public void Send(BaseProtocol Protocol) { Server.instance.Send(this, Protocol); }