public async Task <object> ReadObject()
        {
            //Wait for access
            await semaphoreSlim.WaitAsync();

            if (enabled)
            {
                UdpReceiveResult result;

                //Read object from the stream
                try
                {
                    result = await udpListener.ReceiveAsync();
                }
                catch
                {
                    //Network failure
                    Disable();
                    return(null);
                }
                finally
                {
                    semaphoreSlim.Release();
                }

                if (result.Buffer.Length < TransferStandards.TYPE_PRECEDING_BYTES)
                {
                    return(null);
                }
                byte[] objectData = result.Buffer.Skip(TransferStandards.TYPE_PRECEDING_BYTES).ToArray();
                byte[] typeData   = result.Buffer.Take(TransferStandards.TYPE_PRECEDING_BYTES).ToArray();

                Type specifiedType = TransferStandards.MessageTypes[TransferStandards.ByteSequenceToInteger(typeData)];

                //Attempt deserialization
                try
                {
                    return(Serializer.Deserialize(specifiedType, new MemoryStream(objectData)));
                }
                catch
                {
                    //Serialization failure, likely lost data
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Reads the next object in the network stream.
 /// </summary>
 /// <returns>Returns the object read. Returns null if a connection error occurred while reading.</returns>
 public async Task <object> ReadObject()
 {
     try
     {
         //Read preceding object tag and await specified bytes
         int  specifiedBytes = TransferStandards.ByteSequenceToInteger(await AwaitIncoming(TransferStandards.LENGTH_PRECEDING_BYTES));
         Type specifiedType  = TransferStandards.MessageTypes[TransferStandards.ByteSequenceToInteger(await AwaitIncoming(TransferStandards.TYPE_PRECEDING_BYTES))];
         return(Serializer.Deserialize(specifiedType, new MemoryStream(await AwaitIncoming(specifiedBytes))));
     }
     catch
     {
         //Error while reading object, disable
         Disable();
         return(null);
     }
 }
Beispiel #3
0
        /// <summary>
        /// Sends an object to the network stream.
        /// </summary>
        /// <param name="messageToSend">The object to send to the network stream.</param>
        public async Task <bool> SendMessage(INetworkMessage messageToSend)
        {
            //Serialize object with TCPStandard
            byte[] buffer = TransferStandards.MessageToStandardSequence(messageToSend);

            try
            {
                //Send serialized object
                await networkStream.WriteAsync(buffer, 0, buffer.Length);
            }
            catch
            {
                Disable();
                return(false);
            }

            return(true);
        }
        public async Task <bool> SendMessage(INetworkMessage messageToSend)
        {
            //Convert to standard sequence
            byte[] buffer = TransferStandards.MessageToStandardSequence(messageToSend, sizeData: false, typeData: true);

            try
            {
                //Send serialized object
                await udpSender.SendAsync(buffer, buffer.Length);
            }
            catch
            {
                //Network error
                Disable();
                return(false);
            }

            return(true);
        }