Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 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);
     }
 }