Пример #1
0
        public virtual void ProcessPacketStream(SocketStateObject state, PacketIdentifier packetId, DataStream packetStream)
        {
            if (Handler.Contains(packetId) || CompleteHandler.Contains(packetId))
            {
                GamePacket packet;
                if (!PacketsRegistry.TryGetPacket(packetId, out packet))
                {
                    packet = new BasePacket();
                }

                packet.Deserialize(packetStream);
                var args = Handler.HandlePacket(packetId, packet);
                if (args == null || !args.Cancel)
                {
                    if (args == null)
                    {
                        Send(state, packetId.PacketId, packetStream.Reset());
                    }
                    else
                    {
                        Send(state, packetId, packet);
                    }
                    CompleteHandler.HandlePacket(packetId, packet);
                }
            }
            else
            {
                Send(state, packetId.PacketId, packetStream);
            }
        }
Пример #2
0
        public static void Parse(PacketsRegistry packetsRegistry, string[] protocolLines)
        {
            var structures = new List <string>();

            var sb = new StringBuilder();

            foreach (var line in protocolLines)
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    var newStruct = sb.ToString();
                    if (!string.IsNullOrWhiteSpace(newStruct))
                    {
                        structures.Add(newStruct);
                    }

                    sb.Clear();
                }
                else
                {
                    sb.AppendLine(line);
                }
            }
            var end = sb.ToString();

            if (!string.IsNullOrWhiteSpace(end))
            {
                structures.Add(end);
            }

            foreach (var structure in structures)
            {
                ParseStructure(packetsRegistry, structure);
            }
        }
Пример #3
0
        private void InitializePacket(uint opcode, PacketType packetType, DataStream ds)
        {
            if (OnPacketReceive != null)
            {
                OnPacketReceive(this, new PacketReceiveEventArgs(new PacketIdentifier(opcode, packetType), ds));
                ds.Reset();
            }

            Type type = null;

            if (packetType == PacketType.ServerPacket)
            {
                type = PacketsRegistry.GetPacket(opcode);
            }
            if (packetType == PacketType.ServerContainer)
            {
                type = PacketsRegistry.GetContainer(opcode);
            }

            if (type == null)
            {
                return;
            }
            GamePacket packet;

            try
            {
                packet = DataStreamSerializer.Deserialize(ds, type) as GamePacket;
            }
            catch { return; }
            packet.HandleData(Data);
            packetHandler.Handle(packet);
        }
Пример #4
0
        public void Init()
        {
            PacketsRegistry.Initialize();

            _tcpClient = new TcpClient(new IPEndPoint(IPAddress.Any, 0));

            _isInitialized = true;
            Logger.Info("Network is initialized");
        }
Пример #5
0
        public void Init()
        {
            PacketsRegistry.Initialize();

            _tcpListener = new TcpListener(new IPEndPoint(IPAddress.Parse(_gridServer.Settings.BindAddress), (int)_gridServer.Settings.BindPort));
            _tcpListener.AllowNatTraversal(true);

            Logger.Info("Network is initialized");
        }
Пример #6
0
        public void SendPacket(IPacket packet)
        {
            var writer = new BinaryWriter(_netStream);

            using (var ms = new PacketBuffer(new MemoryStream())) {
                var packetId = PacketsRegistry.GetPacketIdByType(packet.GetType());
                packet.Write(ms);

                var buffer = ((MemoryStream)ms.BaseStream).ToArray();
                writer.Write(packetId);
                writer.Write(buffer.Length);
                writer.Write(buffer);
                _netStream.Flush();
            }
        }
Пример #7
0
        public OOGHost(GameServer gameServer)
        {
            packetHandler = new PacketHandler(this);
            pluginManager = new PluginManager(this);

            // Блок с клиентскими данными
            Data                        = new ConnectionData(this);
            ConnectionStatus            = Data.IncludeDataBlock <ConnectionStatus>();
            ConnectionStatus.GameServer = gameServer;

            // Оболочка для Socket с шифрацией и дешифрацией трафика
            Connection = new Connection(this, Data);

            PacketsRegistry.RegisterPackets();
        }
Пример #8
0
        public static void ParseStructure(PacketsRegistry packetsRegistry, string structure)
        {
            var reader = new StringReader(structure.Trim());

            var head = reader.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var type = head[0];
            var id   = head[1];

            if (id.EndsWith(":"))
            {
                id = id.Substring(0, id.Length - 1);
            }

            var declaration = new StructureDeclaration();

            while (true)
            {
                var line = reader.ReadLine();
                if (string.IsNullOrWhiteSpace(line))
                {
                    break;
                }

                var field = Ext.ParseArgs(line);

                var fieldType = field[0];
                var fieldName = field[1];

                if (fieldName.EndsWith(";"))
                {
                    fieldName = fieldName.Substring(0, fieldName.Length - 1);
                }

                declaration.Fields.Add(new KeyValuePair <string, string>(fieldName, fieldType));
            }

            switch (type)
            {
            case "packet": packetsRegistry.Register(id, declaration); break;

            case "struct":
            case "type": packetsRegistry.Serializer.Register(id, declaration); break;
            }
        }
Пример #9
0
        public virtual void Start(IPEndPoint endPoint)
        {
            lock (startLock)
            {
                if (Started)
                {
                    return;
                }
                LocalEndPoint = endPoint;
                BaseSocket    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                BaseSocket.Bind(endPoint);
                BaseSocket.Listen(100);

                if (PacketsRegistry == null)
                {
                    PacketsRegistry = PacketsRegistry.Default;
                }

                Started = true;
                BeginAccept(BaseSocket);
            }
        }
Пример #10
0
        public IPacket ReadPacket()
        {
            var netReader = new BinaryReader(_netStream);
            var packetId  = netReader.ReadInt32();
            var packetLen = netReader.ReadInt32();

            var packetBuffer = netReader.ReadBytes(packetLen);
            var tempBuffer   = new MemoryStream(packetBuffer);

            using (var reader = new PacketBuffer(tempBuffer)) {
                var initPos = tempBuffer.Position;
                if (packetId == -1)
                {
                    Logger.Debug($"Invalid packet id received {packetId}");
                    return(null);
                }

                var packetType = PacketsRegistry.GetPacketById(packetId);
                if (packetType == null)
                {
                    Logger.Debug($"Unknown packet id received {packetId}");
                    return(null);
                }

                var packet = (IPacket)Activator.CreateInstance(packetType);
                packet.Read(reader);

                if (tempBuffer.Position < initPos + packetLen)
                {
                    Logger.Error($"Packet (id: {packetId}) not fully readed ({tempBuffer.Position} < {initPos + packetLen})");
                    return(null);
                }

                return(packet);
            }
        }
Пример #11
0
 public void Init()
 {
     PacketsRegistry.Initialize();
     _isInitialized = true;
     Logger.Info("Network is initialized");
 }
Пример #12
0
 public void Init()
 {
     PacketsRegistry.Initialize();
     PostInit();
 }