Пример #1
0
        public void OnDeserialize(byte[] data)
        {
            var d = Serialization.ByteArrayToObject <Datatex>(data);

            this.test       = d.test;
            this.testString = d.testString;
        }
Пример #2
0
        private static void webClient_DownloadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            object data = null;
            Type   tipo = (Type)e.UserState;

            if (e is System.Net.DownloadDataCompletedEventArgs)
            {
                data = ((DownloadDataCompletedEventArgs)e).Result;
                if (tipo != null)
                {
                    if (tipo.Equals(typeof(Bitmap)))
                    {
                        data = Images.Convert.BytesToBitmap((byte[])data);
                    }
                    else
                    {
                        data = Serialization.ByteArrayToObject((byte[])data);
                    }
                }
            }
            else if (e is System.Net.DownloadStringCompletedEventArgs)
            {
                data = ((DownloadStringCompletedEventArgs)e).Result;

                if (tipo != null)
                {
                    data = Serialization.JSONDecode(data.ToString(), tipo);
                }
            }

            throwEvent(onWebProcessFinished, sender, new WebEventArgs(e.Error, e.Cancelled));
        }
Пример #3
0
        static async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            // Process the message
            //YJYGlobal.LogLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber}");

            try
            {
                var posToClose = Serialization.ByteArrayToObject(message.Body) as PosToClose;
                YJYGlobal.LogLine(
                    $"Received: {posToClose.Id} {posToClose.closeType} {posToClose.closePx} {posToClose.closePxTime}");
                var autoClosePosition = PositionService.AutoClosePosition(posToClose);

                var checkAndCloseFollowPositions = PositionService.CheckAndCloseFollowPositions(autoClosePosition.Id);

                MessageService.AddAutoCloseMessage(autoClosePosition);

                MessageService.AddAutoCloseMessages(checkAndCloseFollowPositions);

                YJYGlobal.LogLine(
                    $"pos closed: {autoClosePosition.Id} type:{autoClosePosition.CloseType} pl:{autoClosePosition.PL}");

                // Complete the message so that it is not received again.
                // This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
                await queueClient.CompleteAsync(message.SystemProperties.LockToken);

                // Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.
                // If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls
                // to avoid unnecessary exceptions.
            }
            catch (Exception e)
            {
                YJYGlobal.LogException(e);

                await queueClient.AbandonAsync(message.SystemProperties.LockToken);
            }
        }
Пример #4
0
 public virtual T OnDeserialize(byte[] data, int length)
 {
     //Note data may has different size than length
     return(Serialization.ByteArrayToObject <T>(data));
 }
Пример #5
0
        public static List <PacketEnvelope> DissasemblePacketData(SocketState state, bool encrypt, string encryptPacketKey, string keySalt)
        {
            List <PacketEnvelope> envelopes = new List <PacketEnvelope>();

            try
            {
                if (state.PayloadBuilderLength + state.BytesReceived >= state.PayloadBuilder.Length)
                {
                    Array.Resize(ref state.PayloadBuilder, state.PayloadBuilderLength + state.BytesReceived);
                }

                Buffer.BlockCopy(state.Buffer, 0, state.PayloadBuilder, state.PayloadBuilderLength, state.BytesReceived);

                state.PayloadBuilderLength = state.PayloadBuilderLength + state.BytesReceived;

                while (state.PayloadBuilderLength > Constants.PayloadHeaderSize) //[PayloadSize] and [CRC16]
                {
                    Byte[] payloadDelimiterBytes = new Byte[4];
                    Byte[] payloadSizeBytes      = new Byte[4];
                    Byte[] expectedCrc16Bytes    = new Byte[2];

                    Buffer.BlockCopy(state.PayloadBuilder, 0, payloadDelimiterBytes, 0, payloadDelimiterBytes.Length);
                    Buffer.BlockCopy(state.PayloadBuilder, 4, payloadSizeBytes, 0, payloadSizeBytes.Length);
                    Buffer.BlockCopy(state.PayloadBuilder, 8, expectedCrc16Bytes, 0, expectedCrc16Bytes.Length);

                    int    payloadDelimiter = BitConverter.ToInt32(payloadDelimiterBytes, 0);
                    int    grossPayloadSize = BitConverter.ToInt32(payloadSizeBytes, 0);
                    UInt16 expectedCrc16    = BitConverter.ToUInt16(expectedCrc16Bytes, 0);

                    if (payloadDelimiter != Constants.PayloadDelimiter)
                    {
                        SkipPacket(ref state);
                        //throw new Exception("Malformed payload packet, invalid delimiter.");
                        continue;
                    }

                    if (grossPayloadSize < Constants.DefaultMinMsgSize || grossPayloadSize > Constants.DefaultMaxMsgSize)
                    {
                        SkipPacket(ref state);
                        //throw new Exception("Malformed payload packet, invalid length.");
                        continue;
                    }

                    if (state.PayloadBuilderLength < grossPayloadSize)
                    {
                        //We have data in the buffer, but it's not enough to make up
                        //  the entire message (fragmented packet) so we will break and wait on more data.
                        break;
                    }

                    UInt16 actualCrc16 = Crc16.ComputeChecksum(state.PayloadBuilder, Constants.PayloadHeaderSize, grossPayloadSize - Constants.PayloadHeaderSize);

                    if (actualCrc16 != expectedCrc16)
                    {
                        SkipPacket(ref state);
                        //throw new Exception("Malformed payload packet, invalid CRC.");
                        continue;
                    }

                    int    netPayloadSize = grossPayloadSize - Constants.PayloadHeaderSize;
                    byte[] payloadBytes   = new byte[netPayloadSize];

                    Buffer.BlockCopy(state.PayloadBuilder, Constants.PayloadHeaderSize, payloadBytes, 0, netPayloadSize);

                    if (encrypt)
                    {
                        payloadBytes = Decrypt(encryptPacketKey, keySalt, payloadBytes);
                    }

                    payloadBytes = Unzip(payloadBytes);

                    envelopes.Add((PacketEnvelope)Serialization.ByteArrayToObject(payloadBytes));

                    //Zero out the consumed portion of the payload buffer - more for fun than anything else.
                    Array.Clear(state.PayloadBuilder, 0, grossPayloadSize);

                    Buffer.BlockCopy(state.PayloadBuilder, grossPayloadSize, state.PayloadBuilder, 0, state.PayloadBuilderLength - grossPayloadSize);
                    state.PayloadBuilderLength -= grossPayloadSize;
                }
            }
            catch (Exception ex)
            {
                //TODO: allow this to be logged.
            }

            return(envelopes);
        }
Пример #6
0
            public override Data OnDeserialize(byte[] data, int length)
            {
                var remote = CompressTool.Decompress(data);

                return(Serialization.ByteArrayToObject <Data>(remote));
            }