Skip to content

Commit

Permalink
Merged pull request "Sync with Q2PRO: netchan": #335
Browse files Browse the repository at this point in the history
  • Loading branch information
apanteleev committed Oct 6, 2023
2 parents 0e4f243 + 816be14 commit 3e4802f
Show file tree
Hide file tree
Showing 19 changed files with 400 additions and 535 deletions.
62 changes: 20 additions & 42 deletions inc/common/net/chan.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ typedef struct netchan_s {
int incoming_acknowledged;
int outgoing_sequence;

bool incoming_reliable_acknowledged; // single bit
bool incoming_reliable_sequence; // single bit, maintained local
bool reliable_sequence; // single bit
int last_reliable_sequence; // sequence number of last send
int fragment_sequence;

byte *message_buf; // leave space for header

// message is copied to this buffer when it is first transfered
byte *reliable_buf; // unacked reliable message

sizebuf_t fragment_in;
byte *fragment_in_buf;

sizebuf_t fragment_out;
byte *fragment_out_buf;

// common methods
size_t (*Transmit)(struct netchan_s *, size_t, const void *, int);
size_t (*TransmitNextFragment)(struct netchan_s *);
bool (*Process)(struct netchan_s *);
Expand All @@ -72,51 +90,11 @@ extern cvar_t *net_chantype;
void Netchan_Init(void);
void Netchan_OutOfBand(netsrc_t sock, const netadr_t *adr,
const char *format, ...) q_printf(3, 4);
netchan_t *Netchan_Setup(netsrc_t sock, netchan_type_t type,
const netadr_t *adr, int qport, size_t maxpacketlen, int protocol);
void Netchan_Setup(netchan_t *netchan, netsrc_t sock, netchan_type_t type,
const netadr_t *adr, int qport, size_t maxpacketlen, int protocol);
void Netchan_Close(netchan_t *netchan);

#define OOB_PRINT(sock, addr, data) \
NET_SendPacket(sock, CONST_STR_LEN("\xff\xff\xff\xff" data), addr)

//============================================================================

typedef struct netchan_old_s {
netchan_t pub;

// sequencing variables
int incoming_reliable_acknowledged; // single bit
int incoming_reliable_sequence; // single bit, maintained local
int reliable_sequence; // single bit
int last_reliable_sequence; // sequence number of last send

byte *message_buf; // leave space for header

// message is copied to this buffer when it is first transfered
byte *reliable_buf; // unacked reliable message
} netchan_old_t;

typedef struct netchan_new_s {
netchan_t pub;

// sequencing variables
int incoming_reliable_acknowledged; // single bit
int incoming_reliable_sequence; // single bit, maintained local
int reliable_sequence; // single bit
int last_reliable_sequence; // sequence number of last send
int fragment_sequence;

// reliable staging and holding areas
byte message_buf[MAX_MSGLEN]; // leave space for header

// message is copied to this buffer when it is first transfered
byte reliable_buf[MAX_MSGLEN]; // unacked reliable message

sizebuf_t fragment_in;
byte fragment_in_buf[MAX_MSGLEN];

sizebuf_t fragment_out;
byte fragment_out_buf[MAX_MSGLEN];
} netchan_new_t;

#endif // NET_CHAN_H
2 changes: 1 addition & 1 deletion src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ typedef struct client_static_s {

int quakePort; // a 16 bit value that allows quake servers
// to work around address translating routers
netchan_t *netchan;
netchan_t netchan;
int serverProtocol; // in case we are doing some kind of version hack
int protocolVersion; // minor version

Expand Down
5 changes: 1 addition & 4 deletions src/client/entities.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,7 @@ static void set_active_state(void)
#endif

cl.frameflags = 0;

if (cls.netchan) {
cl.initialSeq = cls.netchan->outgoing_sequence;
}
cl.initialSeq = cls.netchan.outgoing_sequence;

if (cls.demo.playback) {
// init some demo things
Expand Down
41 changes: 20 additions & 21 deletions src/client/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ static void IN_AttackDown(void)
{
KeyDown(&in_attack);

if (cl_instantpacket->integer && cls.state == ca_active && cls.netchan) {
if (cl_instantpacket->integer && cls.state == ca_active && !cls.demo.playback) {
cl.sendPacketNow = true;
}
}
Expand All @@ -389,7 +389,7 @@ static void IN_UseDown(void)
{
KeyDown(&in_use);

if (cl_instantpacket->integer && cls.state == ca_active && cls.netchan) {
if (cl_instantpacket->integer && cls.state == ca_active && !cls.demo.playback) {
cl.sendPacketNow = true;
}
}
Expand Down Expand Up @@ -842,7 +842,7 @@ static inline bool ready_to_send(void)
if (cl.sendPacketNow) {
return true;
}
if (cls.netchan->message.cursize || cls.netchan->reliable_ack_pending) {
if (cls.netchan.message.cursize || cls.netchan.reliable_ack_pending) {
return true;
}
if (!cl_maxpackets->integer) {
Expand Down Expand Up @@ -889,7 +889,7 @@ static void CL_SendDefaultCmd(void)
client_history_t *history;

// archive this packet
history = &cl.history[cls.netchan->outgoing_sequence & CMD_MASK];
history = &cl.history[cls.netchan.outgoing_sequence & CMD_MASK];
history->cmdNumber = cl.cmdNumber;
history->sent = cls.realtime; // for ping calculation
history->rcvd = 0;
Expand All @@ -898,7 +898,7 @@ static void CL_SendDefaultCmd(void)

// see if we are ready to send this packet
if (!ready_to_send_hacked()) {
cls.netchan->outgoing_sequence++; // just drop the packet
cls.netchan.outgoing_sequence++; // just drop the packet
return;
}

Expand Down Expand Up @@ -942,17 +942,17 @@ static void CL_SendDefaultCmd(void)
if (cls.serverProtocol <= PROTOCOL_VERSION_DEFAULT) {
// calculate a checksum over the move commands
msg_write.data[checksumIndex] = COM_BlockSequenceCRCByte(
msg_write.data + checksumIndex + 1,
msg_write.cursize - checksumIndex - 1,
cls.netchan->outgoing_sequence);
msg_write.data + checksumIndex + 1,
msg_write.cursize - checksumIndex - 1,
cls.netchan.outgoing_sequence);
}

P_FRAMES++;

//
// deliver the message
//
cursize = cls.netchan->Transmit(cls.netchan, msg_write.cursize, msg_write.data, 1);
cursize = cls.netchan.Transmit(&cls.netchan, msg_write.cursize, msg_write.data, 1);
#if USE_DEBUG
if (cl_showpackets->integer) {
Com_Printf("%zu ", cursize);
Expand Down Expand Up @@ -983,7 +983,7 @@ static void CL_SendBatchedCmd(void)
}

// archive this packet
seq = cls.netchan->outgoing_sequence;
seq = cls.netchan.outgoing_sequence;
history = &cl.history[seq & CMD_MASK];
history->cmdNumber = cl.cmdNumber;
history->sent = cls.realtime; // for ping calculation
Expand Down Expand Up @@ -1052,7 +1052,7 @@ static void CL_SendBatchedCmd(void)
//
// deliver the message
//
cursize = cls.netchan->Transmit(cls.netchan, msg_write.cursize, msg_write.data, 1);
cursize = cls.netchan.Transmit(&cls.netchan, msg_write.cursize, msg_write.data, 1);
#if USE_DEBUG
if (cl_showpackets->integer == 1) {
Com_Printf("%zu(%i) ", cursize, totalCmds);
Expand All @@ -1072,7 +1072,7 @@ static void CL_SendKeepAlive(void)
size_t cursize q_unused;

// archive this packet
history = &cl.history[cls.netchan->outgoing_sequence & CMD_MASK];
history = &cl.history[cls.netchan.outgoing_sequence & CMD_MASK];
history->cmdNumber = cl.cmdNumber;
history->sent = cls.realtime; // for ping calculation
history->rcvd = 0;
Expand All @@ -1081,7 +1081,7 @@ static void CL_SendKeepAlive(void)
cl.lastTransmitCmdNumber = cl.cmdNumber;
cl.lastTransmitCmdNumberReal = cl.cmdNumber;

cursize = cls.netchan->Transmit(cls.netchan, 0, "", 1);
cursize = cls.netchan.Transmit(&cls.netchan, 0, "", 1);
#if USE_DEBUG
if (cl_showpackets->integer) {
Com_Printf("%zu ", cursize);
Expand All @@ -1100,7 +1100,7 @@ static void CL_SendUserinfo(void)
Com_DDPrintf("%s: %u: full update\n", __func__, com_framenum);
MSG_WriteByte(clc_userinfo);
MSG_WriteData(userinfo, len + 1);
MSG_FlushTo(&cls.netchan->message);
MSG_FlushTo(&cls.netchan.message);
} else if (cls.serverProtocol == PROTOCOL_VERSION_Q2PRO) {
Com_DDPrintf("%s: %u: %d updates\n", __func__, com_framenum,
cls.userinfo_modified);
Expand All @@ -1115,7 +1115,7 @@ static void CL_SendUserinfo(void)
MSG_WriteString(NULL);
}
}
MSG_FlushTo(&cls.netchan->message);
MSG_FlushTo(&cls.netchan.message);
} else {
Com_WPrintf("%s: update count is %d, should never happen.\n",
__func__, cls.userinfo_modified);
Expand All @@ -1129,8 +1129,8 @@ static void CL_SendReliable(void)
cls.userinfo_modified = 0;
}

if (cls.netchan->message.overflowed) {
SZ_Clear(&cls.netchan->message);
if (cls.netchan.message.overflowed) {
SZ_Clear(&cls.netchan.message);
Com_Error(ERR_DROP, "Reliable message overflowed");
}
}
Expand All @@ -1141,9 +1141,8 @@ void CL_SendCmd(void)
return; // not talking to a server
}

// generate usercmds while playing a demo,
// but do not send them
if (!cls.netchan) {
// generate usercmds while playing a demo, but do not send them
if (cls.demo.playback) {
return;
}

Expand All @@ -1152,7 +1151,7 @@ void CL_SendCmd(void)
CL_SendReliable();

// just keepalive or update reliable
if (cls.netchan->ShouldUpdate(cls.netchan)) {
if (cls.netchan.ShouldUpdate(&cls.netchan)) {
CL_SendKeepAlive();
}

Expand Down
Loading

0 comments on commit 3e4802f

Please sign in to comment.