Exemple #1
0
        private void HandleReceived_Connection(NecConnection connection, NecPacket packet)
        {
            if (!_connectionHandlers.ContainsKey(packet.id))
            {
                _Logger.LogUnknownIncomingPacket(connection, packet, _serverType);
                return;
            }

            IConnectionHandler connectionHandler = _connectionHandlers[packet.id];

            if (connectionHandler.expectedSize != NO_EXPECTED_SIZE && packet.data.Size < connectionHandler.expectedSize)
            {
                _Logger.Error(connection,
                              $"[{_serverType}] Ignoring Packed (Id:{packet.id}) is smaller ({packet.data.Size}) than expected ({connectionHandler.expectedSize})");
                return;
            }

            _Logger.LogIncomingPacket(connection, packet, _serverType);
            packet.data.SetPositionStart();
            try
            {
                connectionHandler.Handle(connection, packet);
            }
            catch (Exception ex)
            {
                _Logger.Exception(connection, ex);
            }
        }
        public override void Handle(NecConnection connection, NecPacket packet)
        {
            int accountId = packet.Data.ReadInt32();
            int unknown   = packet.Data.ReadInt32();

            byte[] unknown1 = packet.Data.ReadBytes(20); // Suspect SessionId

            // TODO replace with sessionId
            NecClient client = Server.Clients.GetByAccountId(accountId);

            if (client == null)
            {
                Logger.Error(connection, $"AccountId: {accountId} has no active session");
                connection.Socket.Close();
                return;
            }

            client.AreaConnection = connection;
            connection.Client     = client;

            IBuffer res = BufferProvider.Provide();

            res.WriteInt32(0); //  Error
            Router.Send(connection, (ushort)AreaPacketId.recv_base_enter_r, res);
        }
Exemple #3
0
        protected override void HandleConnected(ITcpSocket socket)
        {
            NecConnection connection = new NecConnection(socket, new PacketFactory(_setting), _serverType);

            lock (_lock)
            {
                _connections.Add(socket, connection);
                _Logger.Debug($"[{_serverType}] Clients Count: {_connections.Count}");
            }

            Action <NecConnection> onClientConnected = clientConnected;

            if (onClientConnected != null)
            {
                try
                {
                    onClientConnected.Invoke(connection);
                }
                catch (Exception ex)
                {
                    _Logger.Exception(connection, ex);
                }
            }

            _Logger.Info(connection, $"[{_serverType}] Client connected");
        }
Exemple #4
0
        private void AreaClientDisconnected(NecConnection connection)
        {
            NecClient client = connection.Client;

            if (client == null)
            {
                return;
            }

            Map map = client.Map;

            if (map != null)
            {
                map.Leave(client);
            }

            Union union = client.Union;

            if (union != null)
            {
                union.Leave(client);
            }

            //Remove the client from the list of server clients on disconnect
            this.Clients.Remove(client);
        }
Exemple #5
0
        public override void Handle(NecConnection connection, NecPacket packet)
        {
            string accountName = packet.data.ReadCString();
            string password    = packet.data.ReadCString();
            string macAddress  = packet.data.ReadCString();
            int    unknown     = packet.data.ReadInt16();

            _Logger.Info($"Account:{accountName} Password:{password} Unknown:{unknown}");

            Account account = database.SelectAccountByName(accountName);

            if (account == null)
            {
                if (settings.requireRegistration)
                {
                    _Logger.Error(connection, $"AccountName: {accountName} doesn't exist");
                    SendResponse(connection, null);
                    connection.socket.Close();
                    return;
                }

                string bCryptHash = BCrypt.Net.BCrypt.HashPassword(password, NecSetting.B_CRYPT_WORK_FACTOR);
                account = database.CreateAccount(accountName, accountName, bCryptHash);
            }

            if (!BCrypt.Net.BCrypt.Verify(password, account.hash))
            {
                _Logger.Error(connection, $"Invalid password for AccountName: {accountName}");
                SendResponse(connection, null);
                connection.socket.Close();
                return;
            }

            NecClient client = new NecClient();

            client.account        = account;
            client.authConnection = connection;
            connection.client     = client;
            client.UpdateIdentity();
            server.clients.Add(client);

            SendResponse(connection, account);

            //if client did not send a hardbeat within 75 seconds, something went wrong. remove the client. Thats enough time for 4 heartbeats.
            Task.Delay(TimeSpan.FromSeconds(75)).ContinueWith
                (t1 =>
            {
                if (client != null)
                {
                    if (client.heartBeat == 0)
                    {
                        server.clients.Remove(client);
                        _Logger.Error($"Initial heartbeat missed. disconnecting client. Server.clientCount is now {server.clients.GetCount()}");
                    }
                }
            }
                );
        }
