private void CommandHandle_ClientClose(IPEndPoint remoteIp, NetInputData package) { //携带的玩家索引为空 if (package.PlayerIndex == NetInputData.PlayerIndexEmpty) { return; } Monitor.Enter(netPlayerList); try { //尝试清除一次分配列表内的信息 RemovePlayerFromSelectList(remoteIp); NetInputPlayer player = getNetPlayer(package.PlayerIndex); //检测这个用户是否激活了。并且和客户端是匹配的 if (player != null && player.isActive && player.remoteIp != null && player.remoteIp.Equals(remoteIp)) { //注销这个客户端位置 player.SetActive(false, null); } } catch (System.Exception ex) { } Monitor.Exit(netPlayerList); }
public void Send(NetInputData data) { if (isActive) { server.Send(remoteIp, data); } }
//0x16 同步游戏信息封包 public static NetInputData Create_Command_UpdateGameInfo(string gameName) { NetInputData ret = new NetInputData(NetCommand.Command_UpdateGameInfo); ret.Command_UpdateGameInfo_GameName = gameName; return(ret); }
//0x04 设置客户端玩家索引 public static NetInputData Create_Command_ResetPlayerIndex(int playerIndex) { NetInputData ret = new NetInputData(NetCommand.Command_ResetPlayerIndex); ret.PlayerIndex = playerIndex; return(ret); }
//0x07 用户选择一个空闲位 public static NetInputData Create_Command_SelectFreePosition(int selectPosition) { NetInputData ret = new NetInputData(NetCommand.Command_SelectFreePosition); ret.Command_SelectFreePosition_SelectPosition = selectPosition; return(ret); }
public void Shake(float time) { if (isActive) { this.Send(NetInputData.Create_Command_Shake(time)); } }
protected override void Receive(IPEndPoint remoteIp, byte[] data) { NetInputData package = new NetInputData(data); if (!package.IsValid) { return; } switch (package.Command) { case NetInputData.NetCommand.Command_InputKeyUpdate: CommandHandle_InputKeyUpdate(remoteIp, package); break; case NetInputData.NetCommand.Command_ClientClose: CommandHandle_ClientClose(remoteIp, package); break; case NetInputData.NetCommand.Command_SelectFreePosition: CommandHandle_SelectFreePosition(remoteIp, package); break; case NetInputData.NetCommand.Command_SelectPositionOk: CommandHandle_SelectPositionOk(remoteIp, package); break; case NetInputData.NetCommand.Command_ClientSense: this.Send(remoteIp, NetInputData.Create_Command_ServerRebackSense(package.Command_Sense_IpListIndex)); break; } }
private void CommandHandle_SelectPositionOk(IPEndPoint remoteIp, NetInputData package) { Monitor.Enter(netPlayerList); try { //检测是否在选位列表内 int listIndex = FindPlayerFromSelectList(remoteIp); if (listIndex == -1 || selectPositionList[listIndex].selectPosition == -1) { return; } int selectPosition = selectPositionList[listIndex].selectPosition; NetInputPlayer player = getNetPlayer(selectPosition); if (player == null || player.isActive) { return; } //从分配列表内清除这个角色 RemovePlayerFromSelectList(remoteIp); this.Send(remoteIp, NetInputData.Create_Command_SelectPositionOkReback()); AllocationPlayerPosition(remoteIp, selectPosition); } catch (System.Exception ex) { } finally { Monitor.Exit(netPlayerList); } }
//0x14 设备侦测封包回执 public static NetInputData Create_Command_ServerRebackSense(int iplistIndex) { NetInputData ret = new NetInputData(NetCommand.Command_ServerRebackSense); ret.Command_Sense_IpListIndex = iplistIndex; return(ret); }
//0x02 客户端退出封包 public static NetInputData Create_Command_ClientClose(int playerIndex) { NetInputData ret = new NetInputData(NetCommand.Command_ClientClose); ret.PlayerIndex = playerIndex; return(ret); }
public void Send(IPEndPoint remoteIp, NetInputData data) { if (udpServer != null) { udpServer.Send(data.buffer, data.buffer.Length, remoteIp); } }
//0x01 基础键位同步指令 public static NetInputData Create_Command_InputKeyUpdate(int playerIndex, int keyValue) { NetInputData ret = new NetInputData(NetCommand.Command_InputKeyUpdate); ret.PlayerIndex = playerIndex; ret.Command_InputKeyUpdate_KeyValue = keyValue; return(ret); }
//0x06 同步选位信息状态封包 public static NetInputData Create_Command_UpdateSelectPositionInfo(int supportPlayers, int positionData) { NetInputData ret = new NetInputData(NetCommand.Command_UpdateSelectPositionInfo); ret.Command_UpdateSelectPositionInfo_SupportPlayers = supportPlayers; ret.Command_UpdateSelectPositionInfo_PositionData = positionData; return(ret); }
//0x05 同步游戏信息封包 public static NetInputData Create_Command_SetGameDeviceInfo(int deviceSelect, int deviceFunction) { NetInputData ret = new NetInputData(NetCommand.Command_SetGameDeviceInfo); ret.Command_SetGameDeviceInfo_DeviceSelect = (byte)deviceSelect; ret.Command_SetGameDeviceInfo_DeviceFunction = (byte)deviceFunction; return(ret); }
//分配一个位置给客户端 private void AllocationPlayerPosition(IPEndPoint remoteIp, int index) { NetInputPlayer player = netPlayerList[index]; player.SetActive(true, remoteIp); player.Send(NetInputData.Create_Command_ResetPlayerIndex(index)); //设定游戏设备信息 player.Send(NetInputData.Create_Command_SetGameDeviceInfo((int)gameDeviceSelect, (int)gameDeviceFunction)); }
protected virtual void OnHandleReceiveCommand(NetInputPlayer player, NetInputData package) { player.activeTime = 0.0f; switch (package.Command) { case NetInputData.NetCommand.Command_InputKeyUpdate: player.netKeyValue = package.Command_InputKeyUpdate_KeyValue; break; } }
//0x11 手机震动 public static NetInputData Create_Command_Shake(float time) { int count = (int)(time / ShakeEveryTime); if (count == 0) { count = 1; } NetInputData ret = new NetInputData(NetCommand.Command_Shake); ret.Command_Shake_ShakeCount = count; return(ret); }
protected override void OnStop() { NetInputData data = NetInputData.Create_Command_ServerStop(); for (int i = 0; i < netPlayerList.Length; i++) { //发送服务端关闭封包 if (netPlayerList[i].isActive) { netPlayerList[i].Send(data); } netPlayerList[i].SetActive(false, null); } }
private void CommandHandle_SelectFreePosition(IPEndPoint remoteIp, NetInputData package) { Monitor.Enter(netPlayerList); try { //检测是否在选位列表内 int listIndex = FindPlayerFromSelectList(remoteIp); if (listIndex == -1) { this.Send(remoteIp, NetInputData.Create_Command_SelectFreePositionErr()); return; } int selectpositionindex = package.Command_SelectFreePosition_SelectPosition; int positionInfo = AccountSelectPositionInfo(); //这个位置已经被占用 if ((positionInfo & (1 << selectpositionindex)) == (1 << selectpositionindex)) { this.Send(remoteIp, NetInputData.Create_Command_SelectPositionCantUse()); return; } //标记这个位置占用 SelectPositionData data = selectPositionList[listIndex]; data.selectPosition = selectpositionindex; selectPositionList[listIndex] = data; //通知客户端选择这个位置成功了 NetInputData retpackage = package.Clone(); retpackage.Command = NetInputData.NetCommand.Command_SelectFreePositionReback; this.Send(remoteIp, retpackage); //把新的信息同步给所有选位客户端 retpackage = NetInputData.Create_Command_UpdateSelectPositionInfo( SupportPlayers, AccountSelectPositionInfo()); for (int i = 0; i < selectPositionList.Count; i++) { this.Send(selectPositionList[i].remoteIp, retpackage); } } catch (System.Exception ex) { } finally { Monitor.Exit(netPlayerList); } }
public NetInputData(NetInputData data) { buffer = new byte[data.buffer.Length]; Array.Copy(data.buffer, buffer, buffer.Length); }
private void CommandHandle_InputKeyUpdate(IPEndPoint remoteIp, NetInputData package) { Monitor.Enter(netPlayerList); try { //如果客户端的玩家索引为空,未分配 //检测分配的索引是否溢出当前用户总数 if (package.PlayerIndex == NetInputData.PlayerIndexEmpty || package.PlayerIndex >= SupportPlayers) { if (IsRemoteIpLocked(remoteIp)) { return; } //同步游戏信息 this.Send(remoteIp, NetInputData.Create_Command_UpdateGameInfo(gameName)); int freeIndex = freePlayerPosition; //没有空闲的位置了 if (freeIndex == -1) { this.Send(remoteIp, NetInputData.Create_Command_PlayersFull()); } //游戏只支持一名玩家,或者,游戏剩余的位置就一个位置了,没必要选位置了 else if (SupportPlayers == 1 || freePlayerPositionCount == 1) { AllocationPlayerPosition(remoteIp, freeIndex); } //加入选位清单,并发生选位请求 else { //把这个用户加入选位列表 AddPlayerToSelectList(remoteIp); //向客户端发起选位信息 this.Send(remoteIp, NetInputData.Create_Command_UpdateSelectPositionInfo(SupportPlayers, AccountSelectPositionInfo())); } LockRemoteIp(remoteIp); } else { NetInputPlayer player = getNetPlayer(package.PlayerIndex); if (!player.isActive) { //同步游戏信息 this.Send(remoteIp, NetInputData.Create_Command_UpdateGameInfo(gameName)); //分配这个位置给用户 AllocationPlayerPosition(remoteIp, package.PlayerIndex); } //这个位置已经被其他人激活了 else if (!player.remoteIp.Equals(remoteIp)) { if (IsRemoteIpLocked(remoteIp)) { return; } //同步游戏信息 this.Send(remoteIp, NetInputData.Create_Command_UpdateGameInfo(gameName)); int freeIndex = freePlayerPosition; //没有空闲的位置了 if (freeIndex == -1) { this.Send(remoteIp, NetInputData.Create_Command_PlayersFull()); } //游戏只支持一名玩家,或者,游戏剩余的位置就一个位置了,没必要选位置了 else if (SupportPlayers == 1 || freePlayerPositionCount == 1) { AllocationPlayerPosition(remoteIp, freeIndex); } //加入选位清单,并发生选位请求 else { //把这个用户加入选位列表 AddPlayerToSelectList(remoteIp); //向客户端发起选位信息 this.Send(remoteIp, NetInputData.Create_Command_UpdateSelectPositionInfo(SupportPlayers, AccountSelectPositionInfo())); } LockRemoteIp(remoteIp); } //这个封包就是我的封包被确认,开始处理 else { OnHandleReceiveCommand(player, package); } } } catch (System.Exception ex) { } finally { Monitor.Exit(netPlayerList); } }