Example #1
0
 public void Connect(IPEndPoint remoteEP)
 {
     Reset();
     sock.BeginConnect(remoteEP, onConnect, null);
     asyncEvent.WaitOne(MaxWaitTime, false);
     Verify();
 }
Example #2
0
 /// <summary>
 /// Initates an asynchronous connection to the server whose network
 /// endpoint is specified.
 /// </summary>
 /// <param name="endPoint">The network endpoint.</param>
 /// <param name="callback">The delegate to call when the operation completes (or <c>null</c>).</param>
 /// <param name="state">Application state.</param>
 /// <returns>The async result used to track the operation.</returns>
 public IAsyncResult BeginConnect(IPEndPoint endPoint, AsyncCallback callback, object state)
 {
     using (TimedLock.Lock(this))
     {
         sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         return(sock.BeginConnect(endPoint, callback, state));
     }
 }
Example #3
0
        /// <summary>
        /// Initiates a network connection to the message router at the
        /// specified network endpoint and then initiates the transmission
        /// of the message once the connection is established.
        /// </summary>
        /// <param name="ep">The remote router's endpoint.</param>
        /// <param name="msg">The message to be sent (or <c>null</c>).</param>
        public void Connect(IPEndPoint ep, Msg msg)
        {
            using (TimedLock.Lock(router.SyncRoot))
            {
                Assertion.Test(sock == null);
                sock    = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                localEP = new ChannelEP(Transport.Tcp, router.NormalizeEP(router.TcpEP));

                sock.NoDelay           = !router.TcpDelay;
                sock.SendBufferSize    = router.TcpSockConfig.SendBufferSize;
                sock.ReceiveBufferSize = router.TcpSockConfig.ReceiveBufferSize;

                if (router.FragmentTcp)
                {
                    sock.SendMax    = 1;
                    sock.ReceiveMax = 1;
                }

                // Queue the channel initialization message and the message passed

                Msg initMsg;

                initMsg      = new TcpInitMsg(router.RouterEP, new MsgRouterInfo(router), isUplink, router.TcpEP.Port);
                initMsg._TTL = 1;

                Serialize(initMsg);
                Enqueue(initMsg);

                try
                {
                    SetLastAccess();
                    remoteEP = new ChannelEP(Transport.Tcp, router.NormalizeEP(ep));

                    if (msg != null)
                    {
                        msg._SetToChannel(remoteEP);
                        msg._SetFromChannel(localEP);
                        msg._Trace(router, 2, "TCP: Queue", null);

                        Serialize(msg);
                        Enqueue(msg);
                    }

                    router.Trace(2, "TCP: Outbound", "LocalEP=" + localEP.NetEP.ToString() + " remoteEP=" + remoteEP.NetEP.ToString(), null);
                    sock.BeginConnect(remoteEP.NetEP, new AsyncCallback(OnConnect), null);
                }
                catch (Exception e)
                {
                    router.Trace(string.Format(null, "TCP: Connect Failed [{0}]", ep), e);
                    router.OnTcpClose(this);
                    Close();
                }
            }
        }
Example #4
0
        /// <summary>
        /// Initates an asynchronous connection to the server whose host and
        /// port are specified by a URI.
        /// </summary>
        /// <param name="uri">The uri.</param>
        /// <param name="callback">The delegate to call when the operation completes (or <c>null</c>).</param>
        /// <param name="state">Application state.</param>
        /// <returns>The async result used to track the operation.</returns>
        public IAsyncResult BeginConnect(string uri, AsyncCallback callback, object state)
        {
            var u = new Uri(uri);

            using (TimedLock.Lock(this))
            {
                if (sock != null)
                {
                    throw new InvalidOperationException(AlreadyConnectedMsg);
                }

                sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                return(sock.BeginConnect(u.Host, u.Port, callback, state));
            }
        }
Example #5
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 #6
0
        /// <summary>
        /// Initiates an asynchronous operation to establish a connection with a remote endpoint.
        /// </summary>
        /// <param name="remoteBinding">Specifies the remote endpoint.</param>
        /// <param name="packet">The packet to be transmitted for UDP sockets, ignored for TCP.</param>
        /// <param name="callback">The delegate to be called when the operation completes (or <c>null</c>).</param>
        /// <param name="state">Application specific state.</param>
        /// <returns>
        /// An <see cref="IAsyncResult" /> instance to be used to track the progress of the
        /// operation and to eventually to be passed to the <see cref="EndConnect" /> method.
        /// </returns>
        /// <exception cref="InvalidOperationException">Thrown if the socket is already been connected.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="remoteBinding" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">Thrown for UDP sockets when <paramref name="packet" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="packet" /> is empty.</exception>
        /// <remarks>
        /// <para>
        /// Due to quirks in how UDP sockets work in Silverlight and Windows Phone, a packet must be
        /// transmitted to remote endpoint as part of establishing a connection.  Use the <paramref name="packet" />
        /// parameter to pass the non-empty array of bytes to be transmitted.  This parameter is ignored
        /// for TCP connections.
        /// </para>
        /// <note>
        /// All successful calls to <see cref="BeginConnect" /> should be eventually matched with a call to <see cref="EndConnect" />.
        /// </note>
        /// </remarks>
        public IAsyncResult BeginConnect(NetworkBinding remoteBinding, byte[] packet, AsyncCallback callback, object state)
        {
            if (remoteBinding == null)
            {
                throw new ArgumentNullException("remoteBinding");
            }

            lock (syncLock)
            {
                this.remoteBinding = remoteBinding;

                if (isTcp)
                {
                    return(sock.BeginConnect(remoteBinding, callback, state));
                }
                else
                {
                    if (isUdpConnected)
                    {
                        throw new InvalidOperationException("Socket is already connected.");
                    }

                    const string packetError = "[LiteSocket] requires a valid [packet] when connecting a UDP socket.";

                    if (packet == null)
                    {
                        throw new ArgumentNullException("packet", packetError);
                    }

                    if (packet.Length == 0)
                    {
                        throw new ArgumentNullException("packet", packetError);
                    }

                    udpConnectPacket = packet;

                    return(Dns.BeginGetHostAddresses(remoteBinding.HostOrAddress, callback, state));
                }
            }
        }