public bool SendToServer(ref LoginSocketState state, byte[] data, int count)
        {
            if (data == null || state == null || state.ServerSocket == null)
            {
                return(false);
            }

            var bytes = new byte[count];

            Array.Copy(data, bytes, count);

            var str = DataFunctions.BytesToString(bytes);

            Log(Category, "RETR >> " + str);

            if (str.StartsWith("\\login\\") || str.StartsWith("\\newuser\\"))
            {
                str   = AddSteamIdBeforeFinal(str, "\\final\\");
                data  = DataFunctions.StringToBytes(str);
                count = data.Length;
                Log(Category, "RETRCHANGED >> " + str);
            }

            try
            {
                state.ServerSocket.BeginSend(data, 0, count, SocketFlags.None, OnToServerSent, state);
                return(true);
            }
            catch (NullReferenceException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return(false);
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode != SocketError.ConnectionAborted &&
                    e.SocketErrorCode != SocketError.ConnectionReset)
                {
                    LogError(Category, "Error sending data");
                    LogError(Category, String.Format("{0} {1}", e.SocketErrorCode, e));
                }
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return(false);
            }
            catch (Exception ex)
            {
                LogError(Category, "Error receiving data");
                LogError(Category, ex.ToString());
            }

            return(false);
        }
示例#2
0
        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;

                Logger.Info($"[{state.Type}] New Client: { ((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)
            {
                Logger.Error(e, $"Error accepting client. SocketErrorCode: {e.SocketErrorCode}");
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return;
            }

            WaitForData(ref state);
        }
        private void WaitForData(ref LoginSocketState state)
        {
            Thread.Sleep(10);

            try
            {
                state.GameSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, OnGameDataReceived, state);
            }
            catch (NullReferenceException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
            }
            catch (ObjectDisposedException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode == SocketError.NotConnected)
                {
                    if (state != null)
                    {
                        state.Dispose();
                    }
                    state = null;
                    return;
                }

                if (e.SocketErrorCode != SocketError.ConnectionAborted &&
                    e.SocketErrorCode != SocketError.ConnectionReset)
                {
                    LogError(Category, "Error receiving data");
                    LogError(Category, String.Format("{0} {1}", e.SocketErrorCode, e));
                }

                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return;
            }
            catch (Exception ex)
            {
                LogError(Category, "Error receiving data");
                LogError(Category, ex.ToString());
            }
        }
        private void AcceptCallback(IAsyncResult ar)
        {
            LoginSocketState state = (LoginSocketState)ar.AsyncState;

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

                Thread.Sleep(1);

                state.GameSocket = client;

                if (state.Type == LoginSocketState.SocketType.Client)
                {
                    StartClientManagerServerSocket();

                    state.ServerSocket = _clientManagerServerSocket;

                    _clientManagerServerSocket.BeginConnect(new IPEndPoint(IPAddress.Parse(GameConstants.SERVER_ADDRESS), GameConstants.CLIENT_LOGIN_TCP_PORT), OnConnect, state);
                    RestartClientAcepting();
                }
                else if (state.Type == LoginSocketState.SocketType.Search)
                {
                    StartSearchManagerServerSocket();

                    state.ServerSocket = _searchManagerServerSocket;
                    _searchManagerServerSocket.BeginConnect(new IPEndPoint(IPAddress.Parse(GameConstants.SERVER_ADDRESS), GameConstants.SEARCH_LOGIN_TCP_PORT), OnConnect, state);
                    RestartSearchAcepting();
                }
            }
            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;
            }
            catch (Exception ex)
            {
                LogError(Category, "Error receiving data");
                LogError(Category, ex.ToString());
            }
        }
        private void OnToGameSent(IAsyncResult async)
        {
            LoginSocketState state = (LoginSocketState)async.AsyncState;

            if (state == null || state.GameSocket == null)
            {
                return;
            }

            try
            {
                int sent = state.GameSocket.EndSend(async);
                //Log(Category, String.Format("[{0}] Sent {1} byte response to: {2}:{3}", state.Type, sent, ((IPEndPoint)state.GameSocket.RemoteEndPoint).Address, ((IPEndPoint)state.GameSocket.RemoteEndPoint).Port));
            }
            catch (NullReferenceException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
            }
            catch (SocketException e)
            {
                switch (e.SocketErrorCode)
                {
                case SocketError.ConnectionReset:
                case SocketError.Disconnecting:
                    if (state != null)
                    {
                        state.Dispose();
                    }
                    state = null;
                    return;

                default:
                    LogError(Category, "Error sending data");
                    LogError(Category, String.Format("{0} {1}", e.SocketErrorCode, e));
                    if (state != null)
                    {
                        state.Dispose();
                    }
                    state = null;
                    return;
                }
            }
            catch (Exception ex)
            {
                LogError(Category, "Error receiving data");
                LogError(Category, ex.ToString());
            }
        }
