protected void SendPacket(object packet, StreamWriter to)
        {
            _writeLock.WaitOne();

            var result = new ByteArrayOutputStream();
            var output = new StreamWriter(result, DotPacket.DefaultBufferSize);

            var(id, data) = _registry.Output(Context.State, packet);

            output.WriteByte(id);
            output.WriteUnsignedShort((ushort)data.Length);
            output.WriteBytes(data);

            try
            {
                to.WriteBytes(result.GetBytes());
            }
            catch (Exception e)
            {
                Close(e);
                throw;
            }

            _writeLock.Set();
        }
        public void SendQueue()
        {
            _queueLock.WaitOne();

            var result = new ByteArrayOutputStream();
            var output = new StreamWriter(result, DotPacket.DefaultBufferSize);

            foreach (var packet in _packetQueue)
            {
                SendPacket(packet, output);
            }

            try
            {
                _out.WriteBytes(result.GetBytes());
            }
            catch (Exception e)
            {
                Close(e);
                throw;
            }

            _packetQueue = new List <object>();

            _queueLock.Set();
        }
        public override byte[] Serialize(object packet)
        {
            var stream = new ByteArrayOutputStream();
            var writer = new StreamWriter(stream, DotPacket.DefaultBufferSize);

            _method.Invoke(packet, new object[] { writer });

            return(stream.GetBytes());
        }
Exemple #4
0
        public override byte[] Serialize(object packet)
        {
            var output = new ByteArrayOutputStream();
            var stream = new StreamWriter(output, DotPacket.DefaultBufferSize);

            foreach (var entry in _serializers)
            {
                entry.Value(stream, entry.Key.GetValue(packet));
            }

            return(output.GetBytes());
        }