Exemple #6
0
        public void Info(NecConnection connection, string message)
        {
            NecClient client = connection.client;

            if (client != null)
            {
                Info(client, message);
                return;
            }

            Info($"{connection.identity} {message}");
        }
Exemple #7
0
        public void Error(NecConnection connection, string message)
        {
            NecClient client = connection.Client;

            if (client != null)
            {
                Error(client, message);
                return;
            }

            Error($"{connection.Identity} {message}");
        }
        private void SendResponse(NecConnection connection, NecClient client)
        {
            List <Soul> souls = database.SelectSoulsByAccountId(client.account.id);

            if (souls.Count <= 0)
            {
                IBuffer resq = BufferProvider.Provide();
                resq.WriteInt32(0); //  Error
                for (int i = 0; i < 8; i++)
                {
                    resq.WriteByte(1);
                    resq.WriteFixedString(string.Empty, 49); // Soul Name
                    resq.WriteByte(client.soul.level);       // Soul Level
                    resq.WriteByte(0);                       // bool - if use value 1, can't join in msg server character list
                }

                resq.WriteInt32(0);
                resq.WriteByte(0); //bool
                resq.WriteByte(0);
                router.Send(client, (ushort)MsgPacketId.recv_base_login_r, resq, ServerType.Msg);
                return;
            }

            IBuffer res = BufferProvider.Provide();

            res.WriteInt32(0); //  Error
            for (int i = 0; i < SOUL_COUNT; i++)
            {
                if (souls.Count > i)
                {
                    Soul soul = souls[0];
                    res.WriteByte(1);
                    res.WriteFixedString(soul.name, 49);
                    res.WriteByte(soul.level);
                    res.WriteByte(1); // bool - if use value 1, can't join in msg server character list
                }
                else
                {
                    res.WriteByte(0);
                    res.WriteFixedString(string.Empty, 49); // Soul Name
                    res.WriteByte(0);                       // Soul Level
                    res.WriteByte(0);                       // bool - if use value 1, can't join in msg server character list
                }
            }

            res.WriteInt32(1);
            res.WriteByte(1); // bool
            res.WriteByte(1);

            router.Send(client, (ushort)MsgPacketId.recv_base_login_r, res, ServerType.Msg);
        }
        public override void Handle(NecConnection connection, NecPacket packet)
        {
            uint major = packet.data.ReadUInt32();
            uint minor = packet.data.ReadUInt32();

            _Logger.Info($"{major} - {minor}");

            IBuffer res = BufferProvider.Provide();

            res.WriteInt32(0);
            res.WriteUInt32(major);
            res.WriteUInt32(minor);

            router.Send(connection, (ushort)AuthPacketId.recv_base_check_version_r, res);
        }
Exemple #10
0
        private void SendResponse(NecConnection connection, Account account)
        {
            IBuffer res = BufferProvider.Provide();

            if (account == null)
            {
                res.WriteInt32(1); // Error (0 = OK,  1 = ID or Pw to long)
                res.WriteInt32(0); // Account Id
            }
            else
            {
                res.WriteInt32(0); // Error (0 = OK,  1 = ID or Pw to long)
                res.WriteInt32(account.Id);
            }

            Router.Send(connection, (ushort)AuthPacketId.recv_base_authenticate_r, res);
        }
Exemple #11
0
        public void LogOutgoingPacket(NecConnection connection, NecPacket packet, ServerType serverType)
        {
            NecClient client = connection.Client;

            if (client != null)
            {
                LogOutgoingPacket(client, packet, serverType);
                return;
            }

            if (!_setting.LogIncomingPackets)
            {
                return;
            }

            NecLogPacket logPacket = new NecLogPacket(connection.Identity, packet, NecLogType.PacketOut, serverType);

            WritePacket(logPacket);
        }
