コード例 #1
0
        private void ListenForConnections()
        {
            _listener.Start();

            while (true)
            {
                try
                {
                    TcpClient client = _listener.AcceptTcpClient();
                    IPEndPoint endpoint = (IPEndPoint) client.Client.LocalEndPoint;
                    Console.WriteLine("Accepted from: " + endpoint);
                    AsyncConnection conn = new AsyncConnection(client);

                    lock (_connections)
                    {
                        _connections.Add(endpoint, conn);
                    }
                    PeerProxy proxy = CreateProxy();
                    proxy.Connection = conn;
                    conn.Proxy = proxy;

                    if (ConnectionAccepted != null)
                    {
                        ConnectionAccepted(proxy);
                    }

                    proxy.TryRestartTimer();

                    conn.BeginReceiving(ReceivingCallback);
                }
                catch (Exception e)
                {
                    Bug.Pr(e);
                }
            }
        }
コード例 #2
0
        public PeerProxy EndConnect(IAsyncResult result)
        {
            Tuple<ConnectionAsyncResult, IPAddress> tuple = (Tuple<ConnectionAsyncResult, IPAddress>) result.AsyncState;
            ConnectionAsyncResult connResult = tuple.Item1;
            IPAddress address = tuple.Item2;
            try
            {
                connResult.Client.EndConnect(result);

                AsyncConnection connection = new AsyncConnection(connResult.Client);

                lock (_connections)
                    _connections.Add((IPEndPoint) connResult.Client.Client.LocalEndPoint, connection);

                lock (_unfinishedConnections)
                    _unfinishedConnections.Remove(address);

                connection.Proxy = connResult.Proxy;
                connResult.Proxy.Connection = connection;
                connection.Proxy.TryRestartTimer();

                connection.BeginReceiving(ReceivingCallback);
            }
            catch (ConnectionClosedException)
            {
                HandleDisconnect(connResult.Proxy, true);
                // moze wystapic przy wysylaniu wiadomosci z kolejki
                // w connResult.Proxy.Connection = connection;
                throw;
            }
            catch (SocketException)
            {
                HandleDisconnect(connResult.Proxy, true);
                throw new CannotConnectException();
            }
            catch (ObjectDisposedException)
            {
                HandleDisconnect(connResult.Proxy, true);
                throw new CannotConnectException();
            }
            catch (Exception e)
            {
                HandleDisconnect(connResult.Proxy, true);
                Bug.Pr(e);
            }
            return connResult.Proxy;
        }