Skip to content

Commit

Permalink
Refactoring network message reading and writing
Browse files Browse the repository at this point in the history
It now uses the reliable Reader and Writer classes.
  • Loading branch information
skyjake committed Aug 16, 2011
1 parent 5be2f3b commit 1893336
Show file tree
Hide file tree
Showing 29 changed files with 614 additions and 953 deletions.
2 changes: 2 additions & 0 deletions doomsday/engine/api/doomsday.h
Expand Up @@ -68,6 +68,8 @@ extern "C" {
#include "dd_share.h"
#include "dd_plugin.h"
#include "smoother.h"
#include "reader.h"
#include "writer.h"

// Base.
void DD_AddIWAD(const char* path);
Expand Down
6 changes: 6 additions & 0 deletions doomsday/engine/api/reader.h
Expand Up @@ -65,6 +65,12 @@ size_t Reader_Pos(const Reader* reader);
*/
size_t Reader_Size(const Reader* reader);

/**
* Determines whether the reader is in the end of buffer, i.e., there is nothing
* more to read.
*/
boolean Reader_AtEnd(const Reader* reader);

/**
* Sets the position of the reading cursor in the buffer.
*
Expand Down
22 changes: 17 additions & 5 deletions doomsday/engine/api/writer.h
Expand Up @@ -46,19 +46,31 @@ Writer* Writer_New(void);
* with Writer_Destruct() after it is not needed any more.
*
* @param buffer Buffer to use for reading.
* @param len Length of the buffer.
* @param maxLen Maximum length of the buffer.
*/
Writer* Writer_NewWithBuffer(byte* buffer, size_t len);
Writer* Writer_NewWithBuffer(byte* buffer, size_t maxLen);

/**
* Destroys the writer.
*/
void Writer_Destruct(Writer* writer);

size_t Writer_Pos(const Writer* writer);

/**
* Returns the current output size of the writer, i.e., how much has been written
* so far.
*/
size_t Writer_Size(const Writer* writer);

/**
* Returns the maximum size of the writing buffer.
*/
size_t Writer_TotalBufferSize(const Writer* writer);

/**
* Returns the number of bytes left for writing.
*/
size_t Writer_BytesLeft(const Writer* writer);

/**
* Sets the position of the writing cursor in the buffer.
*
Expand All @@ -72,7 +84,7 @@ void Writer_WriteByte(Writer* writer, byte v);
void Writer_WriteInt16(Writer* writer, int16_t v);
void Writer_WriteUInt16(Writer* writer, uint16_t v);
void Writer_WriteInt32(Writer* writer, int32_t v);
void Writer_WriteUInt16(Writer* writer, uint16_t v);
void Writer_WriteUInt32(Writer* writer, uint32_t v);
void Writer_WriteFloat(Writer* writer, float v);

/**
Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/include/cl_player.h
Expand Up @@ -42,8 +42,8 @@ typedef struct clplayerstate_s {
angle_t turnDelta;
int friction;
int pendingFixes;
int pendingFixTargetClMobjId;
int pendingAngleFix;
thid_t pendingFixTargetClMobjId;
angle_t pendingAngleFix;
float pendingLookDirFix;
float pendingPosFix[3];
float pendingMomFix[3];
Expand All @@ -55,7 +55,7 @@ extern float cplrThrustMul;
void Cl_InitPlayers(void);
void ClPlayer_MoveLocal(float dx, float dy, float dz, boolean onground);
void ClPlayer_UpdatePos(int plrnum);
void ClPlayer_CoordsReceived(void);
//void ClPlayer_CoordsReceived(void);
void ClPlayer_HandleFix(void);
void ClPlayer_ApplyPendingFixes(int plrNum);
void ClPlayer_ReadDelta2(boolean skip);
Expand Down
2 changes: 2 additions & 0 deletions doomsday/engine/portable/include/de_base.h
Expand Up @@ -42,6 +42,8 @@
#include "dd_loop.h"
#include "dd_help.h"
#include "dd_zip.h"
#include "reader.h"
#include "writer.h"

// System headers needed everywhere.
#include <assert.h>
Expand Down
6 changes: 0 additions & 6 deletions doomsday/engine/portable/include/net_buf.h
Expand Up @@ -29,13 +29,9 @@
#ifndef __DOOMSDAY_NETWORK_BUFFER_H__
#define __DOOMSDAY_NETWORK_BUFFER_H__

#include "con_decl.h"

// Send Packet flags:
#define SPF_REBOUND 0x00020000 // Write only to local loopback
#define SPF_DONT_SEND 0x00040000 // Don't really send out anything
//#define SPF_CONFIRM 0x40000000 // Make sure it's received
//#define SPF_ORDERED 0x80000000 // Send in order & confirm

#define NETBUFFER_MAXDATA (0x7fff - 8) // 32 KB
#define NETBUFFER_ACTUALSIZE 0xffff // 64 KB
Expand Down Expand Up @@ -69,8 +65,6 @@ typedef struct netbuffer_s {
size_t length; // Number of bytes in the data buffer.
size_t headerLength; // 1 byte at the moment.

byte *cursor; // Points into the data buffer.

netdata_t msg; // The data buffer for sending and
// receiving packets.
} netbuffer_t;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/net_main.h
Expand Up @@ -85,7 +85,7 @@ enum {
PCL_ACK_SHAKE = 12,
PSV_SYNC = 13,
//PSV_FILTER = 14, // unused?
PKT_COMMAND = 15, // obsolete
//PKT_COMMAND = 15, // obsolete
PKT_LOGIN = 16,
PCL_ACK_SETS = 17,
PKT_COORDS = 18,
Expand Down
41 changes: 21 additions & 20 deletions doomsday/engine/portable/include/net_msg.h
Expand Up @@ -29,25 +29,26 @@
#ifndef __DOOMSDAY_NETMESSAGE_H__
#define __DOOMSDAY_NETMESSAGE_H__

void Msg_Begin(int type);
void Msg_WriteByte(byte b);
void Msg_WriteShort(short w);
void Msg_WritePackedShort(short w);
void Msg_WriteLong(int l);
void Msg_WritePackedLong(unsigned int l);
void Msg_WriteFloat(float f);
void Msg_Write(const void *src, size_t len);
byte Msg_ReadByte(void);
short Msg_ReadShort(void);
short Msg_ReadPackedShort(void);
unsigned short Msg_ReadUnsignedShort(void);
int Msg_ReadLong(void);
unsigned int Msg_ReadPackedLong(void);
float Msg_ReadFloat(void);
void Msg_Read(void *dest, size_t len);
size_t Msg_Offset(void);
void Msg_SetOffset(size_t offset);
size_t Msg_MemoryLeft(void);
boolean Msg_End(void);
#include "reader.h"
#include "writer.h"

extern Writer* msgWriter;
extern Reader* msgReader;

/**
* Begin writing a message to netBuffer. If a message is currently being
* read, the reading will be ended.
*/
void Msg_Begin(int type);
void Msg_End(void);

boolean Msg_BeingWritten(void);

/**
* Begin reading a message from netBuffer. If a message is currently being
* written, the writing will be ended.
*/
void Msg_BeginRead(void);
void Msg_EndRead(void);

#endif
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/sv_def.h
Expand Up @@ -32,8 +32,8 @@
#include "dd_def.h"
#include "m_string.h"

#define SV_VERSION 14
#define SV_WELCOME_STRING "Doomsday "DOOMSDAY_VERSION_TEXT" Server (R14)"
#define SV_VERSION 15
#define SV_WELCOME_STRING "Doomsday "DOOMSDAY_VERSION_TEXT" Server (R15)"

// Anything closer than this is always taken into consideration when
// deltas are being generated.
Expand Down
10 changes: 5 additions & 5 deletions doomsday/engine/portable/include/sv_pool.h
Expand Up @@ -38,8 +38,8 @@
typedef enum {
DT_MOBJ = 0,
DT_PLAYER = 1,
DT_SECTOR_R6 = 2, // 2 bytes for flags.
DT_SIDE_R6 = 3, // 1 byte for flags.
//DT_SECTOR_R6 = 2, // 2 bytes for flags.
//DT_SIDE_R6 = 3, // 1 byte for flags.
DT_POLY = 4,
DT_LUMP = 5,
DT_SOUND = 6, // No emitter
Expand Down Expand Up @@ -113,8 +113,8 @@ typedef enum {
#define PDF_FRICTION 0x0020
#define PDF_EXTRALIGHT 0x0040 // Plus fixedcolormap (same byte).
#define PDF_FILTER 0x0080
#define PDF_CLYAW 0x1000 // Sent in the player num byte.
#define PDF_CLPITCH 0x2000 // Sent in the player num byte.
//#define PDF_CLYAW 0x1000 // Sent in the player num byte.
//#define PDF_CLPITCH 0x2000 // Sent in the player num byte.
#define PDF_PSPRITES 0x4000 // Sent in the player num byte.

// Written separately, stored in playerdelta flags 2 highest bytes.
Expand All @@ -138,7 +138,7 @@ typedef enum {
#define SDF_CEILING_TARGET 0x00000020
#define SDF_CEILING_SPEED 0x00000040
#define SDF_FLOOR_TEXMOVE 0x00000080
#define SDF_CEILING_TEXMOVE 0x00000100
//#define SDF_CEILING_TEXMOVE 0x00000100 // obsolete
#define SDF_COLOR_RED 0x00000200
#define SDF_COLOR_GREEN 0x00000400
#define SDF_COLOR_BLUE 0x00000800
Expand Down
122 changes: 4 additions & 118 deletions doomsday/engine/portable/src/cl_frame.c
Expand Up @@ -222,7 +222,7 @@ VERBOSE2( Con_Printf("Cl_ConvertSetToOrdinal: Wraparound, now base is %i.\n",
*/
void Cl_Frame2Received(int packetType)
{
byte /*set = Msg_ReadByte(),*/ deltaType;
byte /*set = Reader_ReadByte(msgReader),*/ deltaType;
//byte resendAcks[300];
//int i, numResendAcks = 0;
boolean skip = false;
Expand All @@ -233,7 +233,7 @@ void Cl_Frame2Received(int packetType)
#endif

// The first thing in the frame is the gameTime.
frameGameTime = Msg_ReadFloat();
frameGameTime = Reader_ReadFloat(msgReader);

// All frames that arrive before the first frame are ignored.
// They are most likely from the wrong map.
Expand Down Expand Up @@ -279,82 +279,17 @@ void Cl_Frame2Received(int packetType)
}
#endif

// Check for duplicates (might happen if ack doesn't get through
// or ack arrives too late).
//if(!Cl_HistoryCheck(set))
{
// It isn't yet in the history, so add it there.
//Cl_HistoryAdd(set);

//VERBOSE2( Con_Printf("Starting to process deltas in set %i.\n", set) );

/*
#ifdef _NETDEBUG
deltaCount = Msg_ReadLong();
VERBOSE2( Con_Message("Set contains %i deltas.\n", deltaCount) );
#endif
*/

// Read and process the message.
while(!Msg_End())
while(!Reader_AtEnd(msgReader))
{
#ifdef _NETDEBUG
/*Con_Message("Starting to read delta %i of %i...\n", ++readCount,
deltaCount);*/

// Check length field.
startOffset = Msg_Offset();
deltaLength = Msg_ReadLong();
//Con_Message("Incoming delta length %i bytes.\n", deltaLength);
#endif

deltaType = Msg_ReadByte();
#ifdef _NETDEBUG
//Con_Message(" Delta type is %i.\n", deltaType & ~DT_RESENT);
#endif
deltaType = Reader_ReadByte(msgReader);
skip = false;

VERBOSE2( Con_Printf("Received delta %i.\n", deltaType & ~DT_RESENT) );

/*
// Is this a resent delta?
if(deltaType & DT_RESENT)
{
VERBOSE2( Con_Printf(" This is a resent delta.\n") );
deltaType &= ~DT_RESENT;
// Read the set number this was originally in.
oldSet = Msg_ReadByte();
// Read the resend ID.
resend = Msg_ReadByte();
// Did we already receive this delta?
{
boolean historyChecked = Cl_HistoryCheck(oldSet);
boolean resendChecked = Cl_ResendHistoryCheck(resend);
if(historyChecked || resendChecked)
{
// Yes, we've already got this. Must skip.
skip = true;
Con_Message("CL_Frame2Received: Skipping delta %i (oldset=%i, rsid=%i), because history=%i resend=%i\n",
deltaType, oldSet, resend, historyChecked, resendChecked);
}
}
// We must acknowledge that we've received this.
resendAcks[numResendAcks++] = resend;
Cl_ResendHistoryAdd(resend);
}
else
{
VERBOSE2( Con_Printf(" Not resent.\n") );
}
*/

switch(deltaType)
{
case DT_CREATE_MOBJ:
Expand All @@ -376,7 +311,6 @@ void Cl_Frame2Received(int packetType)
ClPlayer_ReadDelta2(skip);
break;

//case DT_SECTOR_R6: // Old format.
case DT_SECTOR:
Cl_ReadSectorDelta2(deltaType, skip);
break;
Expand All @@ -401,16 +335,6 @@ void Cl_Frame2Received(int packetType)
Con_Error("Cl_Frame2Received: Unknown delta type %i.\n",
deltaType);
}

/*#ifdef _NETDEBUG
// Check that we didn't misread.
if(Msg_Offset() - startOffset != deltaLength)
{
Con_Error("Cl_Frame2Received: Misinterpreted delta! Real length was "
"%i bytes, but we read %i bytes.\n",
deltaLength, Msg_Offset() - startOffset);
}
#endif*/
}

#ifdef _DEBUG
Expand All @@ -422,43 +346,5 @@ void Cl_Frame2Received(int packetType)

// We have now received a frame.
gotFrame = true;

// Reset the predict counter.
//predicted_tics = 0;
}

/*
if(numResendAcks == 0)
{
// Acknowledge the set.
Msg_Begin(PCL_ACK_SETS);
Msg_WriteByte(set);
#ifdef _DEBUG
VERBOSE2( Con_Printf("Cl_Frame2Received: Ack set %i. Nothing was resent.\n", set) );
#endif
}
else
{
// Acknowledge the set and the resent deltas.
Msg_Begin(PCL_ACKS);
Msg_WriteByte(set);
#ifdef _DEBUG
VERBOSE2( Con_Printf("Cl_Frame2Received: Ack set %i. Contained %i resent deltas: \n",
set, numResendAcks) );
#endif
for(i = 0; i < numResendAcks; ++i)
{
Msg_WriteByte(resendAcks[i]);
#ifdef _DEBUG
VERBOSE2( Con_Printf("%i ", resendAcks[i]) );
#endif
}
#ifdef _DEBUG
VERBOSE2( Con_Printf("\n") );
#endif
}
Net_SendBuffer(0, 0);
*/
}

0 comments on commit 1893336

Please sign in to comment.