示例#1
0
 public FlacMediaDecoder()
 {
     this._streamDecoder = new StreamDecoder();
     this._streamDecoder.WriteCallback += this.WriteCallback;
     this._streamDecoder.MetadataCallback += this.MetadataCallback;
     this._currentData = _noCurrentData;
 }
 public void GetNextBuffer(BufferSegment segment)
 {
     segment.buffer = buffer;
     segment.offset = bufferOffset;
     segment.size = bufferSize;
     bufferOffset += bufferSize;
 }
示例#3
0
 /// <summary>
 /// Processes a received complex ack
 /// </summary>
 /// <param name="source">The address of the device that sent the ack</param>
 /// <param name="message">The complex ack header</param>
 /// <param name="segment">The buffer segment containing the ack content</param>
 public void ProcessComplexAck(Address source, ComplexAckMessage message, BufferSegment segment)
 {
     ClientTransaction tx = null;
     lock (_lock)
     {
         tx = _getClientTransaction(source, message.InvokeId);
     }
     if(tx != null)
         tx.OnComplexAck(message, segment);
 }
示例#4
0
        protected override void HandleRequest(Socket socket, BufferSegment data, ref object token)
        {
            HttpParser parser = (token as HttpParser) ?? new HttpParser();
            parser.AddData(data.Buffer, data.Offset, data.Length);

            if (parser.Completed)
            {
                OnRequest(new HttpContext(this, socket, parser.Request));
                token = null;
            }
            else
            {
                token = parser;
            }
        }
示例#5
0
        /// <summary>Записывает массив байт в указанное место буфера</summary>
        /// <param name="Position">Адрес, по которому следует разместить первый байт</param>
        /// <param name="Bytes">Данные для записи</param>
        public void Write(int Position, params byte[] Bytes)
        {
            BufferSegment segment = _segments.SingleOrDefault(s => s.StartPosition <= Position && s.StartPosition + s.Length >= Position);
            if (segment == null)
            {
                segment = new BufferSegment(Position);
                _segments.Add(segment);
            }
            List<BufferSegment> overlappedSegments =
                _segments.Where(s => s != segment && s.StartPosition >= Position && s.StartPosition < Position + Bytes.Length).ToList();
            foreach (BufferSegment s in overlappedSegments)
            {
                segment.Seek(s.StartPosition - segment.StartPosition, SeekOrigin.Begin);
                s.Seek(0, SeekOrigin.Begin);
                s.CopyTo(segment);
                _segments.Remove(s);
            }

            segment.Seek(Position - segment.StartPosition, SeekOrigin.Begin);
            segment.Write(Bytes, 0, Bytes.Length);
        }
示例#6
0
        protected virtual bool BuildMessage(BufferSegment buffer)
        {
            if (m_messagePart == null)
            {
                m_messagePart = new IPCMessagePart();
            }

            var reader = new FastBigEndianReader(buffer)
            {
                Position    = buffer.Offset + m_readOffset,
                MaxPosition = buffer.Offset + m_readOffset + m_remainingLength,
            };

            bool built;

            try
            {
                built = m_messagePart.Build(reader);
            }
            catch
            {
                logger.Error("Cannot build message. Length={0} LengthSize={3} RemainingLength={1} Data={2}", m_messagePart.Length, m_remainingLength, m_messagePart.Data, m_messagePart.LengthBytesCount);
                throw;
            }

            // if message is complete
            if (built)
            {
                var dataPos = reader.Position;
                // prevent to read above
                reader.MaxPosition = dataPos + m_messagePart.Length.Value;

                IPCMessage message;
                try
                {
                    message = IPCMessageSerializer.Instance.Deserialize(m_messagePart.Data);
                }
                catch (Exception ex)
                {
                    reader.Seek(dataPos, SeekOrigin.Begin);
                    logger.Debug("Message = {0}", m_messagePart.Data.ToString(" "));
                    logger.Error("Error while deserializing IPC Message : " + ex);

                    return(m_remainingLength <= 0 || BuildMessage(buffer));
                }

                TaskPool.AddMessage(() => ProcessMessage(message));

                m_remainingLength -= (int)(reader.Position - (buffer.Offset + m_readOffset));
                m_writeOffset      = m_readOffset = (int)reader.Position - buffer.Offset;
                m_messagePart      = null;

                return(m_remainingLength <= 0 || BuildMessage(buffer));
            }

            m_remainingLength -= (int)(reader.Position - (buffer.Offset + m_readOffset));
            m_readOffset       = (int)reader.Position - buffer.Offset;
            m_writeOffset      = m_readOffset + m_remainingLength;

            EnsureBuffer(m_messagePart.Length.HasValue ? m_messagePart.Length.Value : 5);

            return(false);
        }
示例#7
0
 public static DisposableRealmPacketIn CreateFromOutPacket(BufferSegment oldSegment, BufferSegment newSegment,
                                                           int totalLength)
 {
     return(DisposableRealmPacketIn.CreateFromOutPacket(oldSegment, newSegment, 0, totalLength));
 }
示例#8
0
        public static DisposableRealmPacketIn Create(PacketId opCode, byte[] outPacketContent)
        {
            BufferSegment segment = BufferManager.GetSegment(outPacketContent.Length + 6);

            return(DisposableRealmPacketIn.Create(opCode, outPacketContent, 0, outPacketContent.Length, segment));
        }
