Example #1
0
        /// <summary>
        /// Completes a pending asynchronous socket connection attempt.
        /// </summary>
        /// <param name="ar">The <see cref="IAsyncResult" /> instance returned by the initiating call to <see cref="BeginConnect" />.</param>
        /// <exception cref="SocketException">Thrown if the connection could not be establised.</exception>
        /// <remarks>
        /// <note>
        /// All successful calls to <see cref="BeginConnect" /> should be eventually matched with a call to <see cref="EndConnect" />.
        /// </note>
        /// </remarks>
        public void EndConnect(IAsyncResult ar)
        {
            lock (syncLock)
            {
                if (isTcp)
                {
                    sock.EndConnect(ar);
                }
                else
                {
                    var addresses = Dns.EndGetHostAddresses(ar);

                    sock.Bind();

                    udpRemoteEndPoint = new IPEndPoint(addresses[0], remoteBinding.Port);
                    isUdpConnected    = true;

                    // $hack(jeff.lill)
                    //
                    // This is a minor hack.  Instead of adding the additional complexity of transmitting the
                    // connection packet asynchronously, I'm just going to make a synchronous call here.  This
                    // shouldn't ever block in real life since the socket send buffer starts out empty.

                    sock.SendTo(udpConnectPacket, udpRemoteEndPoint);

                    udpConnectPacket = null;   // Don't need this any longer
                }
            }
        }
Example #2
0
        /// <summary>
        /// Completes an asynchronous connection attempt.
        /// </summary>
        /// <param name="ar">The async result returned by <see cref="BeginConnect(string, System.AsyncCallback, object)" />.</param>
        public void EndConnect(IAsyncResult ar)
        {
            using (TimedLock.Lock(this))
            {
                sock.EndConnect(ar);

                this.ipAddress    = ((IPEndPoint)sock.RemoteEndPoint).Address;
                this.timedOut     = false;
                this.queryPending = false;

                HttpStack.AddConnection(this);
            }
        }
Example #3
0
 private void OnConnect(IAsyncResult ar)
 {
     try
     {
         syncCompletion = ar.CompletedSynchronously;
         sock.EndConnect(ar);
     }
     catch (Exception e)
     {
         asyncException = e;
     }
     finally
     {
         asyncEvent.Set();
     }
 }
Example #4
0
        public void EnhancedSocket_Async_ConnectToHost()
        {
            EnhancedSocket sock;
            IAsyncResult   ar;
            IPAddress      addr;

            sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            ar   = sock.BeginConnect("www.google.com", 80, null, null);
            sock.EndConnect(ar);
            addr = ((IPEndPoint)sock.RemoteEndPoint).Address;
            sock.Close();

            sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            ar   = sock.BeginConnect(addr.ToString(), 80, null, null);
            sock.EndConnect(ar);
            sock.Close();
        }
Example #5
0
        /// <summary>
        /// Handles connection completions.
        /// </summary>
        /// <param name="ar">The async result.</param>
        private void OnConnect(IAsyncResult ar)
        {
            using (TimedLock.Lock(router.SyncRoot))
            {
                try
                {
                    if (sock == null)
                    {
                        return;
                    }

                    sock.EndConnect(ar);

                    connected = true;
                    remoteEP  = new ChannelEP(Transport.Tcp, new IPEndPoint(((IPEndPoint)sock.RemoteEndPoint).Address, 0));

                    router.Trace(2, "TCP: Connected", "LocalEP=" + localEP.NetEP.ToString() + " remoteEP=" + remoteEP.NetEP.ToString(), null);
                    SetLastAccess();

                    // Initiation reception of the first message

                    BeginReceive();

                    // Start sending any queued messages

                    Assertion.Test(!sending);
                    if (sendQueue.Count > 0)
                    {
                        Msg msg;

                        msg = Dequeue();
                        Transmit(msg._ToEP.ChannelEP, msg, false);
                    }
                }
                catch (Exception e)
                {
                    router.Trace(string.Format(null, "TCP: Connect Failed"), e);
                    router.OnTcpClose(this);
                    Close();
                }
            }
        }