Beispiel #1
0
        public SocketContext([NotNull] IServiceProvider services,
                             [NotNull] Socket socket, int signlinkId)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            _socket = socket ?? throw new ArgumentNullException(nameof(socket));
            _log    = services.ThrowOrGet <ILogger>();

            SignlinkId = signlinkId;

            OutStream = new OutBlob(services, OutStreamSize);
            InStream  = new CircularBlob(InStreamSize);

            _inBufferStream  = new byte[InBufferStreamSize];
            _socket.Blocking = false;
        }
Beispiel #2
0
        public bool TryReinitialize([NotNull] Socket socket, int signlinkId)
        {
            if (!socket.Connected)
            {
                return(false);
            }

            if (!CanReinitialize(signlinkId))
            {
                return(false);
            }

            DeadForMs    = 0;
            _msSinceData = 0;

            IsDisposed = false;
            _socket    = socket;

            // reset streams
            OutStream.ResetHeads();
            InStream = new CircularBlob(InStreamSize);

            return(true);
        }
        public IEnumerable <PacketMessage> Parse(CircularBlob stream)
        {
            while (stream.CanRead())
            {
                // peek everything untill we 100% have the packet.
                var opcodePeek     = stream.Peek();
                var lenType        = _db.GetIncoming(opcodePeek);
                var lenPayloadPeek = 0;
                var payloadOffset  = 0;

                switch (lenType)
                {
                case PacketLength.NextByte:
                    if (!stream.CanRead(1))
                    {
                        break;
                    }

                    lenPayloadPeek = stream.Peek(1);
                    payloadOffset  = 1;
                    break;

                case PacketLength.NextShort:
                    if (!stream.CanRead(2))
                    {
                        break;
                    }

                    lenPayloadPeek = stream.Peek(1) << 8 + stream.Peek(2);
                    payloadOffset  = 2;
                    break;

                case PacketLength.Undefined:
                    yield return(PacketMessage.Undefined(opcodePeek));

                    yield break;

                default:
                    lenPayloadPeek = (byte)lenType;
                    break;
                }

                if (!stream.CanRead(payloadOffset + lenPayloadPeek))
                {
                    break;
                }


                // we can read the whole packet, do so.
                // do some assertions on the way
                var opcode = stream.ReadByte();
                Debug.Assert(opcode == opcodePeek);
                var lenPayload = 0;

                switch (lenType)
                {
                case PacketLength.NextByte:
                    lenPayload = stream.ReadByte();
                    break;

                case PacketLength.NextShort:
                    lenPayload = stream.ReadInt16();
                    break;

                case PacketLength.Undefined:
                    yield return(PacketMessage.Undefined(opcodePeek));

                    yield break;

                default:
                    lenPayload = (byte)lenType;
                    break;
                }

                Debug.Assert(lenPayload == lenPayloadPeek);

                // don't bother creating a new Blob if we're storing nothing.
                if (lenPayload == 0)
                {
                    yield return(PacketMessage.Success(opcode, Blob.Empty));
                }
                else
                {
                    var payload = new byte[lenPayload];
                    stream.ReadBlock(payload, 0, lenPayload);

                    yield return(PacketMessage.Success(opcode, new Blob(payload)));
                }
            }
        }
 public IEnumerable <PacketMessage> Parse(CircularBlob stream) => throw new NotImplementedException();