Example #1
0
        public void OffsetTest()
        {
            var packetInfo = new PacketInfo(new DeliveryOptions(false, true), 100, ConsoleLogger, new Payload(new byte[] { 0, 0, 1, 2, 3, 9, 9 }, 2, 3));

            packetInfo.Append(new Payload(new byte[] { 2, 1 }));
            packetInfo.Append(new Payload(new byte[] { 3, 1 }, 0, 2));
            packetInfo.Append(new Payload(new byte[] { 4, 2, 3, 1, 8 }, 0, 4));
            packetInfo.Append(new Payload(new byte[] { 8, 5, 2, 3, 1 }, 1, 4));
            packetInfo.Append(new Payload(new byte[] { 8, 6, 2, 3, 1, 0 }, 1, 4));

            var offsets = PacketInfo.GetOffsetInfo(packetInfo.Buffer, packetInfo.Offset).ToArray();

            offsets.Length.Should().Be(6);

            offsets[0].Length.Should().Be(3);
            offsets[1].Length.Should().Be(2);
            offsets[2].Length.Should().Be(2);
            offsets[3].Length.Should().Be(4);
            offsets[4].Length.Should().Be(4);
            offsets[5].Length.Should().Be(4);

            new ArraySegment <byte>(packetInfo.Buffer, offsets[0].Offset, offsets[0].Length).ToArray().Should()
            .BeEquivalentTo(new byte[] { 1, 2, 3 });
            new ArraySegment <byte>(packetInfo.Buffer, offsets[1].Offset, offsets[1].Length).ToArray().Should()
            .BeEquivalentTo(new byte[] { 2, 1 });
            new ArraySegment <byte>(packetInfo.Buffer, offsets[2].Offset, offsets[2].Length).ToArray().Should()
            .BeEquivalentTo(new byte[] { 3, 1 });
            new ArraySegment <byte>(packetInfo.Buffer, offsets[3].Offset, offsets[3].Length).ToArray().Should()
            .BeEquivalentTo(new byte[] { 4, 2, 3, 1 });
            new ArraySegment <byte>(packetInfo.Buffer, offsets[4].Offset, offsets[4].Length).ToArray().Should()
            .BeEquivalentTo(new byte[] { 5, 2, 3, 1 });
            new ArraySegment <byte>(packetInfo.Buffer, offsets[5].Offset, offsets[5].Length).ToArray().Should()
            .BeEquivalentTo(new byte[] { 6, 2, 3, 1 });
        }
Example #2
0
 public void ClientOnPackageReceived(IPacketInfo packet)
 {
     try
     {
         var offsets = PacketInfo.GetOffsetInfo(packet.Buffer, packet.Offset);
         foreach (var item in offsets)
         {
             try
             {
                 var operationCode = MessageBase.GetOperationCode(packet.Buffer, item.Offset);
                 if (operationCode != ShamanOperationCode.Bundle)
                 {
                     _logger.Debug($"Shaman message received. Operation code: {operationCode}");
                     _shamanMessageHandler.ProcessMessage(operationCode, packet.Buffer, item.Offset, item.Length);
                 }
                 else
                 {
                     _logger.Debug($"Bundle message received. Operation code: {operationCode}");
                     var bundleOperationCode = MessageBase.GetOperationCode(packet.Buffer, item.Offset + 1);
                     _bundleMessageHandler.ProcessMessage(bundleOperationCode, packet.Buffer, item.Offset + 1, item.Length - 1);
                 }
             }
             catch (Exception ex)
             {
                 _logger.Error(ex);
                 SetAndReportStatus(_status, _statusCallback, false, ex.Message);
             }
         }
     }
     finally
     {
         packet.Dispose();
     }
 }
Example #3
0
        public override void OnReceivePacketFromClient(IPEndPoint endPoint, DataPacket dataPacket)
        {
            GamePeer peer = null;

            try
            {
                peer = PeerCollection.Get(endPoint);
                var offsets = PacketInfo.GetOffsetInfo(dataPacket.Buffer, dataPacket.Offset);
                foreach (var item in offsets)
                {
                    try
                    {
                        var messageData = new Payload(dataPacket.Buffer, item.Offset, item.Length);
                        ProcessMessage(endPoint, messageData, dataPacket.DeliveryOptions, peer);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error($"Error processing message: {ex}");
                        if (peer != null)
                        {
                            _messageSender.Send(new ErrorResponse(ResultCode.MessageProcessingError), peer);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"OnReceivePacketFromClient: Error processing package: {ex}");
                if (peer != null)
                {
                    _messageSender.Send(new ErrorResponse(ResultCode.MessageProcessingError), peer);
                }
            }
        }
Example #4
0
 private void ClientOnPackageReceived(IPacketInfo packet)
 {
     lock (_syncCollection)
     {
         var offsets = PacketInfo.GetOffsetInfo(packet.Buffer, packet.Offset);
         foreach (var item in offsets)
         {
             try
             {
                 ProcessMessage(packet.Buffer, item.Offset, item.Length);
             }
             catch (Exception ex)
             {
                 _logger.Error($"Error processing message: {ex}");
             }
         }
         packet.Dispose();
     }
 }