示例#1
0
        public Task <Scene> Login(Dictionary <string, string> authContext)
        {
            if (_authenticated)
            {
                return(TaskHelper.FromException <Scene>(new InvalidOperationException("Already authenticated.")));
            }

            return(GetAuthenticationScene().Then(authScene =>
            {
                ConnectionState = GameConnectionState.Authenticating;

                return authScene.RpcTask <Dictionary <string, string>, LoginResult>(_loginRoute, authContext);
            })
                   .Then(loginResult =>
            {
                if (loginResult.Success)
                {
                    ConnectionState = GameConnectionState.Authenticated;
                    UserId = loginResult.UserId;
                    UserName = loginResult.UserName;
                    return _client.GetScene(loginResult.Token);
                }
                else
                {
                    throw new Exception(loginResult.ErrorMsg);
                }
            }));
        }
示例#2
0
        public void Poll()
        {
            int elapsed = Environment.TickCount - m_lastTickCount;

            m_lastTickCount = Environment.TickCount;
            GameConnectionState originalState = State;

            switch (State)
            {
            case GameConnectionState.Disconnected:
            {
                m_connectionBackOffTimer -= elapsed;
                if (m_connectionBackOffTimer <= 0.0f)
                {
                    ChangeState(GameConnectionState.Connecting);
                }
                break;
            }

            case GameConnectionState.Connecting:
            {
                if (m_stateTime == 0.0f)
                {
                    m_socket                     = new Socket(SocketType.Stream, ProtocolType.Tcp);
                    m_socket.NoDelay             = true;
                    m_socket.LingerState.Enabled = false;

                    m_connect_args                = new SocketAsyncEventArgs();
                    m_connect_args.Completed     += AsyncConnectCompleted;
                    m_connect_args.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(Configuration.IP), Configuration.Port);

                    System.Console.WriteLine("[{0}:{1}] Starting connection ...", Configuration.IP, Configuration.Port);
                    m_socket.ConnectAsync(m_connect_args);
                }
                break;
            }

            case GameConnectionState.Connected:
            {
                if ((m_socket.Poll(0, SelectMode.SelectRead) && m_socket.Available == 0) || !m_socket.Connected)
                {
                    System.Console.WriteLine("[{0}:{1}] Disconnected.", Configuration.IP, Configuration.Port);

                    ChangeState(GameConnectionState.Disconnected);
                    m_connectionBackOffTimer = ConnectionFailureBackoff;
                }
                else
                {
                    if (m_recieving_payload)
                    {
                        if (m_socket.Available >= 0)
                        {
                            int toRead = Math.Min((int)(m_payload_size - m_payload_buffer_read), (int)m_socket.Available);
                            m_socket.Receive(m_payload_buffer, m_payload_buffer_read, toRead, SocketFlags.None);
                            m_payload_buffer_read += toRead;

                            if (m_payload_buffer_read >= m_payload_size)
                            {
                                Recieve(m_payload_buffer);

                                m_payload_buffer_read = 0;
                                m_recieving_payload   = false;
                            }
                        }
                    }
                    else if (m_socket.Available > 4)
                    {
                        byte[] sizeBuffer = new byte[4];
                        m_socket.Receive(sizeBuffer);
                        m_payload_size        = BitConverter.ToUInt32(sizeBuffer, 0);
                        m_payload_buffer      = new byte[m_payload_size];
                        m_payload_buffer_read = 0;
                        m_recieving_payload   = true;
                    }
                }
                break;
            }
            }

            if (State == originalState)
            {
                m_stateTime += elapsed;
            }
        }
示例#3
0
 private void ChangeState(GameConnectionState NewState)
 {
     State            = NewState;
     StateChangeDirty = true;
     m_stateTime      = 0.0f;
 }