示例#9
0
        /// <summary>
        /// Pass recieved data into the packet buffer and try to parse.
        /// </summary>
        /// <param name="_remainingLength">number of bytes waiting to be read</param>
        /// <returns>false, if there is a part of a packet still remaining</returns>
        protected override bool OnReceive()
        {
            ushort uint16;

            while (true)
            {
                int offset = this._bufferSegment.Offset;
                uint16 = BitConverter.ToUInt16(this._bufferSegment.Buffer.Array, offset + 1);
                if (uint16 != (ushort)0)
                {
                    if (uint16 <= (ushort)4096)
                    {
                        if (this._remainingLength >= (int)uint16)
                        {
                            if (!this.KnownClientVersion)
                            {
                                byte[] buffer = new byte[2]
                                {
                                    this._bufferSegment.Buffer.Array[offset + 3],
                                    this._bufferSegment.Buffer.Array[offset + 4]
                                };
                                Asda2CryptHelper.XorData(buffer, 0, 2L, Locale.Start, Locale.Any);
                                if (buffer[1] == (byte)0)
                                {
                                    this.Locale = Locale.Start;
                                }
                                else if (buffer[1] == (byte)228)
                                {
                                    this.Locale = Locale.Ru;
                                }
                                this.KnownClientVersion = true;
                            }

                            Asda2CryptHelper.XorData(this._bufferSegment.Buffer.Array, offset + 3,
                                                     (long)((int)uint16 - 4), this.Locale, Locale.Any);
                            RealmPacketIn packet = new RealmPacketIn(this._bufferSegment, 7, (int)uint16 - 8,
                                                                     this.IsGameServerConnection);
                            RealmPacketMgr.Instance.HandlePacket((IRealmClient)this, packet);
                            this._remainingLength -= (int)uint16;
                            BufferSegment bufferSegment = this._bufferSegment;
                            this._bufferSegment = ClientBase.Buffers.CheckOut();
                            if (this._remainingLength > 0)
                            {
                                Array.Copy((Array)bufferSegment.Buffer.Array, bufferSegment.Offset + (int)uint16,
                                           (Array)this._bufferSegment.Buffer.Array, this._bufferSegment.Offset,
                                           this._remainingLength);
                            }
                            else
                            {
                                goto label_14;
                            }
                        }
                        else
                        {
                            goto label_5;
                        }
                    }
                    else
                    {
                        goto label_3;
                    }
                }
                else
                {
                    break;
                }
            }

            throw new ObjectDisposedException("none");
label_3:
            LogUtil.WarnException("{0} send packet with lenght {1}. HACKER! Remaining length {2}", new object[3]
            {
                (object)this.AccountName,
                (object)uint16,
                (object)this._remainingLength
            });
            this._remainingLength = 0;
            this.Disconnect(false);
            throw new InvalidOperationException("Wrong data from client.");
label_5:
            return(true);

label_14:
            return(true);
        }
示例#10
0
 public DisposableRealmPacketIn(BufferSegment segment, int offset, int length, int contentLength,
                                RealmServerOpCode packetId)
     : base(segment, offset, length, packetId, length - contentLength)
 {
 }
示例#11
0
 public SkylakeNATMessage(BufferSegment payload)
 {
     this.Payload = payload ?? throw new ArgumentNullException(nameof(payload));
 }
示例#12
0
 protected virtual void OnMessage(BufferSegment e)
 {
     this.Message?.Invoke(this, e);
 }
示例#13
0
 //(int size)
 protected void EnsureBuffer()
 {
     //if (size > BufferSize - _offset)
     {
         // not enough space left in buffer: Copy to new buffer
         var newSegment = Buffers.CheckOut();
         Array.Copy(_bufferSegment.Buffer.Array,
             _bufferSegment.Offset + _offset,
             newSegment.Buffer.Array,
             newSegment.Offset,
             _remainingLength);
         _bufferSegment.DecrementUsage();
         _bufferSegment = newSegment;
         _offset = 0;
     }
 }
示例#14
0
 private void WriteCallback(object sender, StreamDecoderWriteEventArgs e)
 {
     IBuffer currentSample = e.GetBuffer();
     GC.AddMemoryPressure(currentSample.Capacity);
     this._currentData = new BufferSegment(currentSample);
     e.SetResult(StreamDecoderWriteStatus.Continue);
 }
示例#15
0
 /// <summary>
 /// </summary>
 /// <param name="segment">
 /// </param>
 /// <param name="length">
 /// </param>
 public void Send(BufferSegment segment, int length)
 {
     this.Send(segment.Buffer.Array, segment.Offset, length);
 }
示例#16
0
        /// <summary>
        /// Requeues a segment into the buffer pool.
        /// </summary>
        /// <param name="segment">the segment to requeue</param>
        public void CheckIn(BufferSegment segment)
        {
            if (segment.m_uses > 1)
            {

            }

            _availableSegments.Enqueue(segment);
        }
