private void OnConnectionRequested(ConnectionRequest request) { if (_networker.PeersCount < MaxPlayers) { string pin = request.Data.GetString(); if (pin == Pin) { NetPeer peer = request.Accept(); string userName = request.Data.GetString(); peer.Tag = userName; Players.Add(peer); Log.Information("Connection request from {endPoint} with username {userName} accepted", request.RemoteEndPoint, userName); } else { request.Reject(NetConstants.GetKeyValue(NetKey.DisconnectInvalidPin)); Log.Information("Connection request from {endPoint} rejected due to invalid key", request.RemoteEndPoint); } } else { request.Reject(NetConstants.GetKeyValue(NetKey.DisconnectServerFull)); Log.Information("Connection request from {endPoint} rejected as server full", request.RemoteEndPoint); } }
public void Kick(int playerId) { NetPeer peer = Players.FirstOrDefault(p => p.Id == playerId); peer.Disconnect(NetConstants.GetKeyValue(NetKey.Kicked)); Players.Remove(peer); Log.Information("Kicked player '{name}' at {endPoint}", peer.Tag, peer.EndPoint); }
public override void Stop() { if (!IsRunning) { throw App.CreateError <InvalidOperationException>("Server is not running"); } foreach (NetPeer peer in RunNetworkerTask(() => _networker.ConnectedPeerList)) { peer.Disconnect(NetConstants.GetKeyValue(NetKey.DisconnectServerClosed)); } Players.Clear(); base.Stop(); Log.Information("Server stopped"); }
/// <summary> /// Changes the maximum number of players in the server. If the server has more players than the new maximum, the last players to connect will be disconnected /// </summary> /// <param name="maxPlayers">The new number of maximum players</param> /// <param name="disconnectAll">If true, all players are disconnected, regardless of how many are currently connected</param> public void ChangeMaxPlayers(int maxPlayers, bool disconnectAll = false) { MaxPlayers = maxPlayers; if (disconnectAll) { foreach (NetPeer peer in _networker.ConnectedPeerList) { peer.Disconnect(NetConstants.GetKeyValue(NetKey.DisconnectLimitChanged)); } } else { RunNetworkerTask(() => { while (_networker.PeersCount > MaxPlayers) { _networker.ConnectedPeerList[_networker.PeersCount - 1].Disconnect(NetConstants.GetKeyValue(NetKey.DisconnectLimitChanged)); } }); } }
public void RunGame() { RunNetworkerTask(() => _networker.SendToAll(NetConstants.GetKeyValue(GameKey.GameStart), DMethod)); // TODO - run game }
private void OnPeerConnected(NetPeer peer) { peer.Send(NetConstants.GetKeyValue(GameKey.JoinedGame), DMethod); Log.Information("Alerting client that connection was successful"); }