Beispiel #1
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();
        }
Beispiel #2
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 _);
        }
Beispiel #3
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);
        }