示例#17
0
        public IBuffer ReadSample(IBuffer buffer, uint count)
        {
            if (buffer == null)
                throw new ArgumentNullException();

            if (count > buffer.Capacity)
                throw new ArgumentOutOfRangeException();

            if (this._currentData.Count >= count)
            {
                this._currentData.Buffer.CopyTo(this._currentData.Offset, buffer, 0, count);
                this._currentData = new BufferSegment(this._currentData.Buffer,
                    this._currentData.Offset + count, this._currentData.Count - count);
                buffer.Length = count;
                return buffer;
            }

            uint read = this._currentData.Count;
            if (read > 0)
                this._currentData.Buffer.CopyTo(this._currentData.Offset, buffer, 0, this._currentData.Count);
            this._currentData = _noCurrentData;

            while (this.RequestSample())
            {
                uint rest = count - read;
                if (this._currentData.Count >= rest)
                {
                    this._currentData.Buffer.CopyTo(0, buffer, read, rest);
                    read += rest;
                    this._currentData = new BufferSegment(this._currentData.Buffer, rest, this._currentData.Count - rest);
                    break;
                }
                this._currentData.Buffer.CopyTo(0, buffer, read, this._currentData.Count);
                read += this._currentData.Count;
            }

            buffer.Length = read;
            return buffer;
        }
示例#18
0
 public void Send(object handle, BufferSegment data)
 {
     StartSend(handle as Socket, data, null);
 }
示例#19
0
 /// <summary>
 /// Processes a received confirmed request
 /// </summary>
 /// <param name="source">The address of the device that sent the request</param>
 /// <param name="message">The confirmed request header</param>
 /// <param name="segment">The buffer segment containing the request content</param>
 public void ProcessConfirmedRequest(Address source, ConfirmedRequestMessage message, BufferSegment segment)
 {
 }
示例#20
0
        /// <summary>
        /// Called whenever a complex ack is received
        /// for this transaction
        /// </summary>
        /// <param name="message">The received message</param>
        /// <param name="segment">The segment</param>
        public void OnComplexAck(ComplexAckMessage message, BufferSegment segment)
        {
            lock(_lock)
            {
                bool dispose = false;

                if (_state == ClientState.AwaitConfirmation)
                {
                    if(!message.Segmented)
                    {
                        _handle.FeedComplexAck(message, segment);
                        dispose = true;
                    }
                    else if(message.SequenceNumber == 0)
                    {
                        _sequenceNumber = 1;
                        _windowSize = message.ProposedWindowSize;
                        _sendSegmentAck();
                        _handle.FeedComplexAck(message, segment);
                        _transitionTo(ClientState.SegmentedConfirmation);
                    }
                    else
                    {
                        _sendAbort(AbortReason.InvalidApduInThisState);
                        _handle.FeedAbort(AbortReason.InvalidApduInThisState);
                        dispose = true;
                    }
                }
                else if(_state == ClientState.SegmentedConfirmation)
                {
                    int sequenceNumber;

                    if ((sequenceNumber = _inWindow(message.SequenceNumber)) != -1
                        && sequenceNumber == _sequenceNumber)
                    {
                        _handle.FeedComplexAck(message, segment);
                        _windowSize = message.ProposedWindowSize;
                        _sequenceNumber++;

                        dispose = !message.MoreFollows;
                        if (dispose || _sequenceNumber == _windowStart + _windowSize)
                            _sendSegmentAck();
                        else if (!dispose)
                            _transitionTo(ClientState.SegmentedConfirmation);
                    }
                    else
                    {
                        _sendSegmentAck(nack:true);
                        _transitionTo(ClientState.SegmentedConfirmation);
                    }
                }

                if(dispose)
                    _transitionTo(ClientState.Disposed);
            }
        }
示例#21
0
 /// <summary>
 /// Notifies the handle that a new response segment
 /// is available
 /// </summary>
 /// <param name="message">The complex ack message</param>
 /// <param name="segment">The response segment</param>
 public override void FeedComplexAck(ComplexAckMessage message, BufferSegment segment)
 {
     _source.SetException(new AbortException(AbortReason.InvalidApduInThisState));
 }
示例#22
0
        public async Task WriteTests(int[][] writes, WriteType writeType)
        {
            await RunClientServer(
                async clientConnection =>
            {
                await using QuicStream stream = clientConnection.OpenUnidirectionalStream();

                foreach (int[] bufferLengths in writes)
                {
                    switch (writeType)
                    {
                    case WriteType.SingleBuffer:
                        foreach (int bufferLength in bufferLengths)
                        {
                            await stream.WriteAsync(new byte[bufferLength]);
                        }
                        break;

                    case WriteType.GatheredBuffers:
                        var buffers = bufferLengths
                                      .Select(bufferLength => new ReadOnlyMemory <byte>(new byte[bufferLength]))
                                      .ToArray();
                        await stream.WriteAsync(buffers);
                        break;

                    case WriteType.GatheredSequence:
                        var firstSegment          = new BufferSegment(new byte[bufferLengths[0]]);
                        BufferSegment lastSegment = firstSegment;

                        foreach (int bufferLength in bufferLengths.Skip(1))
                        {
                            lastSegment = lastSegment.Append(new byte[bufferLength]);
                        }

                        var buffer = new ReadOnlySequence <byte>(firstSegment, 0, lastSegment, lastSegment.Memory.Length);
                        await stream.WriteAsync(buffer);
                        break;

                    default:
                        Debug.Fail("Unknown write type.");
                        break;
                    }
                }

                stream.Shutdown();
                await stream.ShutdownCompleted();
            },
                async serverConnection =>
            {
                await using QuicStream stream = await serverConnection.AcceptStreamAsync();

                var buffer        = new byte[4096];
                int receivedBytes = 0, totalBytes = 0;

                while ((receivedBytes = await stream.ReadAsync(buffer)) != 0)
                {
                    totalBytes += receivedBytes;
                }

                int expectedTotalBytes = writes.SelectMany(x => x).Sum();
                Assert.Equal(expectedTotalBytes, totalBytes);

                stream.Shutdown();
                await stream.ShutdownCompleted();
            });
        }
