コード例 #1
0
        private void OnReceiveData(IAsyncResult iar)
        {
            if (IsConnected)
            {
                try
                {
                    var received = ((Socket)iar.AsyncState).EndReceive(iar);
                    if (received == 0)
                    {
                        throw new SocketException();
                    }

                    var data = new byte[received];
                    Array.Copy(_buffer, data, received);

                    NetworkProtocol.ProcessParsing(this, _reader.Fill(data));
                }
                catch (Exception ex)
                {
                    if (ex is SocketException || ex is ObjectDisposedException)
                    {
                        if (!IsDisposed)
                        {
                            Disconnect(NetworkDisconnectReason.ConnectionLost);
                            return;
                        }
                    }
                }

                if (IsConnected)
                {
                    _socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(OnReceiveData), _socket);
                }
            }
        }
コード例 #2
0
ファイル: UdpClientEntity.cs プロジェクト: Etamiin/Inertia
        private void OnReceiveData(IAsyncResult iar)
        {
            if (IsConnected)
            {
                try
                {
                    IPEndPoint endPoint = null;
                    var        data     = ((UdpClient)iar.AsyncState).EndReceive(iar, ref endPoint);

                    NetworkProtocol.ProcessParsing(this, new BasicReader(data));
                }
                catch (Exception e)
                {
                    if (e is SocketException || e is ObjectDisposedException)
                    {
                        Disconnect(NetworkDisconnectReason.ConnectionLost);
                        return;
                    }
                }

                if (IsConnected)
                {
                    _client.BeginReceive(new AsyncCallback(OnReceiveData), _client);
                }
            }
        }
コード例 #3
0
ファイル: UdpServerEntity.cs プロジェクト: Etamiin/Inertia
        private void OnReceiveData(IAsyncResult iar)
        {
            try
            {
                IPEndPoint endPoint = null;
                var        data     = ((UdpClient)iar.AsyncState).EndReceive(iar, ref endPoint);

                if (!_connections.ContainsKey(endPoint))
                {
                    var connection = new UdpConnectionEntity((uint)_idProvider.GetId(), this, endPoint);
                    _connections.Add(endPoint, connection);

                    OnConnectionAdded(connection);
                }

                NetworkProtocol.ProcessParsing(_connections[endPoint], new BasicReader(data));
            }
            catch (Exception ex)
            {
                if (ex is SocketException || ex is ObjectDisposedException)
                {
                    return;
                }
            }

            if (IsInitialized)
            {
                _client.BeginReceive(new AsyncCallback(OnReceiveData), _client);
            }
        }