Cleaned networking code a bit (had this one on the to-do list for like 4 months already)

This commit is contained in:
Perttu Ahola 2011-05-21 12:25:08 +03:00
parent 3b707b8a4a
commit fe02a19f17
4 changed files with 45 additions and 45 deletions

View File

@ -320,7 +320,7 @@ IncomingSplitBuffer::~IncomingSplitBuffer()
This will throw a GotSplitPacketException when a full This will throw a GotSplitPacketException when a full
split packet is constructed. split packet is constructed.
*/ */
void IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable) SharedBuffer<u8> IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable)
{ {
u32 headersize = BASE_HEADER_SIZE + 7; u32 headersize = BASE_HEADER_SIZE + 7;
assert(p.data.getSize() >= headersize); assert(p.data.getSize() >= headersize);
@ -363,9 +363,9 @@ void IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable)
// Set chunk data in buffer // Set chunk data in buffer
sp->chunks[chunk_num] = chunkdata; 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) if(sp->allReceived() == false)
return; return SharedBuffer<u8>();
// Calculate total size // Calculate total size
u32 totalsize = 0; u32 totalsize = 0;
@ -392,8 +392,8 @@ void IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable)
// Remove sp from buffer // Remove sp from buffer
m_buf.remove(seqnum); m_buf.remove(seqnum);
delete sp; delete sp;
throw GotSplitPacketException(fulldata); return fulldata;
} }
void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout) void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout)
{ {
@ -709,21 +709,17 @@ SharedBuffer<u8> Channel::ProcessPacket(
con->GetProtocolID(), con->GetProtocolID(),
peer_id, peer_id,
channelnum); channelnum);
try{ // Buffer the packet
// Buffer the packet SharedBuffer<u8> data = incoming_splits.insert(packet, reliable);
incoming_splits.insert(packet, reliable); if(data.getSize() != 0)
}
// This exception happens when all the pieces of a packet
// are collected.
catch(GotSplitPacketException &e)
{ {
con->PrintInfo(); con->PrintInfo();
dout_con<<"RETURNING TYPE_SPLIT: Constructed full data, " dout_con<<"RETURNING TYPE_SPLIT: Constructed full data, "
<<"size="<<e.getData().getSize()<<std::endl; <<"size="<<data.getSize()<<std::endl;
return e.getData(); return data;
} }
con->PrintInfo(); con->PrintInfo();
dout_con<<"BUFFERING TYPE_SPLIT"<<std::endl; dout_con<<"BUFFERED TYPE_SPLIT"<<std::endl;
throw ProcessedSilentlyException("Buffered a split packet chunk"); throw ProcessedSilentlyException("Buffered a split packet chunk");
} }
else if(type == TYPE_RELIABLE) else if(type == TYPE_RELIABLE)

View File

@ -99,19 +99,6 @@ public:
{} {}
}; };
class GotSplitPacketException
{
SharedBuffer<u8> m_data;
public:
GotSplitPacketException(SharedBuffer<u8> data):
m_data(data)
{}
SharedBuffer<u8> getData()
{
return m_data;
}
};
inline u16 readPeerId(u8 *packetdata) inline u16 readPeerId(u8 *packetdata)
{ {
return readU16(&packetdata[4]); return readU16(&packetdata[4]);
@ -314,10 +301,10 @@ class IncomingSplitBuffer
public: public:
~IncomingSplitBuffer(); ~IncomingSplitBuffer();
/* /*
This will throw a GotSplitPacketException when a full Returns a reference counted buffer of length != 0 when a full split
split packet is constructed. packet is constructed. If not, returns one of length 0.
*/ */
void insert(BufferedPacket &p, bool reliable); SharedBuffer<u8> insert(BufferedPacket &p, bool reliable);
void removeUnreliableTimedOuts(float dtime, float timeout); void removeUnreliableTimedOuts(float dtime, float timeout);

View File

@ -81,6 +81,8 @@ SUGG: Calculate lighting per vertex to get a lighting effect like in
SUGG: Background music based on cellular automata? SUGG: Background music based on cellular automata?
http://www.earslap.com/projectslab/otomata http://www.earslap.com/projectslab/otomata
SUGG: Simple light color information to air
Gaming ideas: Gaming ideas:
------------- -------------
@ -135,8 +137,6 @@ Build system / running:
Networking and serialization: Networking and serialization:
----------------------------- -----------------------------
TODO: Get rid of GotSplitPacketException
User Interface: 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: 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 TODO: Block mesh generator to tile properly on smooth lighting
Configuration: Configuration:

View File

@ -371,10 +371,20 @@ template <typename T>
class SharedBuffer class SharedBuffer
{ {
public: public:
SharedBuffer()
{
m_size = 0;
data = NULL;
refcount = new unsigned int;
(*refcount) = 1;
}
SharedBuffer(unsigned int size) SharedBuffer(unsigned int size)
{ {
m_size = 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 = new unsigned int;
(*refcount) = 1; (*refcount) = 1;
} }
@ -404,8 +414,13 @@ public:
SharedBuffer(T *t, unsigned int size) SharedBuffer(T *t, unsigned int size)
{ {
m_size = size; m_size = size;
data = new T[size]; if(m_size != 0)
memcpy(data, t, size); {
data = new T[m_size];
memcpy(data, t, m_size);
}
else
data = NULL;
refcount = new unsigned int; refcount = new unsigned int;
(*refcount) = 1; (*refcount) = 1;
} }
@ -414,9 +429,14 @@ public:
*/ */
SharedBuffer(const Buffer<T> &buffer) SharedBuffer(const Buffer<T> &buffer)
{ {
m_size = buffer.m_size; m_size = buffer.getSize();
data = new T[buffer.getSize()]; if(m_size != 0)
memcpy(data, *buffer, buffer.getSize()); {
data = new T[m_size];
memcpy(data, *buffer, buffer.getSize());
}
else
data = NULL;
refcount = new unsigned int; refcount = new unsigned int;
(*refcount) = 1; (*refcount) = 1;
} }
@ -426,6 +446,7 @@ public:
} }
T & operator[](unsigned int i) const T & operator[](unsigned int i) const
{ {
//assert(i < m_size)
return data[i]; return data[i];
} }
T * operator*() const T * operator*() const
@ -443,7 +464,8 @@ private:
(*refcount)--; (*refcount)--;
if(*refcount == 0) if(*refcount == 0)
{ {
delete[] data; if(data)
delete[] data;
delete refcount; delete refcount;
} }
} }