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); } }
//加载设备的配置信息 public override void Initialization() { if (InputServer.Instance != null) { linkNetPlayer = InputServer.Instance.getNetPlayer(inputPlayer.PlayerIndex); } }
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); }
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; } }
//分配一个位置给客户端 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)); }
public InputServer(int listenPort, int supportPlayers, string gamename, NetInputGameDeviceType gameDvice, int deviceFunction) : base(listenPort) { InputServer.Instance = this; netPlayerList = new NetInputPlayer[supportPlayers]; for (int i = 0; i < netPlayerList.Length; i++) { netPlayerList[i] = new NetInputPlayer(this, i); } gameName = gamename; gameDeviceSelect = gameDvice; gameDeviceFunction = deviceFunction; }
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); } }