Exemple #12
0
        public void Exception(NecConnection connection, Exception exception)
        {
            NecClient client = connection.Client;

            if (client != null)
            {
                Exception(client, exception);
                return;
            }

            if (exception == null)
            {
                Write(LogLevel.Error, $"{connection.Identity} Exception was null.", null);
            }
            else
            {
                Write(LogLevel.Error, $"{connection.Identity} {exception}", exception);
            }
        }
Exemple #13
0
        public override void Handle(NecConnection connection, NecPacket packet)
        {
            string accountName = packet.Data.ReadCString();
            string password    = packet.Data.ReadCString();
            string macAddress  = packet.Data.ReadCString();
            int    unknown     = packet.Data.ReadInt16();

            Logger.Info($"Account:{accountName} Password:{password} Unknown:{unknown}");

            Account account = Database.SelectAccountByName(accountName);

            if (account == null)
            {
                if (Settings.RequireRegistration)
                {
                    Logger.Error(connection, $"AccountName: {accountName} doesn't exist");
                    SendResponse(connection, null);
                    connection.Socket.Close();
                    return;
                }

                string bCryptHash = BCrypt.Net.BCrypt.HashPassword(password, NecSetting.BCryptWorkFactor);
                account = Database.CreateAccount(accountName, accountName, bCryptHash);
            }

            if (!BCrypt.Net.BCrypt.Verify(password, account.Hash))
            {
                Logger.Error(connection, $"Invalid password for AccountName: {accountName}");
                SendResponse(connection, null);
                connection.Socket.Close();
                return;
            }

            NecClient client = new NecClient();

            client.Account        = account;
            client.AuthConnection = connection;
            connection.Client     = client;
            client.UpdateIdentity();
            Server.Clients.Add(client);

            SendResponse(connection, account);
        }
Exemple #14
0
        public void LogUnknownIncomingPacket(NecConnection connection, NecPacket packet, ServerType serverType)
        {
            NecClient client = connection.client;

            if (client != null)
            {
                LogUnknownIncomingPacket(client, packet, serverType);
                return;
            }

            if (!_setting.logIncomingPackets)
            {
                return;
            }

            NecLogPacket logPacket =
                new NecLogPacket(connection.identity, packet, NecLogType.PacketUnhandled, serverType);

            WritePacket(logPacket);
        }
        public override void Handle(NecConnection connection, NecPacket packet)
        {
            int accountId = packet.data.ReadInt32();

            byte[] unknown = packet.data.ReadBytes(20); // Suspect SessionId
            // TODO replace with sessionId
            NecClient client = server.clients.GetByAccountId(accountId);

            if (client == null)
            {
                _Logger.Error(connection, $"AccountId: {accountId} has no active session");
                // TODO refactor null check
                SendResponse(connection, client);
                connection.socket.Close();
                return;
            }

            client.msgConnection = connection;
            connection.client    = client;
            SendResponse(connection, client);
        }
