Exemple #1
0
 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);
 }
Exemple #2
0
 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;
         }
     }
 }
Exemple #3
0
 internal void SetupHandler(ServerSocket con)
 {
     // Set the lSender field
     _sender = con;
 }
Exemple #4
0
 // 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;
     }
 }
Exemple #5
0
 internal void ResetSocket(ServerSocket socket)
 {
     Socket = socket;
 }
Exemple #6
0
 internal PlayerInfo(ServerSocket socket)
 {
     Socket = socket;
     Connected = true;
 }
Exemple #7
0
 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);
 }
Exemple #8
0
 public PlayerInfo GetClient(ServerSocket client)
 {
     return Clients.FirstOrDefault(x => x.Socket == client);
 }
Exemple #9
0
 public PlayerInfo GetClient(ServerSocket client)
 {
     return(Clients.FirstOrDefault(x => x.Socket == client));
 }
Exemple #10
0
 internal void ResetSocket(ServerSocket socket)
 {
     Socket = socket;
 }
Exemple #11
0
 internal PlayerInfo(ServerSocket socket)
 {
     Socket    = socket;
     Connected = true;
 }
Exemple #12
0
 public BinaryParser(ServerSocket socket)
 {
     _socket = socket ?? throw new ArgumentNullException(nameof(socket));
 }
Exemple #13
0
 public BinarySenderStub(ServerSocket to, Handler handler) : base(handler)
 {
     this.to = to;
 }
Exemple #14
0
 public BinarySenderStub(ServerSocket to, Handler handler)
     : base(handler)
 {
     this.to = to;
 }
Exemple #15
0
 public bool SaidHello(ServerSocket socket)
 {
     return(SaidHello(socket.Client));
 }
Exemple #16
0
 public void AddClient(ServerSocket socket)
 {
     try
     {
         _locker.EnterWriteLock();
         _players.Add(new PlayerInfo(socket));
     }
     finally
     {
         _locker.ExitWriteLock();
     }
 }
Exemple #17
0
 internal void SetupHandler(ServerSocket con)
 {
     // Set the lSender field
     _sender = con;
 }
Exemple #18
0
 public bool SaidHello(ServerSocket socket)
 {
     return SaidHello(socket.Client);
 }
Exemple #19
0
 internal PlayerInfo(State state, ServerSocket socket)
 {
     _state    = state;
     Socket    = socket;
     Connected = true;
 }