示例#23
0
        private void EnsureCapacity(int size)
        {
            // return the old segment and get a new, bigger one
            var newSegment = BufferManager.GetSegment(size);
            _segment.CopyTo(newSegment, _length);
            m_Position = m_Position - _segment.Offset + newSegment.Offset;

            _segment.DecrementUsage();
            _segment = newSegment;
        }
示例#24
0
 public SegmentStream(BufferSegment segment)
 {
     _segment = segment;
     m_Position = _segment.Offset;
 }
示例#25
0
 /// <summary>
 /// Notifies the handle that a new response segment
 /// is available
 /// </summary>
 /// <param name="message">The complex ack message</param>
 /// <param name="segment">The response segment</param>
 public override void FeedComplexAck(ComplexAckMessage message, BufferSegment segment)
 {
     _source.SetException(new AbortException(AbortReason.InvalidApduInThisState));
 }
示例#26
0
 public SSyncClient()
 {
     Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     _bufferSegment = Buffers.CheckOut();
 }
示例#27
0
 public DataReceivedEventArgs(BufferSegment buffer, MazeSocket.MessageOpcode opcode)
 {
     Buffer = buffer;
     Opcode = opcode;
 }
示例#28
0
            public virtual bool Send(SkylakeNATMessage message, int sent = 1)
            {
                if (message == null)
                {
                    return(false);
                }
                Socket socket = null;

                lock (this._syncobj)
                {
                    socket = this._socket;
                    if (socket == null)
                    {
                        return(false);
                    }
                }
                BufferSegment payload_segment = message.Payload;

#if !_USE_RC4_SIMPLE_ENCIPHER || __USE_UDP_PAYLOAD_TAP_PACKET
                if (payload_segment.Length > 0)
                {
                    payload_segment = this._encryptor.Encrypt(payload_segment);
                }
#endif
                int    payload_size = payload_segment.Length;
                byte[] packet       = new byte[sizeof(pkg_hdr) + payload_size];
                fixed(byte *pinned = packet)
                {
                    pkg_hdr *pkg = (pkg_hdr *)pinned;

                    pkg->fk  = pkg_hdr.FK;
                    pkg->len = unchecked ((ushort)payload_size);
                    pkg->cmd = unchecked ((byte)message.Commands);
                    if (payload_size > 0)
                    {
                        byte *payload_data = sizeof(pkg_hdr) + pinned;
                        Marshal.Copy(payload_segment.Buffer, payload_segment.Offset, (IntPtr)payload_data, payload_size);
#if _USE_RC4_SIMPLE_ENCIPHER
                        RC4.rc4_crypt(this.Router.Key, payload_data, payload_size, this.Router.Subtract, 1);
#endif
                    }
                }

#if _USE_RC4_SIMPLE_ENCIPHER
                try
                {
                    if (sent <= 0)
                    {
                        sent = 1;
                    }
                    for (int i = 0; i < sent; i++)
                    {
                        socket.BeginSendTo(packet, 0, packet.Length, SocketFlags.None, this.LocalEndPoint, (ar) =>
                        {
                            try
                            {
                                socket.EndSendTo(ar);
                            }
                            catch (Exception) { }
                        }, null);
                    }
                    return(true);
                }
                catch (Exception)
                {
                    return(false);
                }
#else
                SocketError error = SocketError.SocketError;
                try
                {
                    socket.BeginSend(packet, 0, packet.Length, SocketFlags.None, out error, (ar) =>
                    {
                        error = SocketError.SocketError;
                        try
                        {
                            socket.EndSend(ar, out error);
                        }
                        catch (Exception)
                        {
                            error = SocketError.SocketError;
                        }
                        if (error != SocketError.Success)
                        {
                            this.CloseOrAbort();
                        }
                    }, null);
                }
                catch (Exception)
                {
                    error = SocketError.SocketError;
                }
                if (error != SocketError.Success && error != SocketError.IOPending)
                {
                    this.CloseOrAbort();
                    return(false);
                }
                return(true);
#endif
            }
        /// <summary>
        /// </summary>
        /// <param name="buffer">
        /// </param>
        /// <returns>
        /// </returns>
        /// <exception cref="NotImplementedException">
        /// </exception>
        protected override bool OnReceive(BufferSegment buffer)
        {
            // return false, if header cannot be complete (00FF55AA, <Length of packet>)

            // Loop if more than one packet frame received
            while (true)
            {
                if (this._remainingLength == 0)
                {
                    return(true);
                }

                if (this._remainingLength < 8)
                {
                    return(false);
                }

                int expectedLength = this.CheckData(buffer);
                if (expectedLength == -1)
                {
                    // MALFORMED PACKET RECEIVED !!!
                    LogUtil.Debug(DebugInfoDetail.Error, "Malformed packet received: ");
                    byte[] data = new byte[this._remainingLength];
                    buffer.SegmentData.CopyTo(data, this._remainingLength);
                    LogUtil.Debug(DebugInfoDetail.Error, HexOutput.Output(data));
                    this._remainingLength = 0;
                    this._offset          = 0;

                    // Lets clear the buffer and try this again, no need to drop the connection
                    return(true);
                }

                if (expectedLength + 8 > this._remainingLength)
                {
                    return(false);
                }

                if (this._remainingLength >= expectedLength + 8)
                {
                    // Handle packet payload here

                    byte[] dataBytes = new byte[expectedLength];
                    Array.Copy(buffer.SegmentData, 8 + this._offset, dataBytes, 0, expectedLength);
                    if (this.DataReceived != null)
                    {
                        this.DataReceived(this, new OnDataReceivedArgs()
                        {
                            dataBytes = dataBytes
                        });
                    }
                    else
                    {
                        LogUtil.Debug(DebugInfoDetail.Error, "No DataReceived event fired due to missing method");
                    }
                }

                if (expectedLength + 8 <= this._remainingLength)
                {
                    // If we have received a full packet frame
                    // then move the remaining data to a new buffer (with offset 0)
                    // only adjusting offset and length here
                    // Then do the whole thing again
                    this._remainingLength -= expectedLength + 8;
                    this._offset          += expectedLength + 8;
                }
            }
        }
