Exemplo n.º 1
0
        private void Login()
        {
            if (Phase < ConnectionPhase.P2LoggedIn && Phase >= ConnectionPhase.P1Connected)
            {
                var msg = new EMLogin(User);
                var tc  = AsyncRequest(StatusCode.Login, msg);
                while (!tc.IsReplied && !tc.IsTimeOut)
                {
                    ClientAgentEvent?.Invoke(this,
                                             new ClientAgentEventArgs(ClientAgentEventType.LoggingIn | ClientAgentEventType.Prompt, $"logging in, {tc.CountDown / 1000}s."));
                    Thread.Sleep(1000);
                }
                if (tc.IsReplied)
                {
                    var echo = tc.ResponseMsg;
                    if (echo.Status.HasFlag(StatusCode.Ok))
                    {
                        var loggedIn = echo as EMLoggedin;
                        this.nowBlock.Set(loggedIn.ServerTime);
                        User.Update(loggedIn.User);
                        TeleClientName = loggedIn.ServerName;
                        Token          = loggedIn.Token;
                        logger.SetOwner(TeleClientName);
                        Phase = ConnectionPhase.P2LoggedIn;
                        ClientAgentEvent?.Invoke(this,
                                                 new ClientAgentEventArgs(ClientAgentEventType.LoggedIn | ClientAgentEventType.Prompt, "login success!"));


                        this.ResetedEvent?.Invoke();
                    }
                    else  // denied
                    {
                        if (echo is EMError err)
                        {
                            if (err.Code == ErrorCode.IncorrectUsernameOrPassword ||
                                err.Code == ErrorCode.UnregisteredUser ||
                                err.Code == ErrorCode.PushedOut)
                            {
                                bOnWorking = false;  // stop working
                            }
                        }
                        ClientAgentEvent?.Invoke(this,
                                                 new ClientAgentEventArgs(ClientAgentEventType.LoggingIn | ClientAgentEventType.Error, (echo as EMText).Text));
                    }
                }
                else
                {
                    ClientAgentEvent?.Invoke(this,
                                             new ClientAgentEventArgs(ClientAgentEventType.LoggingIn | ClientAgentEventType.Error, "login timout."));
                }
            }
        }
Exemplo n.º 2
0
        public override void Destroy()
        {
            this.Logout();

            base.Destroy();
            Phase = ConnectionPhase.P0Start;
            ClientAgentEvent?.Invoke(this,
                                     new ClientAgentEventArgs(ClientAgentEventType.Disconnected | ClientAgentEventType.Prompt, $"{this} was destroyed."));

            transPool.Destroy();
            transPool       = null;
            this.postOffice = null;
            logger.Debug("transaction pool destroyed.");
        }
Exemplo n.º 3
0
        void IClientMailTransceiver.Activate(params ClientMailBox[] mailBoxes)
        {
            if (LoggedIn)
            {
                var entityNames = string.Join(",", mailBoxes.Select(mb => mb.EntityName));
                var msg         = new EMText(GetEnvelope(), entityNames);
                var reply       = Request(StatusCode.Register | StatusCode.Entity, msg);

                if (reply.HasFlag(StatusCode.Ok))
                {
                    // pass
                }
                else
                {
                    var error = reply as EMText;

                    ClientAgentEvent?.Invoke(this, new ClientAgentEventArgs(
                                                 ClientAgentEventType.Error,
                                                 $"unable to register entity '{entityNames}', detail:{error.Text}"));
                }
            }
        }
Exemplo n.º 4
0
 protected override void OnConnectionTimeout()
 {
     Phase = ConnectionPhase.P0Start;  // reconnect
     ClientAgentEvent?.Invoke(this,
                              new ClientAgentEventArgs(ClientAgentEventType.Connection | ClientAgentEventType.Error, "connection timeout."));
 }
Exemplo n.º 5
0
        private void Connect()
        {
            if (Phase < ConnectionPhase.P1Connected)  // not connected
            {
                var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // create a new thread to perform socket connection
                string errorMessage     = null;
                var    connectionThread = new Thread((x) =>
                {
                    try
                    {
                        socket.Connect(EndPoint);
                    }
                    catch (SocketException se)
                    {
                        errorMessage = se.Message;
                        // log errors
                        logger.Error(se.Message);

                        if (se.SocketErrorCode != SocketError.ConnectionRefused)
                        {  // ignore some kind of errors
                            ClientAgentEvent?.Invoke(this,
                                                     new ClientAgentEventArgs(ClientAgentEventType.Error, se.Message));
                        }
                    }
                });
                connectionThread.IsBackground = true;
                connectionThread.Start();
                var tc = timeout;
                while (!socket.Connected && tc > 0 && errorMessage == null)
                {
                    Thread.Sleep(1);
                    --tc;
                    if (tc % 1000 == 0)
                    {
                        ClientAgentEvent?.Invoke(this,
                                                 new ClientAgentEventArgs(ClientAgentEventType.Connecting | ClientAgentEventType.Prompt, $"connecting to {EndPoint}, {tc / 1000}s."));
                    }
                }

                // succeeded
                if (socket.Connected)
                {
                    base.RestSocket(socket);
                    GetControl(ThreadType.Listen).Start();
                    Phase = ConnectionPhase.P1Connected;
                    ClientAgentEvent?.Invoke(this,
                                             new ClientAgentEventArgs(ClientAgentEventType.Connected | ClientAgentEventType.Prompt, $"connected to {TeleClientName}."));
                }
                else  // failed
                {
                    if (tc <= 0)
                    {
                        errorMessage = $"connect to {EndPoint} timeout";
                        connectionThread.Abort();
                    }
                    else if (errorMessage == null)
                    {
                        errorMessage = $"unkown error, cannot connect to {EndPoint}";
                    }

                    ClientAgentEvent?.Invoke(this,
                                             new ClientAgentEventArgs(ClientAgentEventType.Connecting | ClientAgentEventType.Prompt, errorMessage));
                }
            }
        }