示例#6
0
        private void WaitForData(ref LoginSocketState state)
        {
            Thread.Sleep(10);

            try
            {
                state.Socket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, OnDataReceived, state);
            }
            catch (NullReferenceException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
            }
            catch (ObjectDisposedException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode == SocketError.NotConnected)
                {
                    if (state != null)
                    {
                        state.Dispose();
                    }
                    state = null;
                    return;
                }

                if (e.SocketErrorCode != SocketError.ConnectionAborted &&
                    e.SocketErrorCode != SocketError.ConnectionReset)
                {
                    Logger.Error(e, $"Error receiving data. SocketErrorCode: {e.SocketErrorCode}");
                }
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return;
            }
        }
示例#7
0
        private void OnSent(IAsyncResult async)
        {
            LoginSocketState state = (LoginSocketState)async.AsyncState;

            if (state == null || state.Socket == null)
            {
                return;
            }

            try
            {
                int sent = state.Socket.EndSend(async);
                Logger.Info($"[{state.Type}] Sent {sent} byte response to: " +
                            $"{((IPEndPoint)state.Socket.RemoteEndPoint).Address}:{((IPEndPoint)state.Socket.RemoteEndPoint).Port}");
            }
            catch (NullReferenceException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
            }
            catch (SocketException e)
            {
                switch (e.SocketErrorCode)
                {
                case SocketError.ConnectionReset:
                case SocketError.Disconnecting:
                    if (state != null)
                    {
                        state.Dispose();
                    }
                    state = null;
                    return;

                default:
                    Logger.Error(e, $"Error sending data. SocketErrorCode: {e.SocketErrorCode}");
                    if (state != null)
                    {
                        state.Dispose();
                    }
                    state = null;
                    return;
                }
            }
        }
        public bool SendToGame(ref LoginSocketState state, byte[] data, int count)
        {
            if (data == null || state == null || state.GameSocket == null)
            {
                return(false);
            }

            //Log("RESP", DataFunctions.BytesToString(data));

            try
            {
                state.GameSocket.BeginSend(data, 0, count, SocketFlags.None, OnToGameSent, state);
                return(true);
            }
            catch (NullReferenceException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return(false);
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode != SocketError.ConnectionAborted &&
                    e.SocketErrorCode != SocketError.ConnectionReset)
                {
                    LogError(Category, "Error sending data");
                    LogError(Category, String.Format("{0} {1}", e.SocketErrorCode, e));
                }
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return(false);
            }
            catch (Exception ex)
            {
                LogError(Category, "Error receiving data");
                LogError(Category, ex.ToString());
            }

            return(false);
        }
        public static void Logout(ref LoginSocketState state, Dictionary <string, string> keyValues)
        {
            // we're not doing anything about session, so no need to reset it back to 0...
            // maybe one day though...

            /*Dictionary<string, object> clientData = new Dictionary<string, object>() {
             *                  { "session", (Int64)0 }
             *          };
             *          LoginDatabase.Instance.SetData(state.Name, clientData);*/
            state.Dispose();
        }
示例#10
0
        public bool SendToClient(ref LoginSocketState state, byte[] data)
        {
            if (data == null || state == null || state.Socket == null)
            {
                return(false);
            }

            Logger.Info($"RESP: {DataFunctions.BytesToString(data)}");

            try
            {
                state.Socket.BeginSend(data, 0, data.Length, SocketFlags.None, OnSent, state);
                return(true);
            }
            catch (NullReferenceException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return(false);
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode != SocketError.ConnectionAborted &&
                    e.SocketErrorCode != SocketError.ConnectionReset)
                {
                    Logger.Error(e, $"Error sending data. SocketErrorCode: {e.SocketErrorCode}");
                }
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return(false);
            }
        }