示例#30
0
        private void ProcessReceiveFromUdp(IAsyncResult ar)
        {
            lock (this._syncobj)
            {
                try
                {
                    if (this._disposed)
                        return; }
                    if (ar == null)
                    {
                        EndPoint remoteEP = this._server.LocalEndPoint;
                        this._server.BeginReceiveFrom(_mssPacketBuffer, 0, _mssPacketBuffer.Length, 0, ref remoteEP, ProcessReceiveFromUdp, null);
                    }
                    else
                    {
                        EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
                        int      bytes    = this._server.EndReceiveFrom(ar, ref remoteEP);
                        do
                        {
                            if (bytes < sizeof(pkg_hdr))
                                break;
                            fixed(byte *pinned = _mssPacketBuffer)
                            {
                                pkg_hdr *pkg = (pkg_hdr *)pinned;

                                if (pkg->fk != pkg_hdr.FK)
                                {
                                    break;
                                }
                                if ((pkg->len + sizeof(pkg_hdr)) != bytes)
                                {
                                    break;
                                }
                                if (pkg->id == 0)
                                {
                                    break;
                                }
                                Commands commands = unchecked ((Commands)pkg->cmd);

                                if (commands == Commands.NATCommands_kAuthentication)
                                {
                                    NATClientContext context = null;
                                    SkylakeNATClient client  = null;
                                    lock (this._sockets)
                                    {
                                        if (!_natClientTable.TryGetValue(pkg->id, out context) || context == null)
                                        {
                                            client = this.CreateClient(pkg->id, remoteEP);
                                            if (client != null)
                                            {
                                                client.LocalEndPoint = remoteEP;
                                                context = new NATClientContext()
                                                {
                                                    client = client
                                                };
                                                _natClientTable[pkg->id] = context;
                                                client.Abort            += this._onSocketAbort;
                                                client.Message          += this._onSocketMessage;
                                                client.Authentication   += this._onAuthentication;
                                            }
                                        }
                                        else
                                        {
                                            client = context.client;
                                        }
                                    }
                                    if (context != null && client != null)
                                    {
                                        lock (context)
                                        {
                                            context.agingsw.Restart();
                                        }
                                        client.LocalEndPoint = remoteEP;
                                        client.OnAuthentication(EventArgs.Empty);
                                    }
                                }
                                else
                                {
                                    SkylakeNATClient client = null;
                                    lock (this._sockets)
                                    {
                                        _natClientTable.TryGetValue(pkg->id, out NATClientContext context);
                                        if (context != null)
                                        {
                                            lock (context)
                                            {
                                                context.agingsw.Restart();
                                            }
                                            client = context.client;
                                        }
                                    }
                                    if (client != null)
                                    {
                                        BufferSegment payload = null;
                                        if (pkg->len > 0)
                                        {
                                            int ofs = sizeof(pkg_hdr);
#if _USE_RC4_SIMPLE_ENCIPHER
                                            fixed(byte *payloadPtr = &_mssPacketBuffer[ofs])
                                            RC4.rc4_crypt(this.Key, payloadPtr, pkg->len, this.Subtract, 0);
#endif
                                            payload = client._encryptor.Decrypt(new BufferSegment(_mssPacketBuffer, ofs, pkg->len));
                                        }
                                        else
                                        {
                                            payload = new BufferSegment(BufferSegment.Empty);
                                        }
                                        client.OnMessage(new SkylakeNATMessage(payload)
                                        {
                                            Commands = commands,
                                        });
                                    }
                                }
                            }
                        } while (false);
                        this.ProcessReceiveFromUdp(null);
                    }
                }
示例#31
0
 /// <summary>
 /// Logs an exception as a warning.
 /// </summary>
 /// <param name="exception">The exception object that contains the error.</param>
 /// <param name="buffer">The buffer for the log entry.</param>
 public virtual void Log(Exception exception, BufferSegment buffer)
 {
     Log(LogLevel.Error, exception, buffer.AsSegment());
 }
示例#32
0
 public virtual bool Send(BufferSegment buffer)
 {
     return(this.m_poPCB.Send(buffer));
 }
示例#33
0
 public PacketWriter(BufferSegment segment)
     : this(segment, DefaultEncoding)
 {
 }
示例#34
0
 private Task SendData(BufferSegment data) => Send(data.Buffer, data.Offset, data.Length, true);
示例#35
0
 public PacketWriter(BufferSegment segment, Encoding encoding)
     : this(new BufferStream(segment, 0), encoding)
 {
 }
示例#36
0
 public static DisposableRealmPacketIn CreateFromOutPacket(BufferSegment segment, RealmPacketOut packet)
 {
     byte[] finalizedPacket = packet.GetFinalizedPacket();
     return(DisposableRealmPacketIn.Create(packet.PacketId, finalizedPacket, packet.HeaderSize,
                                           finalizedPacket.Length - packet.HeaderSize, segment));
 }
示例#37
0
 /// <summary>
 /// Constructs a PacketIn object given the buffer to read, the offset to read from, and the number of bytes to read.
 /// </summary>
 /// <param name="segment">The buffer container wrapping our data</param>
 /// <param name="offset">The offset to read from the data array</param>
 /// <param name="length">The number of bytes to read</param>
 protected PacketIn(BufferSegment segment, int offset, int length)
     : base(new MemoryStream(segment.Buffer.Array, segment.Offset + offset, length), DefaultEncoding)
 {
     _segment = segment;
     _offset  = offset;
 }
示例#38
0
        public static DisposableRealmPacketIn CreateFromOutPacket(BufferSegment oldSegment, BufferSegment newSegment,
                                                                  int offset, int totalLength)
        {
            int num = oldSegment.Offset + offset;

            return(DisposableRealmPacketIn.Create(
                       (PacketId)((RealmServerOpCode)((int)oldSegment.Buffer.Array[num + 2] |
                                                      (int)oldSegment.Buffer.Array[num + 3] << 8)), oldSegment.Buffer.Array,
                       oldSegment.Offset + offset + 4, totalLength - 4, newSegment));
        }
示例#39
0
        public IncomingPacket Parse(SocketManager manager, BufferSegment data, TransportEventTypes transportEvent = TransportEventTypes.Unknown)
        {
            using (var stream = new System.IO.MemoryStream(data.Data, data.Offset, data.Count))
            {
                var buff = BufferPool.Get(MsgPackReader.DEFAULT_BUFFER_SIZE, true);
                try
                {
                    var context = new SerializationContext
                    {
                        Options = SerializationOptions.SuppressTypeInformation/*,
                                                                               * ExtensionTypeHandler = CustomMessagePackExtensionTypeHandler.Instance*/
                    };
                    IJsonReader reader = new MsgPackReader(stream, context, Endianness.BigEndian, buff);

                    reader.ReadObjectBegin();

                    int    type = -1, id = -1;
                    string nsp = null;

                    bool hasData = false, readData = false;

                    IncomingPacket packet = IncomingPacket.Empty;

READ:

                    while (reader.Token != JsonToken.EndOfObject)
                    {
                        string key = reader.ReadMember();

                        switch (key)
                        {
                        case "type":
                            type = reader.ReadByte();
                            break;

                        case "nsp":
                            nsp = reader.ReadString();
                            break;

                        case "id":
                            id = reader.ReadInt32();
                            break;

                        case "data":
                            if (!hasData)
                            {
                                hasData = true;
                                SkipData(reader, (SocketIOEventTypes)type);
                            }
                            else
                            {
                                readData = true;

                                packet = new IncomingPacket(transportEvent != TransportEventTypes.Unknown ? transportEvent : TransportEventTypes.Message, (SocketIOEventTypes)type, nsp, id);
                                (string eventName, object[] args) = ReadData(manager, packet, reader);

                                packet.EventName = eventName;
                                if (args != null)
                                {
                                    if (args.Length == 1)
                                    {
                                        packet.DecodedArg = args[0];
                                    }
                                    else
                                    {
                                        packet.DecodedArgs = args;
                                    }
                                }
                            }
                            break;
                        }
                    }

                    // type, nsp, id and data can come in any order. To read data strongly typed we need to know all the additional fields before processing the data field.
                    // In order to do it, when we first encounter the data field we skip it than we do a reset and an additional turn but reading the data too now.
                    if (hasData && !readData)
                    {
                        reader.Reset();
                        stream.Position = 0;
                        reader.ReadObjectBegin();

                        goto READ;
                    }

                    reader.ReadObjectEnd();

                    return(packet.Equals(IncomingPacket.Empty) ? new IncomingPacket(transportEvent != TransportEventTypes.Unknown ? transportEvent : TransportEventTypes.Message, (SocketIOEventTypes)type, nsp, id) : packet);
                }
                finally
                {
                    BufferPool.Release(buff);
                }
            }
        }
示例#40
0
 /// <summary>
 /// Notifies the handle that a new response segment
 /// is available
 /// </summary>
 /// <param name="message">The complex ack message</param>
 /// <param name="segment">The response segment</param>
 public abstract void FeedComplexAck(ComplexAckMessage message, BufferSegment segment);
示例#41
0
        public override async Task <Envelope> ReceiveAsync(CancellationToken cancellationToken)
        {
            EnsureOpen("receive");

            var segments = new List <BufferSegment>();

            await _receiveSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                EnsureOpen("receive");

                while (!_receiveCts.IsCancellationRequested)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var segment = new BufferSegment
                    {
                        Buffer = _arrayPool.Rent(_bufferSize)
                    };
                    segments.Add(segment);

                    // The websocket class go to the 'Aborted' state if the receiveasync operation is cancelled.
                    // In this case, we are unable to close the connection clearly when required.
                    var receiveTask      = WebSocket.ReceiveAsync(new ArraySegment <byte>(segment.Buffer), _receiveCts.Token);
                    var cancellationTask = cancellationToken.AsTask();

                    // If the token is cancelled
                    var completedTask = await Task.WhenAny(receiveTask, cancellationTask).ConfigureAwait(false);

                    if (completedTask != receiveTask)
                    {
                        // The task above will thrown a TaskCancelledException, but just in case...
                        cancellationToken.ThrowIfCancellationRequested();
                    }

                    var receiveResult = await receiveTask;
                    if (receiveResult == null)
                    {
                        continue;
                    }

                    segment.Count = receiveResult.Count;

                    if (receiveResult.MessageType == WebSocketMessageType.Close)
                    {
                        HandleCloseMessage(receiveResult);
                        break;
                    }

                    if (receiveResult.MessageType != _webSocketMessageType)
                    {
                        CloseStatus            = WebSocketCloseStatus.InvalidMessageType;
                        CloseStatusDescription = "An unsupported message type was received";
                        throw new InvalidOperationException(CloseStatusDescription);
                    }

                    if (receiveResult.EndOfMessage)
                    {
                        break;
                    }

                    if (segments.Count + 1 > DEFAULT_MAX_BUFFER_COUNT)
                    {
                        throw new BufferOverflowException("Maximum buffer size reached");
                    }
                }
            }
            catch (WebSocketException)
            {
                foreach (var segment in segments)
                {
                    _arrayPool.Return(segment.Buffer);
                }

                await CloseWithTimeoutAsync().ConfigureAwait(false);

                throw;
            }
            catch (Exception)
            {
                foreach (var segment in segments)
                {
                    _arrayPool.Return(segment.Buffer);
                }

                throw;
            }
            finally
            {
                _receiveSemaphore.Release();
            }

            string serializedEnvelope = null;

            // Build the serialized envelope using the buffers
            using (var stream = new MemoryStream())
            {
                foreach (var segment in segments)
                {
                    stream.Write(segment.Buffer, 0, segment.Count);
                    _arrayPool.Return(segment.Buffer);
                }

                var buffer = stream.ToArray();
                serializedEnvelope = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
            }

            if (_traceWriter != null &&
                _traceWriter.IsEnabled)
            {
                await _traceWriter.TraceAsync(serializedEnvelope, DataOperation.Receive).ConfigureAwait(false);
            }

            if (string.IsNullOrWhiteSpace(serializedEnvelope))
            {
                return(null);
            }

            return(_envelopeSerializer.Deserialize(serializedEnvelope));
        }
