Exemplo n.º 1
0
        void AcceptConnection()
        {
            Socket newSocket;

            try
            {
                if (!listener.Server.IsBound)
                    return;

                newSocket = listener.AcceptSocket();
            }
            catch (Exception e)
            {
                /* TODO: Could have an exception here when listener 'goes away' when calling AcceptConnection! */
                /* Alternative would be to use locking but the listener doesn't go away without a reason. */
                Log.Write("server", "Accepting the connection failed.", e);
                return;
            }

            var newConn = new Connection { Socket = newSocket };
            try
            {
                newConn.Socket.Blocking = false;
                newConn.Socket.NoDelay = true;

                // assign the player number.
                newConn.PlayerIndex = ChooseFreePlayerIndex();
                SendData(newConn.Socket, BitConverter.GetBytes(ProtocolVersion.Version));
                SendData(newConn.Socket, BitConverter.GetBytes(newConn.PlayerIndex));
                PreConns.Add(newConn);

                // Dispatch a handshake order
                var request = new HandshakeRequest
                {
                    Mod = ModData.Manifest.Id,
                    Version = ModData.Manifest.Metadata.Version,
                    Map = LobbyInfo.GlobalSettings.Map
                };

                DispatchOrdersToClient(newConn, 0, 0, new ServerOrder("HandshakeRequest", request.Serialize()).Serialize());
            }
            catch (Exception e)
            {
                DropClient(newConn);
                Log.Write("server", "Dropping client {0} because handshake failed: {1}", newConn.PlayerIndex.ToString(CultureInfo.InvariantCulture), e);
            }
        }
Exemplo n.º 2
0
        void AcceptConnection()
        {
            Socket newSocket = null;

            try
            {
                if (!listener.Server.IsBound) return;
                newSocket = listener.AcceptSocket();
            }
            catch
            {
                /* could have an exception here when listener 'goes away' when calling AcceptConnection! */
                /* alternative would be to use locking but the listener doesnt go away without a reason */
                return;
            }

            var newConn = new Connection { socket = newSocket };
            try
            {
                newConn.socket.Blocking = false;
                newConn.socket.NoDelay = true;

                // assign the player number.
                newConn.PlayerIndex = ChooseFreePlayerIndex();
                newConn.socket.Send(BitConverter.GetBytes(ProtocolVersion.Version));
                newConn.socket.Send(BitConverter.GetBytes(newConn.PlayerIndex));
                preConns.Add(newConn);

                // Dispatch a handshake order
                var request = new HandshakeRequest()
                {
                    Map = lobbyInfo.GlobalSettings.Map,
                    Mods = lobbyInfo.GlobalSettings.Mods.Select(m => "{0}@{1}".F(m,Mod.AllMods[m].Version)).ToArray()
                };
                DispatchOrdersToClient(newConn, 0, 0, new ServerOrder("HandshakeRequest", request.Serialize()).Serialize());
            }
            catch (Exception) { DropClient(newConn); }
        }