public void Write(StarboundStream stream) { stream.Write(Data, 0, Data.Length); }
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); } } }