示例#42
0
 /// <summary>
 /// </summary>
 public ISComV2ClientBase()
 {
     this._bufferSegment = Buffers.CheckOut();
 }
示例#43
0
 /// <summary>
 /// ITransport.Send
 /// </summary>
 /// <param name="msg"></param>
 public abstract void Send(BufferSegment msg);
示例#44
0
 public PacketReader(BufferSegment segment)
     : this(new BufferStream(segment, false))
 {
 }
示例#45
0
        /// <summary>
        /// Start sending data
        /// </summary>
        /// <param name="acceptor"></param>
        /// <param name="data"></param>
        /// <param name="token"></param>
        public void StartSend(Socket acceptor, BufferSegment data, object token)
        {
            _senders.Take(sender =>
            {
                if (data.Length > 0 && acceptor.Connected)
                {
                    sender.AcceptSocket = acceptor;
                    sender.SetBuffer(data.Buffer, data.Offset, data.Length);
                    sender.UserToken = token;

                    if (!acceptor.SendAsync(sender))
                    {
                        ProcessSend(sender);
                    }
                }
                else
                {
                    _senders.Release(sender);
                }
            });
        }
示例#46
0
 public FakeClient(int id)
 {
     Id = id;
     m_bufferSegment = BufferManager.GetSegment(ClientManager.BufferSize);
 }
