Exemple #1
0
 void Server_OnDisconnected(ServerGamer gamer)
 {
     SetOnlineStatus(Gamers.IndexOf(gamer), OnlineStatus.Offline);
     if (!Gamers.Any(hdl => !hdl.IsSpectator && hdl.IsConnected))
     {
         killServer = true;
     }
 }
Exemple #2
0
        private void _ConnectClient(TcpClient client)
        {
            if (killServer)
            {
                return;
            }
            Trace.TraceInformation("Client connected");
            var        stream  = client.GetStream();
            LoginToken?token   = _ReadLoginToken(stream, 4000);
            var        timeOut = stream.ReadTimeout;

            if (token == null)
            {
                return;
            }

            Account theAccount = game.Settings.Accounts.FirstOrDefault
                                     (a => a.LoginToken.TokenString == token.Value.TokenString);
            int indexC;

            if (theAccount != null)
            {
                indexC = game.Settings.Accounts.IndexOf(theAccount);
            }
            else
            {
                indexC = numberOfGamers;
            }
            try
            {
                var packet = new ConnectionResponse()
                {
                    SelfId = indexC, Settings = game.Settings
                };
                Serializer.SerializeWithLengthPrefix <GameDataPacket>(stream, packet, PrefixStyle.Base128);
                stream.Flush();
            }
            catch (IOException)
            {
                try { stream.Close(); }
                catch (Exception) { }
                return;
            }
            ServerGamer gamer = Gamers[indexC];

            gamer.AddStream(stream);
            gamer.IsSpectator = (indexC == numberOfGamers);
            if (indexC != numberOfGamers)
            {
                gamer.StartReceiver();
                SetOnlineStatus(indexC, OnlineStatus.Online);
            }
        }
Exemple #3
0
 /// <summary>
 /// Initialize and start the server.
 /// </summary>
 public Server(Game game, int capacity, IPAddress address)
 {
     ipAddress = address;
     IPEndPoint ep = new IPEndPoint(ipAddress, 0);
     listener = new TcpListener(ep);
     listener.Start();
     ipPort = ((IPEndPoint)listener.LocalEndpoint).Port;
     numberOfGamers = capacity;
     Gamers = new List<ServerGamer>();
     for (int i = 0; i <= capacity; i++)
     {
         ServerGamer gamer = new ServerGamer() { Game = game, OnlineStatus = Network.OnlineStatus.Offline };
         gamer.OnDisconnected += Server_OnDisconnected;
         gamer.StartSender();
         Gamers.Add(gamer);
     }
     this.game = game;
     Trace.TraceInformation("Server initialized with capacity {0}", capacity);
 }
Exemple #4
0
        /// <summary>
        /// Initialize and start the server.
        /// </summary>
        public Server(Game game, int capacity, IPAddress address)
        {
            ipAddress = address;
            IPEndPoint ep = new IPEndPoint(ipAddress, 0);

            listener = new TcpListener(ep);
            listener.Start();
            ipPort         = ((IPEndPoint)listener.LocalEndpoint).Port;
            numberOfGamers = capacity;
            Gamers         = new List <ServerGamer>();
            for (int i = 0; i <= capacity; i++)
            {
                ServerGamer gamer = new ServerGamer()
                {
                    Game = game, OnlineStatus = Network.OnlineStatus.Offline
                };
                gamer.OnDisconnected += Server_OnDisconnected;
                gamer.StartSender();
                Gamers.Add(gamer);
            }
            this.game = game;
            Trace.TraceInformation("Server initialized with capacity {0}", capacity);
        }
Exemple #5
0
 void Server_OnDisconnected(ServerGamer gamer)
 {
     if (!gamer.IsSpectator)
     {
         SetOnlineStatus(Gamers.IndexOf(gamer), OnlineStatus.Offline);
     }
     if (!Gamers.Any(hdl => !hdl.IsSpectator && hdl.IsConnected))
     {
         killServer = true;
     }
 }