示例#11
0
 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 OnGameDataReceived(IAsyncResult async)
        {
            LoginSocketState state = (LoginSocketState)async.AsyncState;

            if (state == null || state.GameSocket == null)
            {
                return;
            }

            try
            {
                // receive data from the socket
                int received = state.GameSocket.EndReceive(async);
                if (received == 0)
                {
                    goto CONTINUE;
                }

                //var str = Encoding.UTF8.GetString(state.Buffer, 0, received);

                // take what we received, and append it to the received data buffer
                //state.ReceivedData.Append(Encoding.UTF8.GetString(state.Buffer, 0, received));
                //string receivedData = state.ReceivedData.ToString();

                SendToServer(ref state, state.Buffer, received);


                // does what we received contain the \final\ delimiter?

                /*if (str.LastIndexOf(@"\final\") > -1)
                 * {
                 *  state.ReceivedData.Clear();
                 *
                 *
                 *  // lets split up the message based on the delimiter
                 *  string[] messages = receivedData.Split(new string[] { @"\final\" }, StringSplitOptions.RemoveEmptyEntries);
                 *
                 *  for (int i = 0; i < messages.Length; i++)
                 *  {
                 *      ParseMessage(ref state, messages[i]);
                 *  }
                 * }*/
            }
            catch (ObjectDisposedException)
            {
                if (state != null)
                {
                    state.Dispose();
                }
                state = null;
                return;
            }
            catch (SocketException e)
            {
                switch (e.SocketErrorCode)
                {
                case SocketError.ConnectionReset:
                case SocketError.Disconnecting:
                case SocketError.NotConnected:
                case SocketError.TimedOut:
                    if (state != null)
                    {
                        state.Dispose();
                    }
                    state = null;
                    return;

                default:
                    LogError(Category, "Error receiving data");
                    LogError(Category, String.Format("{0} {1}", e.SocketErrorCode, e));
                    if (state != null)
                    {
                        state.Dispose();
                    }
                    state = null;
                    return;
                }
            }
            catch (Exception e)
            {
                LogError(Category, "Error receiving data");
                LogError(Category, e.ToString());
            }

            // and we wait for more data...
            CONTINUE : WaitForData(ref state);
        }
