Example #1
0
 /*
  * public GameDataHandler GetHandler(ushort id)
  * {
  *  GameDataHandler handler = null;
  *  if (_gameHandlers.ContainsKey(id))
  *  {
  *      try
  *      {
  *          _gameHandlers.TryGetValue(id, out handler);
  *      }
  *      catch { handler = null; }
  *  }
  *  return handler;
  * }
  */
 public PacketHandler FindExternal(Core.Networking.InPacket inPacket)
 {
     if (_externalPacketList.ContainsKey(inPacket.Id))
     {
         return((Networking.PacketHandler)_externalPacketList[inPacket.Id]);
     }
     else
     {
         Log.Error("UNKNOWN PACKET :: " + inPacket.fullPacket.Remove(inPacket.fullPacket.Length - 1));
     }
     return(null);
 }
Example #2
0
 public PacketHandler FindInternal(Core.Networking.InPacket inPacket)
 {
     if (_internalPacketList.ContainsKey(inPacket.Id))
     {
         return((Networking.PacketHandler)_internalPacketList[inPacket.Id]);
     }
     else
     {
         Log.Instance.WriteBoth("UNKNOWN PACKET :: " + inPacket.fullPacket.Remove(inPacket.fullPacket.Length - 1));
     }
     return(null);
 }
Example #3
0
        public void Handle(Core.Networking.InPacket inPacket)
        {
            this.inPacket = inPacket;

            object attachment = inPacket.Attachment;

            if (attachment is Entities.User)
            {
                this.Process((Entities.User)inPacket.Attachment);
            }
            else if (attachment is Entities.Server)
            {
                this.Process((Entities.Server)inPacket.Attachment);
            }
            else
            {
                Log.Warning("Unknown packet attachment!");
            }
        }
        public void Handle(Core.Networking.InPacket inPacket)
        {
            this.InPacket = inPacket;

            object attachment = inPacket.Attachment;

            if (attachment is Entities.User)
            {
                this.Process((Entities.User)inPacket.Attachment);
            }
            else if (attachment is ServerClient)
            {
                this.Process((ServerClient)inPacket.Attachment);
            }
            else
            {
                Log.Instance.WriteLine("Unknown packet attachment!");
            }
        }
Example #5
0
        public void Handle(Core.Networking.InPacket inPacket)
        {
            this.InPacket = inPacket;

            object attachment = inPacket.Attachment;

            if (attachment is User)
            {
                this.Process((User)inPacket.Attachment);
            }
            else if (attachment is AuthenticationClient)
            {
                this.Process((AuthenticationClient)inPacket.Attachment);
            }
            else
            {
                Log.Warning("Unknown packet attachment! Who is this packet for?");
            }
        }
        private void OnDataReceived(IAsyncResult iAr)
        {
            try
            {
                int bytesReceived = socket.EndReceive(iAr);

                if (bytesReceived > 0)
                {
                    byte[] packetBuffer = new byte[bytesReceived];

                    // Decrypt the bytes with the xOrKey.
                    for (int i = 0; i < bytesReceived; i++)
                    {
                        packetBuffer[i] = (byte)(this.buffer[i] ^ Core.Networking.Constants.xOrKeyInternalSend);
                    }

                    int oldLength = cacheBuffer.Length;
                    Array.Resize(ref cacheBuffer, oldLength + bytesReceived);
                    Array.Copy(packetBuffer, 0, cacheBuffer, oldLength, packetBuffer.Length);

                    int startIndex = 0;                                  // Determs whre the bytes should split
                    for (int i = 0; i < cacheBuffer.Length; i++)
                    {                                                    // loop trough our cached buffer.
                        if (cacheBuffer[i] == 0x0A)
                        {                                                // Found a complete packet
                            byte[] newPacket = new byte[i - startIndex]; // determ the new packet size.
                            for (int j = 0; j < (i - startIndex); j++)
                            {
                                newPacket[j] = cacheBuffer[startIndex + j]; // copy the buffer to the buffer of the new packet.
                            }
                            packetCount++;

                            // Handle the packet instantly.
                            Core.Networking.InPacket inPacket = new Core.Networking.InPacket(newPacket, this);
                            if (inPacket.Id > 0)
                            {
                                Networking.PacketHandler pHandler = Managers.PacketManager.Instance.FindInternal(inPacket);
                                if (pHandler != null)
                                {
                                    // try {
                                    pHandler.Handle(inPacket);
                                    //} catch { /*Disconnect(); }
                                }
                            }

                            startIndex = i + 1;
                        }
                    }

                    if (startIndex > 0)
                    {
                        byte[] fullCopy = cacheBuffer;
                        Array.Resize(ref cacheBuffer, (cacheBuffer.Length - startIndex));
                        for (int i = 0; i < (cacheBuffer.Length - startIndex); i++)
                        {
                            cacheBuffer[i] = fullCopy[startIndex + i];
                        }
                        fullCopy = null;
                    }

                    socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
                }
                else
                {
                    Disconnect();
                }
            }
            catch
            {
                Disconnect();
            }
        }
