Example #1
0
 /// <summary>
 /// TODO: Documentation StartTcpPunch
 /// </summary>
 /// <param name="successCallback"></param>
 /// <param name="failureCallback"></param>
 public void StartTcpPunch(HolePunchSuccessHandler successCallback, HolePunchFailureHandler failureCallback)
 {
     this.PunchThread = new Thread(() =>
     {
         this.TcpPunchThreadStart(successCallback, failureCallback);
     });
     this.PunchThread.Start();
 }
Example #2
0
        /// <summary>
        /// TODO: Documentation TcpPunchThreadStart
        /// </summary>
        /// <param name="successCallback"></param>
        /// <param name="failureCallback"></param>
        private void TcpPunchThreadStart(HolePunchSuccessHandler successCallback, HolePunchFailureHandler failureCallback)
        {
            TcpClient client = null;

            foreach (IPEndPoint peerEP in this.PeersEP)
            {
                Int32   nbTries = 0;
                Boolean quit    = false;

                try
                {
                    while (!quit)
                    {
                        nbTries++;

                        client = new TcpClient();

                        client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);

                        if (this.HostEP != null)
                        {
                            client.Client.Bind(this.HostEP);
                        }

                        IAsyncResult result = client.Client.BeginConnect(peerEP.Address, peerEP.Port, null, null);

                        result.AsyncWaitHandle.WaitOne(3000, true);

                        if (nbTries == 3 || client.Client.Connected)
                        {
                            quit = true;
                        }

                        client.Client.Close();
                    }
                }
                catch (Exception)
                {
                    quit   = true;
                    client = null;
                }

                if (quit)
                {
                    break;
                }
            }


            if (client != null)
            {
                if (client.Client.Connected)
                {
                    if (successCallback != null)
                    {
                        if (this.Invoker == null)
                        {
                            successCallback(this, client.Client, this.Data);
                        }
                        else
                        {
                            this.Invoker.Invoke(new MethodInvoker(() => { successCallback(this, client.Client, this.Data); }));
                        }
                    }
                }
                else
                {
                    client.Client.Close();

                    if (failureCallback != null)
                    {
                        if (this.Invoker == null)
                        {
                            failureCallback(this, this.Data);
                        }
                        else
                        {
                            this.Invoker.Invoke(new MethodInvoker(() => { failureCallback(this, this.Data); }));
                        }
                    }
                }
            }
            this.ClearEPs();
        }
Example #3
0
        /// <summary>
        /// TODO: Documentation TcpPunchThreadStart
        /// </summary>
        /// <param name="successCallback"></param>
        /// <param name="failureCallback"></param>
        private void TcpPunchThreadStart(HolePunchSuccessHandler successCallback, HolePunchFailureHandler failureCallback)
        {
            TcpClient client = null;

            foreach (IPEndPoint peerEP in this.PeersEP)
            {
                Int32 nbTries = 0;
                Boolean quit = false;

                try
                {
                    while (!quit)
                    {
                        nbTries++;

                        client = new TcpClient();

                        client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);

                        if (this.HostEP != null)
                            client.Client.Bind(this.HostEP);

                        IAsyncResult result = client.Client.BeginConnect(peerEP.Address, peerEP.Port, null, null);

                        result.AsyncWaitHandle.WaitOne(3000, true);

                        if (nbTries == 3 || client.Client.Connected)
                            quit = true;

                        client.Client.Close();
                    }
                }
                catch (Exception)
                {
                    quit = true;
                    client = null;
                }

                if (quit)
                    break;
            }


            if (client != null)
            {
                if (client.Client.Connected)
                {
                    if (successCallback != null)
                    {
                        if (this.Invoker == null)
                            successCallback(this, client.Client, this.Data);
                        else
                            this.Invoker.Invoke(new MethodInvoker(() => { successCallback(this, client.Client, this.Data); }));
                    }
                }
                else
                {
                    client.Client.Close();

                    if (failureCallback != null)
                    {
                        if (this.Invoker == null)
                            failureCallback(this, this.Data);
                        else
                            this.Invoker.Invoke(new MethodInvoker(() => { failureCallback(this, this.Data); }));
                    }
                }
            }
            this.ClearEPs();
        }
Example #4
0
 /// <summary>
 /// TODO: Documentation StartTcpPunch
 /// </summary>
 /// <param name="successCallback"></param>
 /// <param name="failureCallback"></param>
 public void StartTcpPunch(HolePunchSuccessHandler successCallback, HolePunchFailureHandler failureCallback)
 {
     this.PunchThread = new Thread(() =>
     {
         this.TcpPunchThreadStart(successCallback, failureCallback);
     });
     this.PunchThread.IsBackground = true;
     this.PunchThread.Start();
 }