Compressed some of the socket layer code and added a path to peek for messages

This commit is contained in:
PS 2021-01-30 14:25:58 -08:00
parent 84854b2ad8
commit 751ee2e27a
5 changed files with 97 additions and 29 deletions

View File

@ -14,19 +14,19 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData)
while (true) while (true)
{ {
#if 0 if (SocketPeek(Data->SocketManager, Data->ListenSocket))
// TODO(pjs): Make this a peek operation
Msg = SocketRecieve(Data->SocketManager, Data->ListenSocket, Ctx->Transient);
if (Msg.Size > 0)
{ {
OutputDebugStringA("Listened"); // TODO(pjs): Make this a peek operation
Data->MicPacketBuffer->Values[Data->MicPacketBuffer->WriteHead++] = Msg; Msg = SocketRecieve(Data->SocketManager, Data->ListenSocket, Ctx->Transient);
if (Data->MicPacketBuffer->WriteHead >= PACKETS_MAX) if (Msg.Size > 0)
{ {
Data->MicPacketBuffer->WriteHead = 0; Data->MicPacketBuffer->Values[Data->MicPacketBuffer->WriteHead++] = Msg;
if (Data->MicPacketBuffer->WriteHead >= PACKETS_MAX)
{
Data->MicPacketBuffer->WriteHead = 0;
}
} }
} }
#endif
while (Data->OutgoingMsgQueue->ReadHead != Data->OutgoingMsgQueue->WriteHead) while (Data->OutgoingMsgQueue->ReadHead != Data->OutgoingMsgQueue->WriteHead)
{ {
@ -41,7 +41,6 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData)
u32 Port = 0; u32 Port = 0;
s32 Flags = 0; s32 Flags = 0;
SocketSend(Data->SocketManager, Data->ListenSocket, Address, Port, Msg, Flags); SocketSend(Data->SocketManager, Data->ListenSocket, Address, Port, Msg, Flags);
OutputDebugStringA("Wrote\n");
} }
} }

View File

@ -497,7 +497,7 @@ WinMain (
*Context.ThreadManager = CreatePlatformThreadManager(Win32CreateThread, Win32KillThread); *Context.ThreadManager = CreatePlatformThreadManager(Win32CreateThread, Win32KillThread);
Context.SocketManager = PushStruct(&PlatformPermanent, platform_socket_manager); Context.SocketManager = PushStruct(&PlatformPermanent, platform_socket_manager);
*Context.SocketManager = CreatePlatformSocketManager(Win32CreateSocket, Win32CloseSocket, Win32SocketReceive, Win32SocketSend); *Context.SocketManager = CreatePlatformSocketManager(Win32CreateSocket, Win32CloseSocket, Win32SocketPeek, Win32SocketReceive, Win32SocketSend);
win32_dll_refresh DLLRefresh = InitializeDLLHotReloading(DLLName, WorkingDLLName, DLLLockFileName); win32_dll_refresh DLLRefresh = InitializeDLLHotReloading(DLLName, WorkingDLLName, DLLLockFileName);
if (!ReloadAndLinkDLL(&DLLRefresh, &Context, &Win32WorkQueue.WorkQueue, true)) { return -1; } if (!ReloadAndLinkDLL(&DLLRefresh, &Context, &Win32WorkQueue.WorkQueue, true)) { return -1; }

View File

@ -194,6 +194,29 @@ Win32CloseSocket(platform_socket* Socket)
return true; return true;
} }
internal u32
Win32SocketPeek(platform_socket* Socket)
{
u32 Result = 0;
s32 Flags = MSG_PEEK;
SOCKET* Win32Sock = (SOCKET*)Socket->PlatformHandle;
char Temp[4];
u32 TempSize = 4;
s32 BytesQueued = recv(*Win32Sock, Temp, TempSize, Flags);
if (BytesQueued != SOCKET_ERROR)
{
Result = (u32)BytesQueued;
}
else
{
// TODO(pjs): Error handling
s32 Error = WSAGetLastError();
InvalidCodePath;
}
return Result;
}
internal gs_data internal gs_data
Win32SocketReceive(platform_socket* Socket, gs_memory_arena* Storage) Win32SocketReceive(platform_socket* Socket, gs_memory_arena* Storage)
{ {
@ -211,6 +234,8 @@ Win32SocketReceive(platform_socket* Socket, gs_memory_arena* Storage)
} }
return Result; return Result;
#else #else
gs_data Result = PushSizeToData(Storage, 1024); gs_data Result = PushSizeToData(Storage, 1024);
s32 Flags = 0; s32 Flags = 0;
SOCKET* Win32Sock = (SOCKET*)Socket->PlatformHandle; SOCKET* Win32Sock = (SOCKET*)Socket->PlatformHandle;

