forked from Ports/perfect_dark
port: net: move clsettings to a separate message; add join notifications
This commit is contained in:
1 parent
2e195fe21a
commit
a261246e64
7 files changed
+116
-48
No files matched your search
@@ -138,13 +138,15 @@ s32 netStartClient(const char *addr);
|
||||
|
||||
u32 netSend(struct netclient *dstcl, struct netbuf *buf, const s32 reliable, const s32 chan);
|
||||
|
||||
void netChat(const char *text);
|
||||
void netChat(struct netclient *dst, const char *text);
|
||||
void netChatPrintf(struct netclient *dst, const char *fmt, ...);
|
||||
|
||||
void netServerStageStart(void);
|
||||
void netServerStageEnd(void);
|
||||
void netServerKick(struct netclient *cl, const u32 reason);
|
||||
|
||||
void netClientSyncRng(void);
|
||||
void netClientSettingsChanged(void);
|
||||
|
||||
void netPlayersAllocate(void);
|
||||
void netSyncIdsAllocate(void);
|
||||
|
||||
@@ -24,11 +24,12 @@
|
||||
#define SVC_CHR_DAMAGE 0x42 // chr was damaged
|
||||
#define SVC_CHR_DISARM 0x43 // chr's weapons were dropped
|
||||
|
||||
#define CLC_BAD 0x00 // trash
|
||||
#define CLC_NOP 0x01 // does nothing
|
||||
#define CLC_AUTH 0x02 // auth request, sent immediately after connecting
|
||||
#define CLC_CHAT 0x03 // chat message
|
||||
#define CLC_MOVE 0x04 // player input
|
||||
#define CLC_BAD 0x00 // trash
|
||||
#define CLC_NOP 0x01 // does nothing
|
||||
#define CLC_AUTH 0x02 // auth request, sent immediately after connecting
|
||||
#define CLC_CHAT 0x03 // chat message
|
||||
#define CLC_MOVE 0x04 // player input
|
||||
#define CLC_SETTINGS 0x05 // player settings changed
|
||||
|
||||
u32 netmsgClcAuthWrite(struct netbuf *dst);
|
||||
u32 netmsgClcAuthRead(struct netbuf *src, struct netclient *srccl);
|
||||
@@ -36,6 +37,8 @@ u32 netmsgClcChatWrite(struct netbuf *dst, const char *str);
|
||||
u32 netmsgClcChatRead(struct netbuf *src, struct netclient *srccl);
|
||||
u32 netmsgClcMoveWrite(struct netbuf *dst);
|
||||
u32 netmsgClcMoveRead(struct netbuf *src, struct netclient *srccl);
|
||||
u32 netmsgClcSettingsWrite(struct netbuf *dst);
|
||||
u32 netmsgClcSettingsRead(struct netbuf *src, struct netclient *srccl);
|
||||
|
||||
u32 netmsgSvcAuthWrite(struct netbuf *dst, struct netclient *authcl);
|
||||
u32 netmsgSvcAuthRead(struct netbuf *src, struct netclient *srccl);
|
||||
|
||||
@@ -7,6 +7,7 @@ extern "C" {
|
||||
|
||||
#include <PR/ultratypes.h>
|
||||
|
||||
#define LOGFLAG_NOCON (1 << 6)
|
||||
#define LOGFLAG_SHOWMSG (1 << 7)
|
||||
|
||||
enum loglevel {
|
||||
|
||||
+1
-1
@@ -219,7 +219,7 @@ void conTick(void)
|
||||
} else if (key == VK_RETURN) {
|
||||
if (conInput[0] && conInputCol) {
|
||||
if (g_NetMode) {
|
||||
netChat(conInput);
|
||||
netChat(NULL, conInput);
|
||||
}
|
||||
}
|
||||
conInput[0] = '\0';
|
||||
|
||||
+47
-17
@@ -1,5 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include "platform.h"
|
||||
@@ -513,18 +514,18 @@ static void netServerEvConnect(ENetPeer *peer, const u32 data)
|
||||
{
|
||||
const char *addrstr = netFormatPeerAddr(peer);
|
||||
|
||||
sysLogPrintf(LOG_NOTE, "NET: connection attempt from %s", addrstr);
|
||||
sysLogPrintf(LOG_NOTE | LOGFLAG_NOCON, "NET: connection attempt from %s", addrstr);
|
||||
|
||||
++g_NetNumClients;
|
||||
|
||||
if (data != NET_PROTOCOL_VER) {
|
||||
sysLogPrintf(LOG_NOTE, "NET: %s rejected: protocol mismatch", addrstr);
|
||||
sysLogPrintf(LOG_NOTE | LOGFLAG_NOCON, "NET: %s rejected: protocol mismatch", addrstr);
|
||||
enet_peer_disconnect(peer, DISCONNECT_VERSION);
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_NetLocalClient && g_NetLocalClient->state > CLSTATE_LOBBY) {
|
||||
sysLogPrintf(LOG_NOTE, "NET: %s rejected: late joins not allowed", addrstr);
|
||||
sysLogPrintf(LOG_NOTE | LOGFLAG_NOCON, "NET: %s rejected: late joins not allowed", addrstr);
|
||||
enet_peer_disconnect(peer, DISCONNECT_LATE);
|
||||
return;
|
||||
}
|
||||
@@ -540,7 +541,7 @@ static void netServerEvConnect(ENetPeer *peer, const u32 data)
|
||||
}
|
||||
|
||||
if (!cl) {
|
||||
sysLogPrintf(LOG_NOTE, "NET: %s rejected: server is full", addrstr);
|
||||
sysLogPrintf(LOG_NOTE | LOGFLAG_NOCON, "NET: %s rejected: server is full", addrstr);
|
||||
enet_peer_disconnect(peer, DISCONNECT_FULL);
|
||||
return;
|
||||
}
|
||||
@@ -553,14 +554,15 @@ static void netServerEvConnect(ENetPeer *peer, const u32 data)
|
||||
|
||||
static void netServerEvDisconnect(struct netclient *cl)
|
||||
{
|
||||
sysLogPrintf(LOG_NOTE, "NET: disconnect event from %s", netFormatClientAddr(cl));
|
||||
sysLogPrintf(LOG_NOTE | LOGFLAG_NOCON, "NET: disconnect event from %s", netFormatClientAddr(cl));
|
||||
|
||||
if (cl->peer) {
|
||||
enet_peer_reset(cl->peer);
|
||||
}
|
||||
|
||||
if (cl->settings.name) {
|
||||
sysLogPrintf(LOG_CHAT, "NET: %s (%u) disconnected", cl->settings.name, cl->id);
|
||||
if (cl->settings.name[0]) {
|
||||
sysLogPrintf(LOG_NOTE, "NET: client %u (%s) disconnected", cl->id, cl->settings.name);
|
||||
netChatPrintf(NULL, "%s disconnected", cl->settings.name);
|
||||
} else {
|
||||
sysLogPrintf(LOG_CHAT, "NET: client %u disconnected", cl->id);
|
||||
}
|
||||
@@ -582,6 +584,7 @@ static void netServerEvReceive(struct netclient *cl)
|
||||
case CLC_AUTH: rc = netmsgClcAuthRead(&cl->in, cl); break;
|
||||
case CLC_CHAT: rc = netmsgClcChatRead(&cl->in, cl); break;
|
||||
case CLC_MOVE: rc = netmsgClcMoveRead(&cl->in, cl); break;
|
||||
case CLC_SETTINGS: rc = netmsgClcSettingsRead(&cl->in, cl); break;
|
||||
default:
|
||||
rc = 1;
|
||||
break;
|
||||
@@ -589,7 +592,7 @@ static void netServerEvReceive(struct netclient *cl)
|
||||
}
|
||||
|
||||
if (rc) {
|
||||
sysLogPrintf(LOG_WARNING, "NET: malformed or unknown message 0x%02x from client %s", msgid, netFormatClientAddr(cl));
|
||||
sysLogPrintf(LOG_WARNING , "NET: malformed or unknown message 0x%02x from client %u", msgid, cl->id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -602,6 +605,7 @@ static void netClientEvConnect(const u32 data)
|
||||
// send auth request
|
||||
netbufStartWrite(&g_NetMsgRel);
|
||||
netmsgClcAuthWrite(&g_NetMsgRel);
|
||||
netmsgClcSettingsWrite(&g_NetMsgRel);
|
||||
netSend(g_NetLocalClient, &g_NetMsgRel, true, NETCHAN_CONTROL);
|
||||
}
|
||||
|
||||
@@ -655,6 +659,19 @@ void netClientSyncRng(void)
|
||||
}
|
||||
}
|
||||
|
||||
void netClientSettingsChanged(void)
|
||||
{
|
||||
if (g_NetMode != NETMODE_CLIENT || !g_NetLocalClient) {
|
||||
return;
|
||||
}
|
||||
|
||||
netClientReadConfig(g_NetLocalClient, 0);
|
||||
|
||||
netbufStartWrite(&g_NetMsgRel);
|
||||
netmsgClcSettingsWrite(&g_NetMsgRel);
|
||||
netSend(NULL, &g_NetMsgRel, true, NETCHAN_CONTROL);
|
||||
}
|
||||
|
||||
void netStartFrame(void)
|
||||
{
|
||||
if (!g_NetMode) {
|
||||
@@ -886,25 +903,38 @@ void netSyncIdsAllocate(void)
|
||||
sysLogPrintf(LOG_NOTE, "NET: last initial syncid: %u", g_NetNextSyncId);
|
||||
}
|
||||
|
||||
void netChat(const char *text)
|
||||
void netChatPrintf(struct netclient *dst, const char *fmt, ...)
|
||||
{
|
||||
char tmp[1024];
|
||||
char tmp[512];
|
||||
u8 bufdata[600];
|
||||
struct netbuf buf = { NULL };
|
||||
|
||||
if (!g_NetMode || !g_NetLocalClient || g_NetLocalClient->state < CLSTATE_LOBBY) {
|
||||
return;
|
||||
}
|
||||
|
||||
snprintf(tmp, sizeof(tmp), "%s: %s", g_NetLocalClient->settings.name, text);
|
||||
char *nl = strchr(tmp, '\n');
|
||||
if (nl) {
|
||||
*nl = ' ';
|
||||
}
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vsnprintf(tmp, sizeof(tmp) - 1, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
buf.data = bufdata;
|
||||
buf.size = sizeof(bufdata);
|
||||
|
||||
if (g_NetMode == NETMODE_SERVER) {
|
||||
sysLogPrintf(LOG_CHAT, "%s", tmp);
|
||||
netmsgSvcChatWrite(&g_NetMsgRel, tmp);
|
||||
netmsgSvcChatWrite(&buf, tmp);
|
||||
} else {
|
||||
netmsgClcChatWrite(&g_NetMsgRel, tmp);
|
||||
netmsgClcChatWrite(&buf, tmp);
|
||||
}
|
||||
|
||||
netSend(dst, &buf, true, NETCHAN_CONTROL);
|
||||
}
|
||||
|
||||
void netChat(struct netclient *dst, const char *text)
|
||||
{
|
||||
if (g_NetMode && g_NetLocalClient) {
|
||||
netChatPrintf(dst, "%s: %s", g_NetLocalClient->settings.name, text);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+53
-23
@@ -154,15 +154,10 @@ u32 netmsgClcAuthWrite(struct netbuf *dst)
|
||||
}
|
||||
|
||||
netbufWriteU8(dst, CLC_AUTH);
|
||||
netbufWriteStr(dst, g_NetLocalClient->settings.name);
|
||||
netbufWriteStr(dst, g_RomName); // TODO: use a CRC or something
|
||||
netbufWriteStr(dst, modDir);
|
||||
netbufWriteU8(dst, 1); // TODO: number of local players
|
||||
netbufWriteU16(dst, g_NetLocalClient->settings.options);
|
||||
netbufWriteU8(dst, g_NetLocalClient->settings.bodynum);
|
||||
netbufWriteU8(dst, g_NetLocalClient->settings.headnum);
|
||||
netbufWriteF32(dst, g_NetLocalClient->settings.fovy);
|
||||
netbufWriteF32(dst, g_NetLocalClient->settings.fovzoommult);
|
||||
netbufWriteStr(dst, g_NetLocalClient->settings.name);
|
||||
|
||||
return dst->error;
|
||||
}
|
||||
@@ -170,28 +165,23 @@ u32 netmsgClcAuthWrite(struct netbuf *dst)
|
||||
u32 netmsgClcAuthRead(struct netbuf *src, struct netclient *srccl)
|
||||
{
|
||||
if (srccl->state != CLSTATE_AUTH) {
|
||||
sysLogPrintf(LOG_WARNING, "NET: CLC_AUTH from %s, who is not in CLSTATE_AUTH", netFormatClientAddr(srccl));
|
||||
sysLogPrintf(LOG_WARNING, "NET: CLC_AUTH from client %u, who is not in CLSTATE_AUTH", srccl->id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *name = netbufReadStr(src);
|
||||
const char *romName = netbufReadStr(src);
|
||||
const char *modDir = netbufReadStr(src);
|
||||
const u8 players = netbufReadU8(src);
|
||||
const u16 options = netbufReadU16(src);
|
||||
const u8 bodynum = netbufReadU8(src);
|
||||
const u8 headnum = netbufReadU8(src);
|
||||
const f32 fovy = netbufReadF32(src);
|
||||
const f32 fovzoommult = netbufReadF32(src);
|
||||
char *name = netbufReadStr(src);
|
||||
|
||||
if (src->error) {
|
||||
sysLogPrintf(LOG_WARNING, "NET: malformed CLC_AUTH from %s", netFormatClientAddr(srccl));
|
||||
sysLogPrintf(LOG_WARNING, "NET: malformed CLC_AUTH from client %u", srccl->id);
|
||||
netServerKick(srccl, DISCONNECT_KICKED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcasecmp(romName, g_RomName) != 0) {
|
||||
sysLogPrintf(LOG_WARNING, "NET: CLC_AUTH: %s has the wrong ROM, disconnecting", netFormatClientAddr(srccl));
|
||||
sysLogPrintf(LOG_WARNING, "NET: CLC_AUTH: client %u has the wrong ROM, disconnecting", srccl->id);
|
||||
netServerKick(srccl, DISCONNECT_FILES);
|
||||
return src->error;
|
||||
}
|
||||
@@ -202,26 +192,25 @@ u32 netmsgClcAuthRead(struct netbuf *src, struct netclient *srccl)
|
||||
|
||||
const char *myModDir = fsGetModDir();
|
||||
if ((!myModDir != !modDir) || (myModDir && modDir && strcasecmp(modDir, myModDir) != 0)) {
|
||||
sysLogPrintf(LOG_WARNING, "NET: CLC_AUTH: %s has the wrong mod, disconnecting", netFormatClientAddr(srccl));
|
||||
sysLogPrintf(LOG_WARNING, "NET: CLC_AUTH: client %u has the wrong mod, disconnecting", srccl->id);
|
||||
netServerKick(srccl, DISCONNECT_FILES);
|
||||
return src->error;
|
||||
}
|
||||
|
||||
// for now use settings from our own client, remote is supposed to send CLC_SETTINGS after CLC_AUTH
|
||||
srccl->settings = g_NetLocalClient->settings;
|
||||
strncpy(srccl->settings.name, name, sizeof(srccl->settings.name) - 1);
|
||||
srccl->settings.options = options;
|
||||
srccl->settings.bodynum = bodynum;
|
||||
srccl->settings.headnum = headnum;
|
||||
srccl->settings.fovy = fovy;
|
||||
srccl->settings.fovzoommult = fovzoommult;
|
||||
srccl->state = CLSTATE_LOBBY;
|
||||
|
||||
sysLogPrintf(LOG_NOTE, "NET: CLC_AUTH from %s (%s), responding", netFormatClientAddr(srccl), srccl->settings.name);
|
||||
sysLogPrintf(LOG_NOTE, "NET: CLC_AUTH from client %u (%s), responding", srccl->id, srccl->settings.name);
|
||||
|
||||
netbufStartWrite(&srccl->out);
|
||||
netmsgSvcAuthWrite(&srccl->out, srccl);
|
||||
netSend(srccl, NULL, true, NETCHAN_CONTROL);
|
||||
|
||||
sysLogPrintf(LOG_CHAT, "NET: %s (%u) joined", srccl->settings.name, srccl->id);
|
||||
sysLogPrintf(LOG_NOTE, "NET: %s (%u) joined", srccl->settings.name, srccl->id);
|
||||
|
||||
netChatPrintf(NULL, "%s joined", name);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -295,6 +284,47 @@ u32 netmsgClcMoveRead(struct netbuf *src, struct netclient *srccl)
|
||||
return src->error;
|
||||
}
|
||||
|
||||
u32 netmsgClcSettingsWrite(struct netbuf *dst)
|
||||
{
|
||||
netbufWriteU8(dst, CLC_SETTINGS);
|
||||
netbufWriteU16(dst, g_NetLocalClient->settings.options);
|
||||
netbufWriteU8(dst, g_NetLocalClient->settings.bodynum);
|
||||
netbufWriteU8(dst, g_NetLocalClient->settings.headnum);
|
||||
netbufWriteF32(dst, g_NetLocalClient->settings.fovy);
|
||||
netbufWriteF32(dst, g_NetLocalClient->settings.fovzoommult);
|
||||
netbufWriteStr(dst, g_NetLocalClient->settings.name);
|
||||
return dst->error;
|
||||
}
|
||||
|
||||
u32 netmsgClcSettingsRead(struct netbuf *src, struct netclient *srccl)
|
||||
{
|
||||
const u16 options = netbufReadU16(src);
|
||||
const u8 bodynum = netbufReadU8(src);
|
||||
const u8 headnum = netbufReadU8(src);
|
||||
const f32 fovy = netbufReadF32(src);
|
||||
const f32 fovzoommult = netbufReadF32(src);
|
||||
char *name = netbufReadStr(src);
|
||||
|
||||
if (src->error) {
|
||||
sysLogPrintf(LOG_WARNING, "NET: malformed CLC_SETTINGS from client %u", srccl->id);
|
||||
netServerKick(srccl, DISCONNECT_KICKED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (srccl->settings.name[0] && strncmp(srccl->settings.name, name, MAX_PLAYERNAME) != 0) {
|
||||
netChatPrintf(NULL, "%s is now known as %s", srccl->settings.name, name);
|
||||
}
|
||||
|
||||
strncpy(srccl->settings.name, name, sizeof(srccl->settings.name) - 1);
|
||||
srccl->settings.options = options;
|
||||
srccl->settings.bodynum = bodynum;
|
||||
srccl->settings.headnum = headnum;
|
||||
srccl->settings.fovy = fovy;
|
||||
srccl->settings.fovzoommult = fovzoommult;
|
||||
|
||||
return src->error;
|
||||
}
|
||||
|
||||
/* server -> client */
|
||||
|
||||
u32 netmsgSvcAuthWrite(struct netbuf *dst, struct netclient *authcl)
|
||||
|
||||
+3
-1
@@ -128,7 +128,9 @@ void sysLogPrintf(s32 level, const char *fmt, ...)
|
||||
fprintf(fout, "%s%s\n", prefix[level & 0x0f], logmsg);
|
||||
fflush(fout);
|
||||
|
||||
conPrintLn((level & LOGFLAG_SHOWMSG) != 0, logmsg);
|
||||
if ((level & LOGFLAG_NOCON) == 0) {
|
||||
conPrintLn((level & LOGFLAG_SHOWMSG) != 0, logmsg);
|
||||
}
|
||||
}
|
||||
|
||||
void sysFatalError(const char *fmt, ...)
|
||||
|
||||
Reference in new issue
Block a user