示例#1
0
        public override void WriteTo(IStarboundStream stream)
        {
            stream.WriteUInt8((byte)EntityType);

            using (StarboundStream s = new StarboundStream())
            {
                s.WriteString(Object);
                s.WriteVariant(Information);
                s.WriteUInt8Array(Unknown, false);

                stream.WriteUInt8Array(s.ToArray());
            }

            stream.WriteSignedVLQ(EntityId);
        }
示例#2
0
        public async Task FlushPackets()
        {
            while (PacketQueue.Count > 0)
            {
                try
                {
                    IPacket next;

                    if (!PacketQueue.TryDequeue(out next))
                    {
                        continue;
                    }

                    if (!next.IsReceive)
                    {
                        foreach (var handler in Server.PacketHandlers)
                        {
                            if (next.PacketId == handler.PacketId)
                            {
                                await handler.Handle(next, OtherClient);
                            }
                        }
                    }

                    if (next.Ignore)
                    {
                        continue;
                    }

                    var stream = new StarboundStream();
                    next.Write(stream);

                    byte[] buffer     = stream.ToArray();
                    bool   compressed = stream.Length >= 512;

                    if (compressed)
                    {
                        buffer = ZlibStream.CompressBuffer(buffer);
                    }

                    stream.Dispose();

                    int length = compressed ? -buffer.Length : buffer.Length;

                    var finalStream = new StarboundStream();

                    finalStream.WriteUInt8(next.PacketId);
                    finalStream.WriteSignedVLQ(length);
                    finalStream.Write(buffer, 0, buffer.Length);

                    byte[] toSend = finalStream.ToArray();

                    finalStream.Dispose();

                    if (Socket == null)
                    {
                        return;
                    }

                    var token = new AsyncUserToken();
                    token.Socket = Socket;

                    SocketAsyncEventArgs writeArgs = new SocketAsyncEventArgs();
                    writeArgs.RemoteEndPoint = Socket.RemoteEndPoint;
                    writeArgs.UserToken      = token;
                    writeArgs.SetBuffer(toSend, 0, toSend.Length);
                    writeArgs.Completed += IO_Completed;

                    Socket.SendAsync(writeArgs);
                }
                catch
                {
                    CloseClientSocket(readEventArgs);
                }
            }
        }