Exemplo n.º 1
0
        public async void Start()
        {
            IsActive = true;

            listener.Start();


            while (IsActive)
            {
                TCPConnection connection = null;

                try
                {
                    TcpClient client = await listener.AcceptTcpClientAsync();

                    client.SendBufferSize    = connectionBufferSize;
                    client.ReceiveBufferSize = connectionBufferSize;
                    client.SendTimeout       = DefaultSendTimeoutMs;
                    client.ReceiveTimeout    = DefaultReceiveTimeoutMs;

                    connection             = new TCPConnection(client, connectionBufferSize);
                    connection.SyncContext = SyncContext;
                }
                catch //(Exception e)
                {
                    if (OnConnected != null)
                    {
                        OnConnected(null);
                    }
                    //Util.Log(e.Message);
                }

                if (SyncContext != null)
                {
                    SyncContext.Post((state) => {
                        CallbackParam param = (CallbackParam)state;
                        if (OnConnected != null)
                        {
                            OnConnected(param.Connection);
                        }

                        if (param.Connection != null)
                        {
                            param.Connection.Start();
                        }
                    }, new CallbackParam(connection));
                }
                else
                {
                    if (OnConnected != null)
                    {
                        OnConnected(connection);
                    }
                    if (connection != null)
                    {
                        connection.Start();
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async void Start()
        {
            IsActive = true;

            listener.Start();


            while (IsActive)
            {
                try
                {
                    TcpClient client = await listener.AcceptTcpClientAsync();

                    client.SendBufferSize    = connectionBufferSize;
                    client.ReceiveBufferSize = connectionBufferSize;
                    client.SendTimeout       = DefaultSendTimeoutMs;
                    client.ReceiveTimeout    = DefaultReceiveTimeoutMs;

                    TCPConnection connection = new TCPConnection(client, connectionBufferSize);

                    if (OnConnected != null)
                    {
                        OnConnected(connection);
                    }

                    connection.Start();
                }
                catch //(Exception e)
                {
                    if (OnConnected != null)
                    {
                        OnConnected(null);
                    }
                    //Util.Log(e.Message);
                }
            }
        }
Exemplo n.º 3
0
        public async Task Connect(string ip)
        {
            TcpClient client = new TcpClient();

            client.SendBufferSize    = connectionBufferSize;
            client.ReceiveBufferSize = connectionBufferSize;
            client.SendTimeout       = DefaultSendTimeoutMs;
            client.ReceiveTimeout    = DefaultReceiveTimeoutMs;

            try
            {
                Task con_task = client.ConnectAsync(ip, portNum);
                if (!con_task.Wait(connectTimeOutSec))
                {
                    client.Close();
                    throw new SocketException(10060); // 10060:WSAETIMEDOUT
                }
            }
            catch (SocketException e)
            {
                if (Global.SyncContext != null)
                {
                    Global.SyncContext.Post((state) => {
                        CallbackParam param = (CallbackParam)state;
                        if (OnConnected != null)
                        {
                            OnConnected(param.Ip, param.Connection);
                        }
                    }, new CallbackParam(ip, null));
                }
                else
                {
                    if (OnConnected != null)
                    {
                        OnConnected(ip, null);
                    }
                }
                //Util.Log("SocketException");
            }
            catch (AggregateException e)
            {
                if (e.InnerException is SocketException)
                {
                    if (Global.SyncContext != null)
                    {
                        Global.SyncContext.Post((state) => {
                            CallbackParam param = (CallbackParam)state;
                            if (OnConnected != null)
                            {
                                OnConnected(param.Ip, param.Connection);
                            }
                        }, new CallbackParam(ip, null));
                    }
                    else
                    {
                        if (OnConnected != null)
                        {
                            OnConnected(ip, null);
                        }
                    }
                    //Util.Log("AggregateException");
                }
            }

            TCPConnection connection = new TCPConnection(client, connectionBufferSize);

            if (Global.SyncContext != null)
            {
                Global.SyncContext.Post((state) => {
                    CallbackParam param = (CallbackParam)state;
                    if (OnConnected != null)
                    {
                        OnConnected(param.Ip, param.Connection);
                    }
                }, new CallbackParam(ip, connection));
            }
            else
            {
                if (OnConnected != null)
                {
                    OnConnected(ip, connection);
                }
            }
            await connection.Start();
        }