View File

@ -3359,6 +3359,11 @@ CLOSE_SOCKET(PlatformCloseSocket_Stub)
return false; return false;
} }
SOCKET_PEEK(PlatformSocketPeek_Stub)
{
return 0;
}
SOCKET_RECEIVE(PlatformSocketRecieve_Stub) SOCKET_RECEIVE(PlatformSocketRecieve_Stub)
{ {
return {}; return {};
@ -3372,12 +3377,14 @@ SOCKET_SEND(PlatformSocketSend_Stub)
internal platform_socket_manager internal platform_socket_manager
CreatePlatformSocketManager(platform_create_socket* CreateSocketProc, CreatePlatformSocketManager(platform_create_socket* CreateSocketProc,
platform_close_socket* CloseSocketProc, platform_close_socket* CloseSocketProc,
platform_socket_peek* SocketPeekProc,
platform_socket_receive* SocketRecieveProc, platform_socket_receive* SocketRecieveProc,
platform_socket_send* SocketSendProc) platform_socket_send* SocketSendProc)
{ {
platform_socket_manager Result = {}; platform_socket_manager Result = {};
Result.CreateSocketProc = CreateSocketProc; Result.CreateSocketProc = CreateSocketProc;
Result.CloseSocketProc = CloseSocketProc; Result.CloseSocketProc = CloseSocketProc;
Result.SocketPeekProc = SocketPeekProc;
Result.SocketRecieveProc = SocketRecieveProc; Result.SocketRecieveProc = SocketRecieveProc;
Result.SocketSendProc = SocketSendProc; Result.SocketSendProc = SocketSendProc;
@ -3389,6 +3396,10 @@ CreatePlatformSocketManager(platform_create_socket* CreateSocketProc,
{ {
Result.CloseSocketProc = PlatformCloseSocket_Stub; Result.CloseSocketProc = PlatformCloseSocket_Stub;
} }
if (!SocketPeekProc)
{
Result.SocketPeekProc = PlatformSocketPeek_Stub;
}
if (!SocketRecieveProc) if (!SocketRecieveProc)
{ {
Result.SocketRecieveProc = PlatformSocketRecieve_Stub; Result.SocketRecieveProc = PlatformSocketRecieve_Stub;
@ -3421,16 +3432,46 @@ CreateSocket(platform_socket_manager* Manager, char* Addr, char* Port)
return Result; return Result;
} }
internal platform_socket*
SocketManagerGetSocket(platform_socket_manager* Manager, platform_socket_handle_ Handle)
{
platform_socket* Result = 0;
if (Manager->SocketsUsed[Handle.Index])
{
platform_socket* Socket = &Manager->Sockets[Handle.Index];
if (Socket->PlatformHandle != 0)
{
Result = Socket;
}
}
return Result;
}
internal bool internal bool
CloseSocket(platform_socket_manager* Manager, platform_socket_handle_ Handle) CloseSocket(platform_socket_manager* Manager, platform_socket_handle_ Handle)
{ {
bool Result = false; bool Result = false;
platform_socket* Socket = &Manager->Sockets[Handle.Index]; platform_socket* Socket = SocketManagerGetSocket(Manager, Handle);
if (Manager->CloseSocketProc(Socket)) if (Socket)
{ {
Manager->SocketsUsed[Handle.Index] = false; if (Manager->CloseSocketProc(Socket))
*Socket = {}; {
Result = true; Manager->SocketsUsed[Handle.Index] = false;
*Socket = {};
Result = true;
}
}
return Result;
}
internal u32
SocketPeek(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle)
{
u32 Result = 0;
platform_socket* Socket = SocketManagerGetSocket(Manager, SocketHandle);
if (Socket)
{
Result = Manager->SocketPeekProc(Socket);
} }
return Result; return Result;
} }
@ -3439,13 +3480,10 @@ internal gs_data
SocketRecieve(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle, gs_memory_arena* Storage) SocketRecieve(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle, gs_memory_arena* Storage)
{ {
gs_data Result = {}; gs_data Result = {};
if (Manager->SocketsUsed[SocketHandle.Index]) platform_socket* Socket = SocketManagerGetSocket(Manager, SocketHandle);
if (Socket)
{ {
platform_socket* Socket = &Manager->Sockets[SocketHandle.Index]; Result = Manager->SocketRecieveProc(Socket, Storage);
if (Socket->PlatformHandle != 0)
{
Result = Manager->SocketRecieveProc(Socket, Storage);
}
} }
return Result; return Result;
} }
@ -3454,14 +3492,11 @@ internal bool
SocketSend(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle, u32 Address, u32 Port, gs_data Data, s32 Flags) SocketSend(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle, u32 Address, u32 Port, gs_data Data, s32 Flags)
{ {
bool Result = false; bool Result = false;
if (Manager->SocketsUsed[SocketHandle.Index]) platform_socket* Socket = SocketManagerGetSocket(Manager, SocketHandle);
if (Socket)
{ {
platform_socket* Socket = &Manager->Sockets[SocketHandle.Index]; s32 SizeSent = Manager->SocketSendProc(Socket, Address, Port, Data, Flags);
if (Socket->PlatformHandle != 0) Result = (SizeSent == Data.Size);
{
s32 SizeSent = Manager->SocketSendProc(Socket, Address, Port, Data, Flags);
Result = (SizeSent == Data.Size);
}
} }
return Result; return Result;
} }

View File

@ -1104,6 +1104,13 @@ typedef CREATE_SOCKET(platform_create_socket);
#define CLOSE_SOCKET(name) bool name(platform_socket* Socket) #define CLOSE_SOCKET(name) bool name(platform_socket* Socket)
typedef CLOSE_SOCKET(platform_close_socket); typedef CLOSE_SOCKET(platform_close_socket);
#define SOCKET_PEEK(name) u32 name(platform_socket* Socket)
typedef SOCKET_PEEK(platform_socket_peek);
// TODO(pjs): allow for a size parameter that can be zero
// if provided, that is how big the message it expects to be
// if it equals zero, the proc will peek at the message first to determine
// the needed size
#define SOCKET_RECEIVE(name) gs_data name(platform_socket* Socket, gs_memory_arena* Storage) #define SOCKET_RECEIVE(name) gs_data name(platform_socket* Socket, gs_memory_arena* Storage)
typedef SOCKET_RECEIVE(platform_socket_receive); typedef SOCKET_RECEIVE(platform_socket_receive);
@ -1118,8 +1125,10 @@ typedef struct platform_socket_manager
platform_create_socket* CreateSocketProc; platform_create_socket* CreateSocketProc;
platform_close_socket* CloseSocketProc; platform_close_socket* CloseSocketProc;
platform_socket_peek* SocketPeekProc;
platform_socket_receive* SocketRecieveProc; platform_socket_receive* SocketRecieveProc;
platform_socket_send* SocketSendProc; platform_socket_send* SocketSendProc;
} platform_socket_manager; } platform_socket_manager;
#define GS_TYPES_H #define GS_TYPES_H
#endif // GS_TYPES_H #endif // GS_TYPES_H