示例#13
0
        private void HandleClientManager(ref LoginSocketState state, string query, Dictionary <string, string> keyValues)
        {
            if (state == null || String.IsNullOrWhiteSpace(query) || keyValues == null)
            {
                return;
            }

            if (state.State >= 4)
            {
                state.Dispose();
            }
            else
            {
                switch (query)
                {
                case "login":
                    SendToClient(ref state, LoginServerMessages.SendProof(ref state, keyValues));
                    Users[state.ProfileId] = state;
                    state.StartKeepAlive(this);

                    break;

                case "newuser":
                    SendToClient(ref state, LoginServerMessages.NewUser(ref state, keyValues));
                    break;

                case "addbuddy":
                    // \\addbuddy\\\\sesskey\\51437\\newprofileid\\1\\reason\\just be my friend

                    var newFriendId = long.Parse(keyValues["newprofileid"]);

                    if (Database.MainDBInstance.AddFriend(state.ProfileId, newFriendId))
                    {
                        //var signature = (state.ProfileId.ToString() + newFriendId.ToString()).ToMD5();
                        // "\\bm\\%d\\f\\%d\\msg\\%s|signed|%s",GPI_BM_REQUEST,profileid,reason,signature);
                        //SendToClient(ref state, $@"\bm\2\f\{newFriendId}\msg\{keyValues["reason"]}|signed|{signature}".ToAssciiBytes());

                        if (Users.TryGetValue(newFriendId, out LoginSocketState friendState))
                        {
                            SendToClient(ref friendState, $@"\bm\100\f\{state.ProfileId}\msg\|s|{state.Status}{state.GetLSParameter()}|ss|{state.StatusString}\final\".ToAssciiBytes());
                            SendToClient(ref state, $@"\bm\100\f\{newFriendId}\msg\|s|{friendState.Status}{friendState.GetLSParameter()}|ss|{friendState.StatusString}\final\".ToAssciiBytes());
                        }
                        else
                        {
                            SendToClient(ref state, $@"\bm\100\f\{newFriendId}\msg\|s|0|ss|Offline\final\".ToAssciiBytes());
                        }
                    }
                    break;

                case "delbuddy":
                    // \delbuddy\\sesskey\51437\delprofileid\1
                    Database.MainDBInstance.RemoveFriend(state.ProfileId, long.Parse(keyValues["delprofileid"]));
                    break;

                case "getprofile":
                    SendToClient(ref state, LoginServerMessages.SendProfile(ref state, keyValues));
                    break;

                case "updatepro":
                    //LoginServerMessages.UpdateProfile(ref state, keyValues);
                    break;

                case "status":

                    var remoteEndPoint = ((IPEndPoint)state.Socket.RemoteEndPoint);

                    var profile = Database.MainDBInstance.GetProfileById(state.ProfileId);

                    if (profile == null)
                    {
                        return;
                    }

                    var friends = profile.Friends ?? new List <long>();

                    SendToClient(ref state, $@"\bdy\{friends.Count + 1}\list\{string.Join(",",friends.Concat(new long[] { profile.Id }))}\final\".ToAssciiBytes());

                    // var loopbackIp = (uint)IPAddress.NetworkToHostOrder((int)IPAddress.Loopback.Address);
                    // var loopbackIp = ReverseEndian32((uint)IPAddress.NetworkToHostOrder((int)IPAddress.Parse("192.168.159.128").Address));
                    // var port = ReverseEndian16(27900);

                    state.Status       = keyValues.GetOrDefault("status") ?? state.Status ?? "0";
                    state.StatusString = keyValues.GetOrDefault("statstring") ?? state.StatusString ?? "Offline";
                    state.LocString    = keyValues.GetOrDefault("locstring") ?? state.LocString ?? "-1";

                    //SendToClient(ref state, $@"\bm\100\f\{profile.Id}\msg\|s|{1}|ss|DXP{"|ls|"}{-1}|ip|{loopbackIp}|p|{port}|qm|{0}\final\".ToAssciiBytes());

                    var statusResult = $@"\bm\100\f\{profile.Id}\msg\|s|{state.Status}{state.GetLSParameter()}|ss|{state.StatusString}\final\".ToAssciiBytes();
                    SendToClient(ref state, statusResult);

                    for (int i = 0; i < friends.Count; i++)
                    {
                        var friendId = friends[i];

                        if (Users.TryGetValue(friendId, out LoginSocketState friendState))
                        {
                            SendToClient(ref friendState, statusResult);
                            //SendToClient(ref state, $@"\bm\100\f\{friendId}\msg\|s|{1}|ss|DXP|ls|-1\final\".ToAssciiBytes());
                            SendToClient(ref state, $@"\bm\100\f\{friendId}\msg\|s|{friendState.Status}{friendState.GetLSParameter()}|ss|{friendState.StatusString}\final\".ToAssciiBytes());
                        }
                        else
                        {
                            SendToClient(ref state, $@"\bm\100\f\{friendId}\msg\|s|0|ss|Offline\final\".ToAssciiBytes());
                        }
                    }

                    // \status\1\sesskey\17562\statstring\DXP\locstring\-1

                    // userid\200000003\profileid\100000003\uniquenick\Bambochuk

                    //sendBuddies()
                    // SendToClient(ref state, $@"\bdy\1\list\100000003\final\".ToAssciiBytes());
                    //SendToClient(ref state, $@"\bdy\0\list\\final\".ToAssciiBytes());

                    // TODO: sendAddRequests();

                    //sendStatusUpdateToBuddies(this);

                    // send self status
                    // |s|%d|ss|%s%s%s|ip|%d|p|%d|qm|%d

                    /*
                     * c->status,
                     *      c->statusstr,
                     *      c->locstr[0] != 0 ? "|ls|" : "",
                     *      c->locstr,
                     *      reverse_endian32(c->ip),
                     *      reverse_endian16(c->port),
                     *      c->quietflags
                     */

                    // SendToClient(ref state, $@"\bm\100\f\100000003\msg\|s|{2}|ss|DXP{"|ls|"}{-1}|ip|{(uint)IPAddress.NetworkToHostOrder((int)IPAddress.Loopback.Address)}|p|{ReverseEndian16(6500)}|qm|{0}\final\".ToAssciiBytes());
                    //SendToClient(ref state, $@"\bm\100\f\100000002\msg\|s|0|ss|Offline\final\".ToAssciiBytes());
                    //SendToClient(ref state, $@"\bm\100\f\100000001\msg\|s|{1}|ss|DXP{"|ls|"}{-1}|ip|{(uint)IPAddress.NetworkToHostOrder((int)remoteEndPoint.Address.Address)}|p|{ReverseEndian16(6500)}|qm|{0}\final\".ToAssciiBytes());



                    // send friend status



                    break;

                case "logout":
                    Users.TryRemove(state.ProfileId, out LoginSocketState removingState);
                    LoginServerMessages.Logout(ref state, keyValues);
                    break;

                case "registernick":
                    SendToClient(ref state, DataFunctions.StringToBytes(string.Format(@"\rn\{0}\id\{1}\final\", keyValues["uniquenick"], keyValues["id"])));
                    break;

                case "ka":
                    SendToClient(ref state, $@"\ka\\final\".ToAssciiBytes());
                    break;

                default:
                    break;
                }
            }
        }