1
0
cuberite-2a/src/ByteBuffer.h

161 lines
5.9 KiB
C
Raw Normal View History

// ByteStream.h
// Interfaces to the cByteBuffer class representing a ringbuffer of bytes
#pragma once
2017-08-25 08:43:18 -04:00
// fwd:
class cUUID;
/** An object that can store incoming bytes and lets its clients read the bytes sequentially
The bytes are stored in a ringbuffer of constant size; if more than that size
is requested, the write operation fails.
The bytes stored can be retrieved using various ReadXXX functions; these assume that the needed
number of bytes are present in the buffer (ASSERT; for performance reasons).
The reading doesn't actually remove the bytes, it only moves the internal read ptr.
To remove the bytes, call CommitRead().
To re-start reading from the beginning, call ResetRead().
This class doesn't implement thread safety, the clients of this class need to provide
their own synchronization.
*/
class cByteBuffer
{
public:
cByteBuffer(size_t a_BufferSize);
~cByteBuffer();
2016-02-05 16:45:45 -05:00
/** Writes the bytes specified to the ringbuffer. Returns true if successful, false if not */
bool Write(const void * a_Bytes, size_t a_Count);
2016-02-05 16:45:45 -05:00
/** Returns the number of bytes that can be successfully written to the ringbuffer */
size_t GetFreeSpace(void) const;
2016-02-05 16:45:45 -05:00
/** Returns the number of bytes that are currently in the ringbuffer. Note GetReadableBytes() */
size_t GetUsedSpace(void) const;
2016-02-05 16:45:45 -05:00
/** Returns the number of bytes that are currently available for reading (may be less than UsedSpace due to some data having been read already) */
size_t GetReadableSpace(void) const;
2016-02-05 16:45:45 -05:00
/** Returns the current data start index. For debugging purposes. */
2014-03-09 14:21:42 -04:00
size_t GetDataStart(void) const { return m_DataStart; }
2016-02-05 16:45:45 -05:00
/** Returns true if the specified amount of bytes are available for reading */
2014-03-07 14:04:25 -05:00
bool CanReadBytes(size_t a_Count) const;
/** Returns true if the specified amount of bytes are available for writing */
2014-03-07 14:04:25 -05:00
bool CanWriteBytes(size_t a_Count) const;
// Read the specified datatype and advance the read pointer; return true if successfully read:
bool ReadBEInt8 (Int8 & a_Value);
bool ReadBEInt16 (Int16 & a_Value);
bool ReadBEInt32 (Int32 & a_Value);
bool ReadBEInt64 (Int64 & a_Value);
bool ReadBEUInt8 (UInt8 & a_Value);
bool ReadBEUInt16 (UInt16 & a_Value);
bool ReadBEUInt32 (UInt32 & a_Value);
bool ReadBEUInt64 (UInt64 & a_Value);
bool ReadBEFloat (float & a_Value);
bool ReadBEDouble (double & a_Value);
bool ReadBool (bool & a_Value);
bool ReadVarInt32 (UInt32 & a_Value);
bool ReadVarInt64 (UInt64 & a_Value);
bool ReadVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8
bool ReadLEInt (int & a_Value);
2020-07-20 04:56:27 -04:00
bool ReadXYZPosition64 (int & a_BlockX, int & a_BlockY, int & a_BlockZ);
bool ReadXZYPosition64 (int & a_BlockX, int & a_BlockY, int & a_BlockZ);
2017-08-25 08:43:18 -04:00
bool ReadUUID (cUUID & a_Value);
/** Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned short, char, Byte, double, ...) */
template <typename T> bool ReadVarInt(T & a_Value)
{
UInt64 v;
bool res = ReadVarInt64(v);
if (res)
{
a_Value = static_cast<T>(v);
}
return res;
}
// Write the specified datatype; return true if successfully written
bool WriteBEInt8 (Int8 a_Value);
bool WriteBEInt16 (Int16 a_Value);
bool WriteBEInt32 (Int32 a_Value);
bool WriteBEInt64 (Int64 a_Value);
bool WriteBEUInt8 (UInt8 a_Value);
bool WriteBEUInt16 (UInt16 a_Value);
bool WriteBEUInt32 (UInt32 a_Value);
bool WriteBEUInt64 (UInt64 a_Value);
bool WriteBEFloat (float a_Value);
bool WriteBEDouble (double a_Value);
bool WriteBool (bool a_Value);
bool WriteVarInt32 (UInt32 a_Value);
bool WriteVarInt64 (UInt64 a_Value);
bool WriteVarUTF8String (const AString & a_Value); // string length as VarInt, then string as UTF-8
bool WriteLEInt32 (Int32 a_Value);
2020-07-20 04:56:27 -04:00
bool WriteXYZPosition64 (Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ);
bool WriteXZYPosition64 (Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ);
2016-02-05 16:45:45 -05:00
/** Reads a_Count bytes into a_Buffer; returns true if successful */
2014-03-07 14:04:25 -05:00
bool ReadBuf(void * a_Buffer, size_t a_Count);
2016-02-05 16:45:45 -05:00
/** Writes a_Count bytes into a_Buffer; returns true if successful */
2014-03-07 14:04:25 -05:00
bool WriteBuf(const void * a_Buffer, size_t a_Count);
2016-02-05 16:45:45 -05:00
/** Reads a_Count bytes into a_String; returns true if successful */
2014-03-07 14:04:25 -05:00
bool ReadString(AString & a_String, size_t a_Count);
2016-02-05 16:45:45 -05:00
/** Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer */
2014-03-07 14:04:25 -05:00
bool SkipRead(size_t a_Count);
2016-02-05 16:45:45 -05:00
/** Reads all available data into a_Data */
void ReadAll(AString & a_Data);
2016-02-05 16:45:45 -05:00
/** Reads the specified number of bytes and writes it into the destinatio bytebuffer. Returns true on success. */
2013-12-22 09:19:29 -05:00
bool ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes);
2016-02-05 16:45:45 -05:00
/** Removes the bytes that have been read from the ringbuffer */
void CommitRead(void);
2016-02-05 16:45:45 -05:00
/** Restarts next reading operation at the start of the ringbuffer */
void ResetRead(void);
2016-02-05 16:45:45 -05:00
/** Re-reads the data that has been read since the last commit to the current readpos. Used by ProtoProxy to duplicate communication */
void ReadAgain(AString & a_Out);
2016-02-05 16:45:45 -05:00
/** Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs */
void CheckValid(void) const;
1.9 / 1.9.2 / 1.9.3 / 1.9.4 protocol support (#3135) * Semistable update to 15w31a I'm going through snapshots in a sequential order since it should make things easier, and since protocol version history is written. * Update to 15w34b protocol Also, fix an issue with the Entity Equipment packet from the past version. Clients are able to connect and do stuff! * Partially update to 15w35e Chunk data doesn't work, but the client joins. I'm waiting to do chunk data because chunk data has an incomplete format until 15w36d. * Add '/blk' debug command This command lets one see what block they are looking at, and makes figuring out what's supposed to be where in a highly broken chunk possible. * Fix CRLF normalization in CheckBasicStyle.lua Normally, this doesn't cause an issue, but when running from cygwin, it detects the CR as whitespace and creates thousands of violations for every single line. Lua, when run on windows, will normalize automatically, but when run via cygwin, it won't. The bug was simply that gsub was returning a replaced version, but not changing the parameter, so the replaced version was ignored. * Update to 15w40b This includes chunk serialization. Fully functional chunk serialization for 1.9. I'm not completely happy with the chunk serialization as-is (correct use of palettes would be great), but cuberite also doesn't skip sending empty chunks so this performance optimization should probably come later. The creation of a full buffer is suboptimal, but it's the easiest way to implement this code. * Write long-by-long rather than creating a buffer This is a bit faster and should be equivalent. However, the code still doesn't look too good. * Update to 15w41a protocol This includes the new set passengers packet, which works off of the ridden entity, not the rider. That means, among other things, that information about the previously ridden vehicle is needed when detaching. So a new method with that info was added. * Update to 15w45a * 15w51b protocol * Update to 1.9.0 protocol Closes #3067. There are still a few things that need to be worked out (picking up items, effects, particles, and most importantly inventory), but in general this should work. I'll make a few more changes tomorrow to get the rest of the protocol set up, along with 1.9.1/1.9.2 (which did make a few changes). Chunks, however, _are_ working, along with most other parts of the game (placing/breaking blocks). * Fix item pickup packet not working That was a silly mistake, but at least it was an easy one. * 1.9.2 protocol support * Fix version info found in server list ping Thus, the client reports that it can connect rather than saying that the server is out of date. This required creating separate classes for 1.9.1 and 1.9.2, unfortunately. * Fix build errors generated by clang These didn't happen in MSVC. * Add protocol19x.cpp and protocol19x.h to CMakeLists * Ignore warnings in protocol19x that are ignored in protocol18x * Document BLOCK_FACE and DIG_STATUS constants * Fix BLOCK_FACE links and add separate section for DIG_STATUS * Fix bat animation and object spawning The causes of both of these are explained in #3135, but the gist is that both were typos. * Implement Use Item packet This means that buckets, bows, fishing rods, and several other similar items now work when not looking at a block. * Handle DIG_STATUS_SWAP_ITEM_IN_HAND * Add support for spawn eggs and potions The items are transformed from the 1.9 version to the 1.8 version when reading and transformed back when sending. * Remove spammy potion debug logging * Fix wolf collar color metadata The wrong type was being used, causing several clientside issues (including the screen going black). * Fix 1.9 chunk sending in the nether The nether and the end don't send skylight. * Fix clang build errors * Fix water bottles becoming mundane potions This happened because the can become splash potion bit got set incorrectly. Water bottles and mundane potions are only differentiated by the fact that water bottles have a metadata of 0, so setting that bit made it a mundane potion. Also add missing break statements to the read item NBT switch, which would otherwise break items with custom names and also cause incorrect "Unimplemented NBT data when parsing!" logging. * Copy Protocol18x as Protocol19x Aditionally, method and class names have been swapped to clean up other diffs. This commit is only added to make the following diffs more readable; it doesn't make any other changes (beyond class names). * Make thrown potions use the correct appearence This was caused by potions now using metadata. * Add missing api doc for cSplashPotionEntity::GetItem * Fix compile error in SplashPotionEntity.cpp * Fix fix of cSplashPotionEntity API doc * Temporarilly disable fall damage particles These were causing issues in 1.9 due to the changed effect ID. * Properly send a kick packet when connecting with an invalid version This means that the client no longer waits on the server screen with no indication whatsoever. However, right now the server list ping isn't implemented for unknown versions, so it'll only load "Old" on the ping. I also added a GetVarIntSize method to cByteBuffer. This helps clean up part of the code here (and I think it could clean up other parts), but it may make sense for it to be moved elsewhere (or declared in a different way). * Handle server list pings from unrecognized versions This isn't the cleanest way of writing it (it feels odd to use ProtocolRecognizer to send packets, and the addition of m_InPingForUnrecognizedVersion feels like the wrong technique), but it works and I can't think of a better way (apart from creating a full separate protocol class to handle only the ping... which would be worse). * Use cPacketizer for the disconnect packet This also should fix clang build errors. * Add 1.9.3 / 1.9.4 support * Fix incorrect indentation in APIDesc
2016-05-14 15:12:42 -04:00
/** Gets the number of bytes that are needed to represent the given VarInt */
static size_t GetVarIntSize(UInt32 a_Value);
protected:
char * m_Buffer;
2014-03-07 14:04:25 -05:00
size_t m_BufferSize; // Total size of the ringbuffer
2016-02-05 16:45:45 -05:00
2014-03-07 14:04:25 -05:00
size_t m_DataStart; // Where the data starts in the ringbuffer
size_t m_WritePos; // Where the data ends in the ringbuffer
size_t m_ReadPos; // Where the next read will start in the ringbuffer
#ifdef _DEBUG
/** The ID of the thread currently accessing the object.
Used for checking that only one thread accesses the object at a time, via cSingleThreadAccessChecker. */
mutable std::thread::id m_ThreadID;
#endif
2016-02-05 16:45:45 -05:00
/** Advances the m_ReadPos by a_Count bytes */
2014-03-07 14:04:25 -05:00
void AdvanceReadPos(size_t a_Count);
} ;