示例#47
0
 protected abstract void HandleRequest(Socket socket, BufferSegment data, ref object token);
示例#48
0
 public NetgramReceivedMessage(byte portId, Mac source, BufferSegment segment)
 {
     this.PortId = portId;
     this.Source = source;
     this.Segment = segment;
 }
示例#49
0
        /// <summary>
        /// Process data received
        /// </summary>
        /// <param name="receiver"></param>
        protected void ProcessReceive(SocketAsyncEventArgs receiver)
        {
            var token = receiver.UserToken;
            var socket = receiver.AcceptSocket;
            bool cont = false;

            if (receiver.SocketError != SocketError.Success)
            {
                Close(socket);
            }
            else if (receiver.BytesTransferred > 0)
            {
                var segment = new BufferSegment(receiver.Buffer, receiver.Offset, receiver.BytesTransferred);
                HandleRequest(socket, segment, ref token);
                cont = true;
            }

            // Send the receiver back
            receiver.UserToken = null;
            receiver.AcceptSocket = null;
            _receivers.Release(receiver);

            if (cont)
            {
                StartReceive(socket, token);
            }
        }
示例#50
0
 public bool DataArrivals(BufferSegment data)
 {
     if (OnDataArrival != null)
         OnDataArrival(data.SegmentData);
     return true;
 }
示例#51
0
 protected virtual Task SendData(BufferSegment data, DataTransmitter target)
 {
     target.ReceiveData(data.Buffer, data.Offset);
     return(Task.CompletedTask);
 }
