From fe02a19f1795429c110e6c7ed76d333cb42a3072 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 21 May 2011 12:25:08 +0300 Subject: [PATCH] Cleaned networking code a bit (had this one on the to-do list for like 4 months already) --- src/connection.cpp | 26 +++++++++++--------------- src/connection.h | 19 +++---------------- src/main.cpp | 9 ++------- src/utility.h | 36 +++++++++++++++++++++++++++++------- 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/connection.cpp b/src/connection.cpp index b07e0de90..548a7f532 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -320,7 +320,7 @@ IncomingSplitBuffer::~IncomingSplitBuffer() This will throw a GotSplitPacketException when a full split packet is constructed. */ -void IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable) +SharedBuffer IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable) { u32 headersize = BASE_HEADER_SIZE + 7; assert(p.data.getSize() >= headersize); @@ -363,9 +363,9 @@ void IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable) // Set chunk data in buffer sp->chunks[chunk_num] = chunkdata; - // If not all chunks are received, return + // If not all chunks are received, return empty buffer if(sp->allReceived() == false) - return; + return SharedBuffer(); // Calculate total size u32 totalsize = 0; @@ -392,8 +392,8 @@ void IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable) // Remove sp from buffer m_buf.remove(seqnum); delete sp; - - throw GotSplitPacketException(fulldata); + + return fulldata; } void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout) { @@ -709,21 +709,17 @@ SharedBuffer Channel::ProcessPacket( con->GetProtocolID(), peer_id, channelnum); - try{ - // Buffer the packet - incoming_splits.insert(packet, reliable); - } - // This exception happens when all the pieces of a packet - // are collected. - catch(GotSplitPacketException &e) + // Buffer the packet + SharedBuffer data = incoming_splits.insert(packet, reliable); + if(data.getSize() != 0) { con->PrintInfo(); dout_con<<"RETURNING TYPE_SPLIT: Constructed full data, " - <<"size="< m_data; -public: - GotSplitPacketException(SharedBuffer data): - m_data(data) - {} - SharedBuffer getData() - { - return m_data; - } -}; - inline u16 readPeerId(u8 *packetdata) { return readU16(&packetdata[4]); @@ -314,10 +301,10 @@ class IncomingSplitBuffer public: ~IncomingSplitBuffer(); /* - This will throw a GotSplitPacketException when a full - split packet is constructed. + Returns a reference counted buffer of length != 0 when a full split + packet is constructed. If not, returns one of length 0. */ - void insert(BufferedPacket &p, bool reliable); + SharedBuffer insert(BufferedPacket &p, bool reliable); void removeUnreliableTimedOuts(float dtime, float timeout); diff --git a/src/main.cpp b/src/main.cpp index 7b33bdb84..202c5e75e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,6 +81,8 @@ SUGG: Calculate lighting per vertex to get a lighting effect like in SUGG: Background music based on cellular automata? http://www.earslap.com/projectslab/otomata +SUGG: Simple light color information to air + Gaming ideas: ------------- @@ -135,8 +137,6 @@ Build system / running: Networking and serialization: ----------------------------- -TODO: Get rid of GotSplitPacketException - User Interface: --------------- @@ -164,11 +164,6 @@ TODO: A setting for enabling bilinear filtering for textures TODO: Better control of draw_control.wanted_max_blocks -TODO: Get player texture (and some others) from the specified texture - directory - -SUGG: Simple light color information to air - TODO: Block mesh generator to tile properly on smooth lighting Configuration: diff --git a/src/utility.h b/src/utility.h index cc8891a07..0df43a31b 100644 --- a/src/utility.h +++ b/src/utility.h @@ -371,10 +371,20 @@ template class SharedBuffer { public: + SharedBuffer() + { + m_size = 0; + data = NULL; + refcount = new unsigned int; + (*refcount) = 1; + } SharedBuffer(unsigned int size) { m_size = size; - data = new T[size]; + if(m_size != 0) + data = new T[m_size]; + else + data = NULL; refcount = new unsigned int; (*refcount) = 1; } @@ -404,8 +414,13 @@ public: SharedBuffer(T *t, unsigned int size) { m_size = size; - data = new T[size]; - memcpy(data, t, size); + if(m_size != 0) + { + data = new T[m_size]; + memcpy(data, t, m_size); + } + else + data = NULL; refcount = new unsigned int; (*refcount) = 1; } @@ -414,9 +429,14 @@ public: */ SharedBuffer(const Buffer &buffer) { - m_size = buffer.m_size; - data = new T[buffer.getSize()]; - memcpy(data, *buffer, buffer.getSize()); + m_size = buffer.getSize(); + if(m_size != 0) + { + data = new T[m_size]; + memcpy(data, *buffer, buffer.getSize()); + } + else + data = NULL; refcount = new unsigned int; (*refcount) = 1; } @@ -426,6 +446,7 @@ public: } T & operator[](unsigned int i) const { + //assert(i < m_size) return data[i]; } T * operator*() const @@ -443,7 +464,8 @@ private: (*refcount)--; if(*refcount == 0) { - delete[] data; + if(data) + delete[] data; delete refcount; } }