Ejemplo n.º 1
0
        public static byte[] PackMessage(int msgType, MessageBase msg)
        {
            // reset cached writer length and position
            packWriter.SetLength(0);

            // write message type
            packWriter.Write((short)msgType);

            // serialize message into writer
            msg.Serialize(packWriter);

            // return byte[]
            return(packWriter.ToArray());
        }
        public async UniTask <int> ReceiveAsync(MemoryStream buffer)
        {
            // wait for a message
            await MessageCount.WaitAsync();

            buffer.SetLength(0);
            reader.buffer = writer.ToArraySegment();

            ArraySegment <byte> data = reader.ReadBytesAndSizeSegment();

            if (data.Count == 0)
            {
                throw new EndOfStreamException();
            }

            buffer.SetLength(0);
            buffer.Write(data.Array, data.Offset, data.Count);

            if (reader.Position == reader.Length)
            {
                // if we reached the end of the buffer, reset the buffer to recycle memory
                writer.SetLength(0);
                reader.Position = 0;
            }

            return(0);
        }
Ejemplo n.º 3
0
        public static NetworkWriter GetWriter()
        {
            if (pool.Count != 0)
            {
                NetworkWriter writer = pool.Pop();
                // reset cached writer length and position
                writer.SetLength(0);
                return(writer);
            }

            return(new NetworkWriter());
        }
        public static NetworkWriter GetPooledWriter()
        {
            if (writerPool.Count != 0)
            {
                NetworkWriter writer = writerPool.Pop();
                writer.pooled = false;
                // reset cached writer length and position
                writer.SetLength(0);
                return(writer);
            }

            return(new NetworkWriter(true));
        }
 public void ResetBuffer()
 {
     writer.SetLength(0);
     reader.Position = 0;
 }