示例#52
0
        private void ProcessRecieve(SocketAsyncEventArgs args)
        {
            try
            {
                var bytesReceived = args.BytesTransferred;

                if (args.BytesTransferred == 0)
                {

                    OnSocketClosed();
                }
                else
                {
                    unchecked
                    {
                        _bytesReceived += (uint)bytesReceived;
                    }

                    Interlocked.Add(ref _totalBytesReceived, bytesReceived);

                    if (this.DataArrivals(_bufferSegment))
                    {
                        _offset = 0;
                        _bufferSegment.DecrementUsage();
                        _bufferSegment = Buffers.CheckOut();
                    }
                    else
                    {
                        EnsureBuffer();
                    }

                    this.Receive();
                }
            }
            catch (ObjectDisposedException)
            {

                OnSocketClosed();
            }
            catch (Exception)
            {

                OnSocketClosed();
            }
            finally
            {
                args.Completed -= ReceiveAsyncComplete;
                SocketHelpers.ReleaseSocketArg(args);
            }
        }
示例#53
0
            private void StartReceive(IAsyncResult ar)
            {
                SkylakeNATMessage message = null;
                SocketError       error   = SocketError.SocketError;

                try
                {
                    do
                    {
                        Socket socket = null;
                        lock (this._syncobj)
                            socket = this._socket;
                        if (socket == null)
                        {
                            return;
                        }
                        if (ar == null)
                        {
                            if (!_fhdr)
                            {
                                socket.BeginReceive(_phdr, 0, _phdr.Length, SocketFlags.None, out error, StartReceive, null);
                            }
                            else
                            {
                                int suplus = _message.Length - _fseek;
                                if (suplus >= Layer3Netif.MSS)
                                {
                                    suplus = Layer3Netif.MSS;
                                }
                                socket.BeginReceive(_message, _fseek, suplus, SocketFlags.None, out error, StartReceive, null);
                            }
                            if (error == SocketError.IOPending)
                            {
                                error = SocketError.Success;
                            }
                        }
                        else
                        {
                            int len = -1;
                            try
                            {
                                len = socket.EndReceive(ar, out error);
                            }
                            catch (Exception)
                            {
                                len = -1;
                            }
                            if (len <= 0)
                            {
                                error = SocketError.SocketError;
                                break;
                            }
                            else
                            {
                                bool completion = false;
                                if (!_fhdr)
                                {
                                    fixed(byte *pinned = _phdr)
                                    {
                                        pkg_hdr *pkg = (pkg_hdr *)pinned;

                                        if (len != sizeof(pkg_hdr) || pkg->fk != pkg_hdr.FK)
                                        {
                                            error = SocketError.SocketError;
                                            break;
                                        }
                                        if (0 == pkg->len)
                                        {
                                            completion = true;
                                        }
                                        else
                                        {
                                            _fseek   = 0;
                                            _fhdr    = true;
                                            _message = new byte[pkg->len];
                                        }
                                        error = SocketError.Success;
                                    }
                                }
                                else
                                {
                                    _fseek += len;
                                    if (_fseek >= _message.Length)
                                    {
                                        completion = true;
                                    }
                                    error = SocketError.Success;
                                }
                                if (completion)
                                {
                                    fixed(byte *pinned = _phdr)
                                    {
                                        pkg_hdr *pkg = (pkg_hdr *)pinned;

                                        if (0 == pkg->len)
                                        {
                                            message = new SkylakeNATMessage(new BufferSegment(BufferSegment.Empty));
                                        }
                                        else
                                        {
                                            message = new SkylakeNATMessage(new BufferSegment(_message));
                                        }
                                        if (0 == this.Id)
                                        {
                                            this.Id = pkg->id;
                                        }
                                        message.Commands = unchecked ((Commands)pkg->cmd);
                                        _fseek           = 0;
                                        _fhdr            = false;
                                        _message         = null;
                                    }
                                    error = SocketError.Success;
                                }
                            }
                        }
                    } while (false);
                }
                catch (Exception)
                {
                    error = SocketError.SocketError;
                }
                if (error != SocketError.Success)
                {
                    this.CloseOrAbort();
                }
                else if (ar != null)
                {
                    if (message != null)
                    {
                        BufferSegment segment = message.Payload;
#if !_USE_RC4_SIMPLE_ENCIPHER
                        if (segment.Length > 0)
                        {
                            segment         = this._encryptor.Decrypt(segment);
                            message.Payload = segment;
                        }
#else
                        fixed(byte *pinned = segment.Buffer)
                        if (pinned != null)
                        {
                            RC4.rc4_crypt(this.Router.Key, pinned, segment.Length, this.Router.Subtract, 0);
                        }
#endif
                        if (message.Commands != Commands.NATCommands_kAuthentication)
                        {
                            this.OnMessage(message);
                        }
                        else
                        {
                            this.OnAuthentication(message);
                        }
                    }
                    this.StartReceive(null);
                }
            }
示例#54
0
 public SSyncClient(Socket _sock)
 {
     this.Sock = _sock;
     _bufferSegment = Buffers.CheckOut();
     Receive();
 }
示例#55
0
        private bool RequestSample()
        {
            this.EnsureMetadataRead();

            bool result = this._streamDecoder.ProcessSingle();
            if (!result)
                this._currentData = _noCurrentData;

            return this._currentData.Count > 0;
        }