Exemple #1
0
        private void ConnectCallback(IAsyncResult ar)
        {
            ConnectStateObject state = (ConnectStateObject)ar.AsyncState;

            try
            {
                state.workSocket.EndConnect(ar);
                this.IsConnected = true;
                StartReceive();
            }
            catch (SocketException ex)
            {
                state.errorType = (SocketError)ex.ErrorCode;
                --state.timesToTry;
                if (state.timesToTry <= 0)
                {
                    return;
                }

                state.workSocket.BeginConnect(state.endPoint,
                                              new System.AsyncCallback(ConnectCallback), state);
            }
            finally
            {
                SocketConnectEvent?.Invoke(this, new SocketConnectEventArgs(state, this));
                if (state.externalCallback != null)
                {
                    state.externalCallback(this, new SocketConnectEventArgs(state, this));
                }
            }
        }
Exemple #2
0
 public void ServiceStart()
 {
     CommonData.userSocket                 = new AsyncSocketClient();
     CommonData.userSocket.Address         = CommonData.userSocketConn.Address;
     CommonData.userSocket.Port            = CommonData.userSocketConn.Port;
     CommonData.userSocket.OnReceived     += Socket_OnReceived;
     CommonData.userSocket.OnConnected    += (asyncSocketClient) => SocketConnectEvent?.Invoke("connected", asyncSocketClient.Address, asyncSocketClient.Port);
     CommonData.userSocket.OnDisconnected += (asyncSocketClient) => SocketDisConnectEvent?.Invoke("disconnected", asyncSocketClient.Address, asyncSocketClient.Port);
     CommonData.userSocket.Start();
 }