Esempio n. 1
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);
            }
        }
Esempio n. 2
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);
            }
        }
        private async Task SendAckMessage(ushort id)
        {
            this.processing[id].UpdateResponse(null);

            try {
                var writer = new PXBinaryWriterAsync(await this.streamReadyTaskSource.Task);
                writer.Write(MESSAGE_TYPE_ACK);
                writer.Write(id);
                await writer.FlushAsync();
            } catch (PXConnectionLostException e) {
                ReportOnConnectionLost(e.ConnectionStream);

                await SendAckMessage(id);
            }
        }
        public async Task SendResponse(ushort messageId, byte[] response)
        {
            this.processing[messageId].UpdateResponse(response);

            try {
                var writer = new PXBinaryWriterAsync(await streamReadyTaskSource.Task);
                writer.Write(MESSAGE_TYPE_RESPONSE);
                writer.Write(messageId);
                writer.Write(response.Length);
                writer.Write(response);
                await writer.FlushAsync();
            } catch (PXConnectionLostException e) {
                ReportOnConnectionLost(e.ConnectionStream);

                await SendResponse(messageId, response);
            }
        }
        private async Task <ushort> SendMessageInternal(MessageData message)
        {
            try {
                var writer = new PXBinaryWriterAsync(await streamReadyTaskSource.Task);
                writer.Write(MESSAGE_TYPE_DATA);
                writer.Write(message.id);
                writer.Write(message.responseWaiter != null ? FLAG_REQUEST : (byte)0);
                writer.Write(message.data.Length);
                writer.Write(message.data);
                await writer.FlushAsync();
            } catch (PXConnectionLostException e) {
                ReportOnConnectionLost(e.ConnectionStream);

                await SendMessageInternal(message);
            }

            return(message.id);
        }
        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));
        }