コード例 #1
0
        private async Task AcceptClientAsync(Socket socket)
        {
            await Task.Yield();

            AsyncSocket s = null;
            try
            {
                s = new AsyncSocket(socket);
            }
            catch (SocketException)
            {
                socket.Dispose();
                return;
            }

            _connectedClients.TryAdd(s, true);
            Console.WriteLine($"Client connected: {s.IpEndPoint?.ToString().Replace("::ffff:", "")}");

            try
            {
                while (true)
                {
                    Task<AsyncSocket.ReadResult> receive = s.ReceiveAsync();
                    await receive;
                    ReceiveTcp(receive.Result);
                }
            }
            catch (SocketException)
            {
            }
            catch (ObjectDisposedException)
            {
            }
            finally
            {
                RemoveTarget(s);
            }
        }
コード例 #2
0
ファイル: Broker.Transfer.cs プロジェクト: Ploug/BOD-Server
        /// <summary>
        /// Awaits a message from the client containing a sessionId.
        /// If the sessionId matches any we have given out. Assign the found PlayerEndPoint
        /// to the client. We write back the sessionId again as a signal that the client should
        /// begin attempting to "connect" with UDP.
        /// </summary>
        /// <param name="socket"> The TCP socket for the client. </param>
        /// <returns> true if the client succesfully verified through TCP. false otherwise. </returns>
        private async Task<bool> AddTCPTarget(AsyncSocket socket)
        {
            AsyncSocket.ReadResult result = await socket.ReceiveAsync();
            if (result.BytesRead != SESSIONID_LENGTH)
            {
                return false;
            }

            string sessionId = Convert.ToBase64String(result.Buffer);
            PlayerEndPoint playerEndPoint;
            if (!_playerSessionTokens.TryGetValue(sessionId, out playerEndPoint))
            {
                return false;
            }

            if (sessionId != playerEndPoint.SessionId)
                return false;

            playerEndPoint.TCPSocket = socket;

            await socket.SendMessage(result.Buffer);

            return _playerEndPoints.TryAdd(socket.IpEndPoint, playerEndPoint);
        }
コード例 #3
0
ファイル: Broker.Transfer.cs プロジェクト: Ploug/BOD-Server
        private async Task ReceiveFromClientAsync(AsyncSocket s)
        {
            while (true)
            {
                AsyncSocket.ReadResult result = await s.ReceiveAsync();

                LightEvent e;
                if (_connectedClients.TryGetValue(s, out e))
                {
                    // Reset the LightEvent's timer to prevent timeout.
                    e.Reset();
                }

                Read(result.Buffer, s.IpEndPoint);
            }
        }
コード例 #4
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();
            }
        }