private void HandleSearchManager(ref LoginSocketState state, string query, Dictionary <string, string> keyValues)
 {
     if (state.State == 0)
     {
         if (query.Equals("nicks", StringComparison.InvariantCultureIgnoreCase))
         {
             SendToClient(ref state, LoginServerMessages.SendNicks(ref state, keyValues));
         }
         else if (query.Equals("check", StringComparison.InvariantCultureIgnoreCase))
         {
             SendToClient(ref state, LoginServerMessages.SendCheck(ref state, keyValues));
         }
         else if (query.Equals("search", StringComparison.InvariantCultureIgnoreCase))
         {
             SendToClient(ref state, LoginServerMessages.SendSearch(ref state, keyValues));
         }
         else if (query.Equals("newuser", StringComparison.InvariantCultureIgnoreCase))
         {
             SendToClient(ref state, LoginServerMessages.NewUser(ref state, keyValues));
         }
         else
         {
             Log(Category, String.Format("[{0}] Unhandled query type: {1}", state.Type, query));
         }
     }
     else if (state.State == 1)
     {
         state.State++;
     }
     else if (state.State >= 2)
     {
         state.Dispose();
     }
 }
        private void AcceptCallback(IAsyncResult ar)
        {
            LoginSocketState state = (LoginSocketState)ar.AsyncState;

            try {
                Socket client = state.Socket.EndAccept(ar);

                Thread.Sleep(1);

                if (state.Type == LoginSocketState.SocketType.Client)
                {
                    _clientManagerReset.Set();
                }
                else if (state.Type == LoginSocketState.SocketType.Search)
                {
                    _searchManagerReset.Set();
                }

                state.Socket = client;

                Log(Category, String.Format("[{0}] New Client: {1}:{2}", state.Type, ((IPEndPoint)state.Socket.RemoteEndPoint).Address, ((IPEndPoint)state.Socket.RemoteEndPoint).Port));

                if (state.Type == LoginSocketState.SocketType.Client)
                {
                    // ClientManager server sends data first
                    byte[] buffer = LoginServerMessages.GenerateServerChallenge(ref state);
                    SendToClient(ref state, buffer);

                    if (state != null)
                    {
                        state.State++;
                    }
                }
                else if (state.Type == LoginSocketState.SocketType.Search)
                {
                    // SearchManager server waits for data first
                }
            } catch (NullReferenceException) {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
            } catch (SocketException e) {
                LogError(Category, "Error accepting client");
                LogError(Category, String.Format("{0} {1}", e.SocketErrorCode, e));
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return;
            }

            WaitForData(ref state);
        }
        private void HandleClientManager(ref LoginSocketState state, string query, Dictionary <string, string> keyValues)
        {
            if (state == null || String.IsNullOrWhiteSpace(query) || keyValues == null)
            {
                return;
            }

            if (state.State == 1)
            {
                if (query.Equals("login", StringComparison.InvariantCultureIgnoreCase))
                {
                    SendToClient(ref state, LoginServerMessages.SendProof(ref state, keyValues));
                    state.StartKeepAlive(this);
                }
                else if (query.Equals("newuser", StringComparison.InvariantCultureIgnoreCase))
                {
                    SendToClient(ref state, LoginServerMessages.NewUser(ref state, keyValues));
                }
            }
            else if (state.State == 2)
            {
                if (query.Equals("status", StringComparison.InvariantCultureIgnoreCase))
                {
                    //LogError("STATUS NOT RESPONDED","1");
                    //SendToClient(ref state, LoginServerMessages.StatusResponse(ref state, keyValues));
                }
                else if (query.Equals("getprofile", StringComparison.InvariantCultureIgnoreCase))
                {
                    SendToClient(ref state, LoginServerMessages.SendProfile(ref state, keyValues, false));
                }
                else if (query.Equals("updatepro", StringComparison.InvariantCultureIgnoreCase))
                {
                    LoginServerMessages.UpdateProfile(ref state, keyValues);
                }
            }
            else if (state.State == 3)
            {
                if (query.Equals("logout", StringComparison.InvariantCultureIgnoreCase))
                {
                    LoginServerMessages.Logout(ref state, keyValues);
                }
                else if (query.Equals("getprofile", StringComparison.InvariantCultureIgnoreCase))
                {
                    SendToClient(ref state, LoginServerMessages.SendProfile(ref state, keyValues, true));
                }
            }
            else if (state.State >= 4)
            {
                state.Dispose();
            }
        }
 private void HandleSearchManager(ref LoginSocketState state, string query, Dictionary <string, string> keyValues)
 {
     if (state.State == 0)
     {
         if (query.Equals("nicks", StringComparison.InvariantCultureIgnoreCase))
         {
             SendToClient(ref state, LoginServerMessages.SendNicks(ref state, keyValues));
         }
         else if (query.Equals("check", StringComparison.InvariantCultureIgnoreCase))
         {
             SendToClient(ref state, LoginServerMessages.SendCheck(ref state, keyValues));
         }
     }
     else if (state.State == 1)
     {
         state.State++;
     }
     else if (state.State >= 2)
     {
         state.Dispose();
     }
 }
        private void KeepAliveCallback(object s)
        {
            LoginServer server = (LoginServer)s;

            try {
                if (_keepAliveTimer == null)
                {
                    Dispose();
                    return;
                }

                LoginSocketState state = this;
                HeartbeatState++;

                Console.WriteLine("keep alive callback");
                if (!server.SendToClient(ref state, LoginServerMessages.SendKeepAlive()))
                {
                    Dispose();
                    return;
                }


                // every 2nd keep alive request, we send an additional heartbeat
                if (HeartbeatState % 2 == 0)
                {
                    //Console.WriteLine("sending heartbeat");
                    if (!server.SendToClient(ref state, LoginServerMessages.SendHeartbeat()))
                    {
                        Dispose();
                        return;
                    }
                }
                // ServerListRetrieve.
            } catch (Exception e) {
                server.LogError(LoginServer.Category, "Error running keep alive: " + e);
                Dispose();
            }
        }