public async Task StartReading()
        {
            while (true)
            {
                try {
                    var reader = new PXBinaryReaderAsync(await streamReadyTaskSource.Task);

                    switch (await reader.ReadByte())
                    {
                    case MESSAGE_TYPE_DATA: {
                        ushort id = await reader.ReadUInt16();

                        byte flags = await reader.ReadByte();

                        int length = await reader.ReadInt32();

                        await OnMessageReceived(
                            id,
                            (flags & FLAG_REQUEST) != 0,
                            await reader.ReadBytes(length)
                            );
                    }
                    break;

                    case MESSAGE_TYPE_ACK:
                        OnAcknowledgementReceived(await reader.ReadUInt16());
                        break;

                    case MESSAGE_TYPE_RESPONSE: {
                        ushort id = await reader.ReadUInt16();

                        int length = await reader.ReadInt32();

                        OnResponseReceived(id, await reader.ReadBytes(length));
                    }
                    break;
                    }
                } catch (PXConnectionLostException e) {
                    ReportOnConnectionLost(e.ConnectionStream);
                }

                RemoveOutdatedProcessing();
            }
        }
Esempio n. 2
0
        public async Task <string> WelcomeFromReceiver(Stream stream)
        {
            try {
                stream = new PXExceptionsFilterStream(stream);

                var reader = new PXBinaryReaderAsync(stream);
                var writer = new PXBinaryWriterAsync(stream);

                if (await reader.ReadInt16() == PLLP_VERSION)
                {
                    writer.Write(PLLP_SIGNAL_VERSION_OK);
                    await writer.FlushAsync();
                }
                else
                {
                    writer.Write(PLLP_SIGNAL_INCORRECT_VERSION);
                    await writer.FlushAsync();

                    throw new PLLPVersionIncorrectException();
                }

                Guid id;

                if (await reader.ReadBool())
                {
                    id = (await reader.ReadGuid());
                }
                else
                {
                    id = Guid.NewGuid();
                    writer.Write(id);
                    await writer.FlushAsync();
                }

                return(id.ToString());
            } catch (PLLPVersionIncorrectException) {
                throw;
            } catch (PXConnectionClosedRemoteException) {
                throw;
            } catch (Exception e) {
                throw new PLLPUknownException(e);
            }
        }
        public async Task BasicTypesSerializationTest()
        {
            MemoryStream memory = new MemoryStream();

            var writer = new PXBinaryWriterAsync(memory);

            writer.Write((byte)1);
            writer.Write((short)2);
            writer.Write((ushort)3);
            writer.Write(new byte[] { 1, 2, 3 });
            await writer.FlushAsync();

            var reader = new PXBinaryReaderAsync(new MemoryStream(memory.ToArray()));

            Assert.AreEqual((byte)1, await reader.ReadByte());
            Assert.AreEqual((short)2, await reader.ReadInt16());
            Assert.AreEqual((ushort)3, await reader.ReadUInt16());
            Assert.AreEqual(new byte[] { 1, 2, 3 }, await reader.ReadBytes(3));
        }
Esempio n. 4
0
        public async Task <string> WelcomeFromSender(Stream stream, string reconnectingClientId)
        {
            try {
                stream = new PXExceptionsFilterStream(stream);

                var reader = new PXBinaryReaderAsync(stream);
                var writer = new PXBinaryWriterAsync(stream);

                writer.Write(PLLP_VERSION);
                await writer.FlushAsync();

                if (await reader.ReadInt16() != PLLP_SIGNAL_VERSION_OK)
                {
                    throw new PLLPVersionIncorrectException();
                }

                if (reconnectingClientId != null)
                {
                    writer.Write(true);
                    writer.Write(Guid.Parse(reconnectingClientId));
                    await writer.FlushAsync();

                    return(reconnectingClientId);
                }
                else
                {
                    writer.Write(false);
                    await writer.FlushAsync();

                    return((await reader.ReadGuid()).ToString());
                }
            } catch (PLLPVersionIncorrectException) {
                throw;
            } catch (PXConnectionClosedRemoteException) {
                throw;
            } catch (Exception e) {
                throw new PLLPUknownException(e);
            }
        }