Exemplo n.º 1
0
        public void RemoveTarget(AsyncSocket socket)
        {
            LightEvent e;
            _connectedClients.TryRemove(socket, out e);

            PlayerEndPoint endPoint;
            // perhaps add socket to PlayerEndPoint so we can always efficiently remove the player from _connectedClients
            if (_playerEndPoints.TryRemove(socket.IpEndPoint, out endPoint))
            {
                BoDConsole.WriteLine($"Client: {socket.IpEndPoint.Address.MapToIPv4()}:{socket.IpEndPoint.Port} disconnected.");

                // If any PlayerEndPoint, other than the one that was just removed, 
                // references PlayerId, the player must be connected on another PlayerEndPoint.
                if (_playerEndPoints.Values.All(p => p.PlayerId != endPoint.PlayerId))
                {
                    Game game;
                    if (BoDService.PlayerIngame.TryRemove(endPoint.PlayerId, out game))
                    {
                        game.RemovePlayer(endPoint.PlayerId);
                    }
                }
            }
            socket.Dispose();
        }
Exemplo n.º 2
0
        public void RemoveTarget(AsyncSocket socket)
        {
            bool b;
            _connectedClients.TryRemove(socket, out b);

            PlayerEndPoint endPoint;
            // perhaps add socket to PlayerEndPoint so we can always efficiently remove the player from _connectedClients
            if (_playerEndPoints.TryRemove(socket.IpEndPoint, out endPoint))
            {
                Console.WriteLine($"Client: {socket.IpEndPoint.ToString().Replace("::ffff:", "")} disconnected.");
                bool b2;
                _udpEndPoints.TryRemove(endPoint.IpEndPoint, out b2);

                Game game;
                if (BoDService.PlayerIngame.TryRemove(endPoint.PlayerId, out game))
                {
                    game.RemovePlayer(endPoint.PlayerId);
                    Console.WriteLine($"Player: {endPoint.PlayerId} quit game: {game.Id}.");
                }
            }
            socket.Dispose();
        }
Exemplo n.º 3
0
        private async Task AcceptClientAsync(Socket socket)
        {
            await Task.Yield();

            AsyncSocket s = null;
            try
            {
                s = new AsyncSocket(socket);
                _connectedClients.TryAdd(s, true);
                Console.WriteLine($"Client connected: {s.IpEndPoint?.ToString().Replace("::ffff:", "")}");

                while (true)
                {
                    Task<AsyncSocket.ReadResult> receive = s.ReceiveAsync();
                    await receive;
                    ReceiveTcp(receive.Result);
                }
            }
            catch (SocketException)
            {
            }
            finally
            {
                if (s != null)
                {
                    bool b;
                    if (_connectedClients.TryRemove(s, out b))
                    {
                        IPEndPoint ip = s.IpEndPoint;
                        if (ip != null)
                        {
                            Console.WriteLine($"Client: {ip.ToString().Replace("::ffff:", "")} disconnected.");
                            RemoveTarget(ip);
                        }
                    }
                    s.Dispose();
                }
                socket?.Close();
            }
        }