Exemple #16
0
        private void AreaClientDisconnected(NecConnection connection)
        {
            NecClient client = connection.client;

            if (client == null)
            {
                return;
            }
            //Try to update the character stats.
            if (!database.UpdateCharacter(client.character))
            {
                _Logger.Error("Could not update the database with character details before disconnect");
            }
            if (!database.UpdateSoul(client.soul))
            {
                _Logger.Error("Could not update the database with soul details before disconnect");
            }
            clients.Remove(client);

            //I disconnected while my dead body was being carried around by another player
            if (client.character.hasDied)
            {
                DeadBody deadBody = instances.GetInstance(client.character.deadBodyInstanceId) as DeadBody;
                if (deadBody.salvagerId != 0)
                {
                    NecClient mySalvager = clients.GetByCharacterInstanceId(deadBody.salvagerId);
                    if (mySalvager != null)
                    {
                        deadBody.x               = mySalvager.character.x;
                        deadBody.y               = mySalvager.character.y;
                        deadBody.z               = mySalvager.character.z;
                        deadBody.mapId           = mySalvager.character.mapId;
                        deadBody.connectionState = 0;
                        mySalvager.bodyCollection.Remove(deadBody.instanceId);

                        mySalvager.map.deadBodies.Add(deadBody.instanceId, deadBody);
                        RecvDataNotifyCharaBodyData cBodyData = new RecvDataNotifyCharaBodyData(deadBody);
                        if (client.map.id.ToString()[0] != "1"[0]) //Don't Render dead bodies in town.  Town map ids all start with 1
                        {
                            router.Send(mySalvager.map, cBodyData.ToPacket(), client);
                        }

                        //must occur after the charaBody notify.
                        RecvCharaBodySalvageEnd recvCharaBodySalvageEnd = new RecvCharaBodySalvageEnd(deadBody.instanceId, 5);
                        router.Send(mySalvager, recvCharaBodySalvageEnd.ToPacket());
                    }
                }
            }

            //while i was dead and being carried around, the player carrying me disconnected
            foreach (NecClient collectedBody in client.bodyCollection.Values)
            {
                DeadBody deadBody = instances.GetInstance(collectedBody.character.deadBodyInstanceId) as DeadBody;

                RecvCharaBodySelfSalvageEnd recvCharaBodySelfSalvageEnd = new RecvCharaBodySelfSalvageEnd(3);
                router.Send(collectedBody, recvCharaBodySelfSalvageEnd.ToPacket());


                deadBody.x = client.character.x;
                deadBody.y = client.character.y;
                deadBody.z = client.character.z;
                collectedBody.character.x = client.character.x;
                collectedBody.character.y = client.character.y;
                collectedBody.character.z = client.character.z;
                //ToDo  add Town checking.  if map.ID.toString()[0]==1 skip deadbody rendering
                deadBody.mapId = client.character.mapId;

                client.map.deadBodies.Add(deadBody.instanceId, deadBody);
                RecvDataNotifyCharaBodyData cBodyData = new RecvDataNotifyCharaBodyData(deadBody);
                if (client.map.id.ToString()[0] != "1"[0]) //Don't Render dead bodies in town.  Town map ids all start with 1
                {
                    router.Send(client.map, cBodyData.ToPacket());
                }

                //send your soul to all the other souls runnin around
                RecvDataNotifyCharaData cData = new RecvDataNotifyCharaData(collectedBody.character, collectedBody.soul.name);
                foreach (NecClient soulStateClient in client.map.clientLookup.GetAll())
                {
                    if (soulStateClient.character.state == CharacterState.SoulForm)
                    {
                        router.Send(soulStateClient, cData.ToPacket());
                    }
                }
            }

            Map map = client.map;

            //If i was dead, toggle my deadBody to a Rucksack
            if (map != null)
            {
                if (map.deadBodies.ContainsKey(client.character.deadBodyInstanceId))
                {
                    map.deadBodies.TryGetValue(client.character.deadBodyInstanceId, out DeadBody deadBody);
                    deadBody.connectionState = 0;
                    RecvCharaBodyNotifySpirit recvCharaBodyNotifySpirit = new RecvCharaBodyNotifySpirit(client.character.deadBodyInstanceId, (byte)RecvCharaBodyNotifySpirit.ValidSpirit.DisconnectedClient);
                    router.Send(map, recvCharaBodyNotifySpirit.ToPacket());

                    Task.Delay(TimeSpan.FromSeconds(600)).ContinueWith
                        (t1 =>
                    {
                        if (map.deadBodies.ContainsKey(client.character.deadBodyInstanceId))
                        {
                            RecvObjectDisappearNotify recvObjectDisappearNotify = new RecvObjectDisappearNotify(client.character.deadBodyInstanceId);
                            router.Send(client.map, recvObjectDisappearNotify.ToPacket(), client);
                            map.deadBodies.Remove(client.character.deadBodyInstanceId);
                        }
                    }
                        );
                }
            }

            if (map != null)
            {
                map.Leave(client);
            }

            Union union = client.union;

            if (union != null)
            {
                union.Leave(client);
            }

            Character character = client.character;

            if (character != null)
            {
                instances.FreeInstance(character);
                character.characterActive = false;
            }
        }
Exemple #17
0
 private void MsgClientDisconnected(NecConnection client)
 {
 }
Exemple #18
0
 private void AuthClientDisconnected(NecConnection client)
 {
 }
Exemple #19
0
 /// <summary>
 /// Send a packet to a connection.
 /// </summary>
 public void Send(NecConnection connection, NecPacket packet)
 {
     connection.Send(packet);
 }
Exemple #20
0
        /// <summary>
        /// Send a packet to a connection.
        /// </summary>
        public void Send(NecConnection connection, ushort id, IBuffer data)
        {
            NecPacket packet = new NecPacket(id, data, connection.ServerType);

            Send(connection, packet);
        }
 public abstract void Handle(NecConnection client, NecPacket packet);