예제 #1
0
 public async Task RemovePeer(AsyncPeer peer, int type, object payload = null)
 {
     if (_peers.TryRemove(peer.PeerId, out _))
     {
         await peer.Send(type, payload).ConfigureAwait(false);
     }
 }
예제 #2
0
        public async Task Start(IPAddress address, int bindPort = 9050)
        {
            if (_alive)
            {
                throw new Exception("Cannot start, client is running");
            }

            var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
            {
                NoDelay = true
            };
            await socket.ConnectAsync(new IPEndPoint(address, bindPort)).ConfigureAwait(false);

            _alive = true;

            Peer = new AsyncPeer(socket);

            try
            {
                await Peer.Process().ConfigureAwait(false);
            }
            catch { }

            ShutDown();
        }
예제 #3
0
        public async Task ProcessUDPBuffer(AsyncPeer peer, byte[] buffer)
        {
            try
            {
                var(type, size) = ReadHeader(buffer);

                if (size == 0)
                {
                    await AsyncTcp.PeerHandler.UnpackUnreliableMessage(peer, type, null).ConfigureAwait(false);

                    return;
                }

                using (var stream = new MemoryStream(size))
                {
                    await stream.WriteAsync(buffer, 8, size).ConfigureAwait(false);

                    stream.Position = 0;

                    await AsyncTcp.PeerHandler.UnpackUnreliableMessage(peer, type, stream).ConfigureAwait(false);
                }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
예제 #4
0
        public Task RemovePeer(AsyncPeer peer, ReadOnlyMemory <byte> buffer)
        {
            if (_peers.TryRemove(peer.EndPoint.Address, out _))
            {
                return(peer.Send(AsyncTcp.ErrorType, buffer));
            }

            return(Task.CompletedTask);
        }
예제 #5
0
 public async Task SendUnreliable(AsyncPeer peer, ReadOnlyMemory <byte> buffer)
 {
     try
     {
         await _udpSocket
         .SendToAsync(buffer.GetArray(), SocketFlags.None, peer.UdpEndpoint)
         .ConfigureAwait(false);
     }
     catch { }
 }
예제 #6
0
        private async Task ProcessSocket(Socket socket)
        {
            var peer = new AsyncPeer(socket);

            _peers.TryAdd(peer.PeerId, peer);

            try
            {
                await peer.Process().ConfigureAwait(false);
            }
            catch { }

            _peers.TryRemove(peer.PeerId, out _);
        }
예제 #7
0
        public async Task Start(IPAddress address, int bindPort = 9050)
        {
            if (_alive)
            {
                throw new Exception("Cannot start, client is running");
            }

            if (address == null)
            {
                throw new Exception("Specify server address to use");
            }

            if (address.AddressFamily != AddressFamily.InterNetwork)
            {
                throw new Exception("Address family must be of type Internetwork for UDP support");
            }

            _udpSocket = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
            var tcpSocket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
            {
                NoDelay = true
            };
            await tcpSocket.ConnectAsync(new IPEndPoint(address, bindPort)).ConfigureAwait(false);

            _alive = true;

            var receiveTask = ProcessReceiveUnreliable();

            try
            {
                _peer = new AsyncPeer(tcpSocket);

                await _peer.Process().ConfigureAwait(false);
            }
            catch
            { }

            ShutDown();

            await receiveTask.ConfigureAwait(false);
        }
예제 #8
0
 public Task Send(AsyncPeer peer, int type, ReadOnlyMemory <byte> buffer)
 {
     return(peer.Send(type, buffer));
 }
예제 #9
0
 public Task Send(AsyncPeer peer, int type)
 {
     return(peer.Send(type));
 }