Пример #1
0
        public Session(
            IPool <DataStream> dataStreamPool = null,
            PacketReader packetReader         = null,
            PacketWriter packetWriter         = null,
            PacketPolicy packetPolicy         = null,
            ProtoListTable baseProto          = null)
        {
            if (dataStreamPool == null)
            {
                dataStreamPool = Link.Pools.DataStreamPool.Instance;
            }
            if (packetReader == null)
            {
                packetReader = new PacketReader(dataStreamPool.Take(), dataStreamPool.Take());
            }
            if (packetWriter == null)
            {
                packetWriter = new PacketWriter(dataStreamPool.Take(), dataStreamPool.Take());
            }
            if (packetPolicy == null)
            {
                packetPolicy = PacketPolicy.AllAcceptPolicy;
            }

            DataStreamPool = dataStreamPool;
            PacketReader   = packetReader;
            PacketWriter   = packetWriter;
            PacketPolicy   = packetPolicy;

            if (baseProto == null)
            {
                Proto = new ProtoListTable();
            }
            else
            {
                Proto = baseProto;
            }

            Handler       = new PacketHandlerTable();
            Handler.Proto = Proto;
            Modules       = new ModulesManager(this);

            connectionInputRoute  = new RouteChain();
            connectionOutputRoute = new RouteOutputHandler(SendNext);

            InputChain  = new RouteSession();
            OutputChain = new RouteSession();

            InputChain.Handler = OutputChain.Handler = Handler;
            InputChain.Session = OutputChain.Session = this;

            connectionInputRoute.Next = InputChain;
            OutputChain.Next          = connectionOutputRoute;

            InputChain.IsInput  = true;
            OutputChain.IsInput = false;

            InputChain.Redirect  = OutputChain;
            OutputChain.Redirect = InputChain;
        }
Пример #2
0
 public ClientSession(IPassiveConnectionFactory factory,
                      IPool <DataStream> dataStreamPool = null,
                      PacketReader packetReader         = null,
                      PacketWriter packetWriter         = null,
                      PacketPolicy packetPolicy         = null) : base(dataStreamPool, packetReader, packetWriter, packetPolicy)
 {
     Initialize(factory);
 }
Пример #3
0
 public ClientSession(string server,
                      IPool <DataStream> dataStreamPool = null,
                      PacketReader packetReader         = null,
                      PacketWriter packetWriter         = null,
                      PacketPolicy packetPolicy         = null) : base(dataStreamPool, packetReader, packetWriter, packetPolicy)
 {
     Initialize(new TcpSocketOpener(server));
 }
Пример #4
0
        private void ProcessReceivedData(object sender, byte[] buffer, int offset, int length)
        {
            var connection = (Connection)sender;

            if (!CheckConnection(connection))
            {
                return;
            }
            PacketReader.PushBack(buffer, offset, length);

            while (PacketReader.ReadNext())
            {
                if (!CheckConnection(connection))
                {
                    return;
                }
                switch (PacketReader.State)
                {
                case PacketReaderState.Complete:
                {
                    var packet       = GetPacket(PacketReader.PacketId, PacketReader.PacketStream);
                    var policyResult = PacketPolicy.CheckPacket(packet);
                    PacketReader.UpdatePolicy(policyResult);

                    if (policyResult == PacketPolicyState.Drop)
                    {
                        Close();
                        return;
                    }
                    if (policyResult == PacketPolicyState.Reject)
                    {
                        continue;
                    }

                    ProcessReceivedPacket(packet);
                }
                    continue;

                case PacketReaderState.WaitingContent:
                {
                    var policyResult = PacketPolicy.CheckLength(PacketReader.PacketId, PacketReader.PacketLength);
                    PacketReader.UpdatePolicy(policyResult);

                    if (policyResult == PacketPolicyState.Drop)
                    {
                        Close();
                        return;
                    }
                }
                    continue;

                case PacketReaderState.WaitingLength:
                {
                    var policyResult = PacketPolicy.CheckId(PacketReader.PacketId);
                    PacketReader.UpdatePolicy(policyResult);

                    if (policyResult == PacketPolicyState.Drop)
                    {
                        Close();
                        return;
                    }
                }
                    continue;
                }
            }
        }