Example #7
0
        private void OnDataReceived(IAsyncResult iAr)
        {
            try
            {
                int bytesReceived = _socket.EndReceive(iAr);

                if (bytesReceived > 0)
                {
                    byte[] packetBuffer = new byte[bytesReceived];

                    // Decrypt the bytes with the xOrKey.
                    for (int i = 0; i < bytesReceived; i++)
                    {
                        packetBuffer[i] = (byte)(_buffer[i] ^ Core.Networking.Constants.xOrKeyClientRecieve);
                    }

                    int oldLength = _cacheBuffer.Length;
                    Array.Resize(ref _cacheBuffer, oldLength + bytesReceived);
                    Array.Copy(packetBuffer, 0, _cacheBuffer, oldLength, packetBuffer.Length);

                    int startIndex = 0;                                  // Determs where the bytes should split
                    for (int i = 0; i < _cacheBuffer.Length; i++)
                    {                                                    // loop trough our cached buffer.
                        if (_cacheBuffer[i] == 0x0A)
                        {                                                // Found a complete packet
                            byte[] newPacket = new byte[i - startIndex]; // determ the new packet size.
                            for (int j = 0; j < (i - startIndex); j++)
                            {
                                newPacket[j] = _cacheBuffer[startIndex + j]; // copy the buffer to the buffer of the new packet.
                            }
                            _packetCount++;
                            // Instant handeling
                            Core.Networking.InPacket inPacket = new Core.Networking.InPacket(newPacket, this);
                            if (inPacket != null)
                            {
                                if (inPacket.Id > 0)
                                {
                                    Networking.PacketHandler pHandler = Managers.PacketManager.Instance.FindExternal(inPacket);
                                    if (pHandler != null)
                                    {
                                        try
                                        {
                                            pHandler.Handle(inPacket);
                                        }
                                        catch (Exception e) { Log.Error(e.ToString()); }
                                    }
                                }
                            }
                            // Increase start index.
                            startIndex = i + 1;
                        }
                    }

                    if (startIndex > 0)
                    {
                        byte[] fullCopy = _cacheBuffer;
                        Array.Resize(ref _cacheBuffer, (_cacheBuffer.Length - startIndex));
                        for (int i = 0; i < (_cacheBuffer.Length - startIndex); i++)
                        {
                            _cacheBuffer[i] = fullCopy[startIndex + i];
                        }
                        fullCopy = null;
                    }
                    _socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
                }
                else
                {
                    Disconnect();
                }
            }
            catch
            {
                Disconnect();
            }
        }
        public void Process(Entities.User u, Core.Networking.InPacket packet)
        {
            string[] blocks = packet.Blocks;
            ushort   subId  = 0;

            try {
                ushort.TryParse(blocks[3], out subId);
            }catch { subId = 0; }

            if (subId > 0 && Enum.IsDefined(typeof(Enums.GameSubs), subId))
            {
                this.type = (Enums.GameSubs)subId;

                lock (handleLock) {
                    this.packet = packet;
                    mapData     = false;
                    updateLobby = false;
                    selfTarget  = false;
                    respond     = false;

                    this.blocks = new string[blocks.Length - 4];
                    Array.Copy(blocks, 4, this.blocks, 0, this.blocks.Length);

                    this.Room = u.Room;
                    Entities.Player p = null;
                    selfTarget = false;

                    try {
                        this.Room.Players.TryGetValue(u.RoomSlot, out p);
                    } catch { p = null; }

                    if (p != null)
                    {
                        this.Player = p;
                        roomSlot    = p.Id;

                        try {
                            Handle();
                        } catch { respond = false; }

                        if (respond)
                        {
                            string[] packetData;
                            if (errorCode == 1)
                            {
                                packetData    = new string[this.blocks.Length + 5];
                                packetData[0] = errorCode.ToString();
                                packetData[1] = roomSlot.ToString();
                                packetData[2] = Room.ID.ToString();
                                packetData[3] = blocks[2];                 // 2 - 0
                                packetData[4] = ((ushort)type).ToString(); // Type

                                Array.Copy(this.blocks, 0, packetData, 5, this.blocks.Length);
                            }
                            else
                            {
                                packetData = new string[] { errorCode.ToString() };
                            }

                            // Generate packet buffer //
                            byte[] buffer = new Packets.GameData(packetData).BuildEncrypted();
                            if (errorCode > 1 || selfTarget)
                            {
                                u.Send(buffer);
                            }
                            else
                            {
                                Room.Send(buffer);
                            }

                            if (mapData)
                            {
                                u.Send(new Packets.MapData(Room));
                            }

                            if (updateLobby) // Send a update to the lobby :)
                            {
                                byte roomPage   = (byte)Math.Floor((decimal)(Room.ID / 8));
                                var  targetList = Managers.ChannelManager.Instance.Get(Room.Channel).Users.Select(n => n.Value).Where(n => n.RoomListPage == roomPage && n.Room == null);
                                if (targetList.Count() > 0)
                                {
                                    byte[] outBuffer = new Packets.RoomUpdate(Room, true).BuildEncrypted();
                                    foreach (Entities.User usr in targetList)
                                    {
                                        usr.Send(outBuffer);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                u.Disconnect(); // Wrong id?
            }
        }