diff --git a/src/app/blumen_lumen.cpp b/src/app/blumen_lumen.cpp index caf3147..3525506 100644 --- a/src/app/blumen_lumen.cpp +++ b/src/app/blumen_lumen.cpp @@ -14,19 +14,19 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData) while (true) { -#if 0 - // TODO(pjs): Make this a peek operation - Msg = SocketRecieve(Data->SocketManager, Data->ListenSocket, Ctx->Transient); - if (Msg.Size > 0) + if (SocketPeek(Data->SocketManager, Data->ListenSocket)) { - OutputDebugStringA("Listened"); - Data->MicPacketBuffer->Values[Data->MicPacketBuffer->WriteHead++] = Msg; - if (Data->MicPacketBuffer->WriteHead >= PACKETS_MAX) + // TODO(pjs): Make this a peek operation + Msg = SocketRecieve(Data->SocketManager, Data->ListenSocket, Ctx->Transient); + 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) { @@ -41,7 +41,6 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData) u32 Port = 0; s32 Flags = 0; SocketSend(Data->SocketManager, Data->ListenSocket, Address, Port, Msg, Flags); - OutputDebugStringA("Wrote\n"); } } diff --git a/src/app/platform_win32/win32_foldhaus.cpp b/src/app/platform_win32/win32_foldhaus.cpp index 9877fa3..f0974e3 100644 --- a/src/app/platform_win32/win32_foldhaus.cpp +++ b/src/app/platform_win32/win32_foldhaus.cpp @@ -497,7 +497,7 @@ WinMain ( *Context.ThreadManager = CreatePlatformThreadManager(Win32CreateThread, Win32KillThread); 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); if (!ReloadAndLinkDLL(&DLLRefresh, &Context, &Win32WorkQueue.WorkQueue, true)) { return -1; } diff --git a/src/app/platform_win32/win32_foldhaus_socket.h b/src/app/platform_win32/win32_foldhaus_socket.h index adce909..1795cfe 100644 --- a/src/app/platform_win32/win32_foldhaus_socket.h +++ b/src/app/platform_win32/win32_foldhaus_socket.h @@ -194,6 +194,29 @@ Win32CloseSocket(platform_socket* Socket) 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 Win32SocketReceive(platform_socket* Socket, gs_memory_arena* Storage) { @@ -211,6 +234,8 @@ Win32SocketReceive(platform_socket* Socket, gs_memory_arena* Storage) } return Result; #else + + gs_data Result = PushSizeToData(Storage, 1024); s32 Flags = 0; SOCKET* Win32Sock = (SOCKET*)Socket->PlatformHandle; diff --git a/src/gs_libs/gs_types.cpp b/src/gs_libs/gs_types.cpp index cff4d8c..f51a16d 100644 --- a/src/gs_libs/gs_types.cpp +++ b/src/gs_libs/gs_types.cpp @@ -3359,6 +3359,11 @@ CLOSE_SOCKET(PlatformCloseSocket_Stub) return false; } +SOCKET_PEEK(PlatformSocketPeek_Stub) +{ + return 0; +} + SOCKET_RECEIVE(PlatformSocketRecieve_Stub) { return {}; @@ -3372,12 +3377,14 @@ SOCKET_SEND(PlatformSocketSend_Stub) internal platform_socket_manager CreatePlatformSocketManager(platform_create_socket* CreateSocketProc, platform_close_socket* CloseSocketProc, + platform_socket_peek* SocketPeekProc, platform_socket_receive* SocketRecieveProc, platform_socket_send* SocketSendProc) { platform_socket_manager Result = {}; Result.CreateSocketProc = CreateSocketProc; Result.CloseSocketProc = CloseSocketProc; + Result.SocketPeekProc = SocketPeekProc; Result.SocketRecieveProc = SocketRecieveProc; Result.SocketSendProc = SocketSendProc; @@ -3389,6 +3396,10 @@ CreatePlatformSocketManager(platform_create_socket* CreateSocketProc, { Result.CloseSocketProc = PlatformCloseSocket_Stub; } + if (!SocketPeekProc) + { + Result.SocketPeekProc = PlatformSocketPeek_Stub; + } if (!SocketRecieveProc) { Result.SocketRecieveProc = PlatformSocketRecieve_Stub; @@ -3421,16 +3432,46 @@ CreateSocket(platform_socket_manager* Manager, char* Addr, char* Port) 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 CloseSocket(platform_socket_manager* Manager, platform_socket_handle_ Handle) { bool Result = false; - platform_socket* Socket = &Manager->Sockets[Handle.Index]; - if (Manager->CloseSocketProc(Socket)) + platform_socket* Socket = SocketManagerGetSocket(Manager, Handle); + if (Socket) { - Manager->SocketsUsed[Handle.Index] = false; - *Socket = {}; - Result = true; + if (Manager->CloseSocketProc(Socket)) + { + 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; } @@ -3439,13 +3480,10 @@ internal gs_data SocketRecieve(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle, gs_memory_arena* Storage) { gs_data Result = {}; - if (Manager->SocketsUsed[SocketHandle.Index]) + platform_socket* Socket = SocketManagerGetSocket(Manager, SocketHandle); + if (Socket) { - platform_socket* Socket = &Manager->Sockets[SocketHandle.Index]; - if (Socket->PlatformHandle != 0) - { - Result = Manager->SocketRecieveProc(Socket, Storage); - } + Result = Manager->SocketRecieveProc(Socket, Storage); } 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) { bool Result = false; - if (Manager->SocketsUsed[SocketHandle.Index]) + platform_socket* Socket = SocketManagerGetSocket(Manager, SocketHandle); + if (Socket) { - platform_socket* Socket = &Manager->Sockets[SocketHandle.Index]; - if (Socket->PlatformHandle != 0) - { - s32 SizeSent = Manager->SocketSendProc(Socket, Address, Port, Data, Flags); - Result = (SizeSent == Data.Size); - } + s32 SizeSent = Manager->SocketSendProc(Socket, Address, Port, Data, Flags); + Result = (SizeSent == Data.Size); } return Result; } diff --git a/src/gs_libs/gs_types.h b/src/gs_libs/gs_types.h index 3b29abe..04a64fb 100644 --- a/src/gs_libs/gs_types.h +++ b/src/gs_libs/gs_types.h @@ -1104,6 +1104,13 @@ typedef CREATE_SOCKET(platform_create_socket); #define CLOSE_SOCKET(name) bool name(platform_socket* 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) typedef SOCKET_RECEIVE(platform_socket_receive); @@ -1118,8 +1125,10 @@ typedef struct platform_socket_manager platform_create_socket* CreateSocketProc; platform_close_socket* CloseSocketProc; + platform_socket_peek* SocketPeekProc; platform_socket_receive* SocketRecieveProc; platform_socket_send* SocketSendProc; } platform_socket_manager; + #define GS_TYPES_H #endif // GS_TYPES_H \ No newline at end of file