internal void ReceiveMessage(byte[] data, ServerSocket con) { //Debug.WriteLine("[Message] {0}", data[4]); // Check if this is the first message received if (!State.Instance.SaidHello(con)) { var acceptableMessages = new byte[] { 1, // Error 4, // Hello 5, // HelloAgain 92, // Ping }; //TODO Maybe we shouldn't kill the connection here // Basically, if someone dc's it's possible that // a network call gets sent up on accident before HelloAgain, // which effectivly kills the game. // Maybe need a flag on the player saying they at least said // hello once. // A new connection must always start with a hello message, refuse the connection if (acceptableMessages.Contains(data[4]) == false) { var pi = State.Instance.GetClient(con); pi.Kick(false, L.D.ServerMessage__FailedToSendHelloMessage); State.Instance.RemoveClient(pi); return; } } // Set the lSender field _sender = con; _sender.OnPingReceived(); // Parse and handle the message _binParser.Parse(data); }
private async Task ListenSingle() { try { TcpClient client = null; try { client = await _tcp.AcceptTcpClientAsync(); } catch (ObjectDisposedException) { return; } if (client != null) { Log.InfoFormat("New Connection {0}", client.Client.RemoteEndPoint); var sc = new ServerSocket(client, this); Context.State.Players.AddClient(sc); return; } throw new NotImplementedException(); } catch (SocketException e) { if (e.SocketErrorCode != SocketError.Interrupted) { throw; } } }
internal void SetupHandler(ServerSocket con) { // Set the lSender field _sender = con; }
// Main thread function: waits and accept incoming connections private void Listen(object o) { // Retrieve the parameter var started = (ManualResetEvent)o; // Start the server and signal it _tcp.Start(); started.Set(); try { while (!_closed) { // Accept new connections var con = _tcp.AcceptTcpClient(); if (con != null) { var sc = new ServerSocket(con, this); State.Instance.AddClient(sc); } } } catch (SocketException e) { // Interrupted is expected when the server gets stopped if (e.SocketErrorCode != SocketError.Interrupted) throw; } }
internal void ResetSocket(ServerSocket socket) { Socket = socket; }
internal PlayerInfo(ServerSocket socket) { Socket = socket; Connected = true; }
internal void ReceiveMessage(byte[] data, ServerSocket con) { //Debug.WriteLine("[Message] {0}", data[4]); // Check if this is the first message received if (!State.Instance.SaidHello(con)) { // A new connection must always start with a hello message, refuse the connection if (data[4] != (byte)3 && data[4] != (byte)4) { var pi = State.Instance.GetClient(con); pi.Kick("You must shake hands. No one likes an anti social connection."); State.Instance.RemoveClient(pi); return; } } // Set the lSender field _sender = con; _sender.OnPingReceived(); // Parse and handle the message _binParser.Parse(data); }
public PlayerInfo GetClient(ServerSocket client) { return Clients.FirstOrDefault(x => x.Socket == client); }
public PlayerInfo GetClient(ServerSocket client) { return(Clients.FirstOrDefault(x => x.Socket == client)); }
public BinaryParser(ServerSocket socket) { _socket = socket ?? throw new ArgumentNullException(nameof(socket)); }
public BinarySenderStub(ServerSocket to, Handler handler) : base(handler) { this.to = to; }
public bool SaidHello(ServerSocket socket) { return(SaidHello(socket.Client)); }
public void AddClient(ServerSocket socket) { try { _locker.EnterWriteLock(); _players.Add(new PlayerInfo(socket)); } finally { _locker.ExitWriteLock(); } }
public bool SaidHello(ServerSocket socket) { return SaidHello(socket.Client); }
internal PlayerInfo(State state, ServerSocket socket) { _state = state; Socket = socket; Connected = true; }