Added a User Space Cleanup Proc, made BlumenLumen_CustomCleanup end its thread, and implemented a SocketQueryStatus function

This commit is contained in:
Peter Slattery 2021-02-20 13:14:54 -08:00
parent ac19ab97ba
commit 3bc51afe73
10 changed files with 113 additions and 37 deletions

View File

@ -21,38 +21,41 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData)
u32 WeathermanIPV4 = (u32)UpackB4(WeathermanIPAddr); u32 WeathermanIPV4 = (u32)UpackB4(WeathermanIPAddr);
u32 WeathermanPort = 20185; u32 WeathermanPort = 20185;
while (true) while (*Data->Running)
{ {
#if 1 #if 1
// TODO(pjs): Removing this block for now - nothing is wrong with it except that SocketPeek is still blocking for some reason if (SocketQueryStatus(Data->SocketManager, Data->ListenSocket))
if (SocketPeek(Data->SocketManager, Data->ListenSocket))
{ {
// TODO(pjs): Make this a peek operation // TODO(pjs): Removing this block for now - nothing is wrong with it except that SocketPeek is still blocking for some reason
Msg = SocketRecieve(Data->SocketManager, Data->ListenSocket, Ctx->Transient); if (SocketPeek(Data->SocketManager, Data->ListenSocket))
if (Msg.Size > 0)
{ {
Data->MicPacketBuffer->Values[Data->MicPacketBuffer->WriteHead++] = Msg; // TODO(pjs): Make this a peek operation
if (Data->MicPacketBuffer->WriteHead >= PACKETS_MAX) 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 #endif
while (Data->OutgoingMsgQueue->ReadHead != Data->OutgoingMsgQueue->WriteHead)
{
u32 ReadIndex = Data->OutgoingMsgQueue->ReadHead++;
if (Data->OutgoingMsgQueue->ReadHead >= BLUMEN_MESSAGE_QUEUE_COUNT)
{
Data->OutgoingMsgQueue->ReadHead = 0;
}
Msg = Data->OutgoingMsgQueue->Buffers[ReadIndex]; while (Data->OutgoingMsgQueue->ReadHead != Data->OutgoingMsgQueue->WriteHead)
u32 Address = WeathermanIPV4; {
u32 Port = WeathermanPort; u32 ReadIndex = Data->OutgoingMsgQueue->ReadHead++;
s32 Flags = 0; if (Data->OutgoingMsgQueue->ReadHead >= BLUMEN_MESSAGE_QUEUE_COUNT)
SocketSend(Data->SocketManager, Data->ListenSocket, Address, Port, Msg, Flags); {
Data->OutgoingMsgQueue->ReadHead = 0;
}
Msg = Data->OutgoingMsgQueue->Buffers[ReadIndex];
u32 Address = WeathermanIPV4;
u32 Port = WeathermanPort;
s32 Flags = 0;
SocketSend(Data->SocketManager, Data->ListenSocket, Address, Port, Msg, Flags);
}
} }
} }
@ -112,6 +115,9 @@ BlumenLumen_CustomInit(app_state* State, context Context)
Result = PushSizeToData(&State->Permanent, sizeof(blumen_lumen_state)); Result = PushSizeToData(&State->Permanent, sizeof(blumen_lumen_state));
blumen_lumen_state* BLState = (blumen_lumen_state*)Result.Memory; blumen_lumen_state* BLState = (blumen_lumen_state*)Result.Memory;
BLState->Running = true;
BLState->MicListenJobData.Running = &BLState->Running;
BLState->MicListenJobData.SocketManager = Context.SocketManager; BLState->MicListenJobData.SocketManager = Context.SocketManager;
BLState->MicListenJobData.MicPacketBuffer = &BLState->MicPacketBuffer; BLState->MicListenJobData.MicPacketBuffer = &BLState->MicPacketBuffer;
BLState->MicListenJobData.OutgoingMsgQueue = &BLState->OutgoingMsgQueue; BLState->MicListenJobData.OutgoingMsgQueue = &BLState->OutgoingMsgQueue;
@ -208,18 +214,18 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context)
} }
} }
if (MotorTimeElapsed > OpenClosePeriod) if (MotorTimeElapsed > 60)
{ {
// NOTE(pjs): // NOTE(pjs):
MotorTimeElapsed = 0; MotorTimeElapsed = 0;
u8 Position = LastPosition; u8 Position = LastPosition;
if (LastPosition == 2) if (LastPosition == 2)
{ {
LastPosition = ClosedValue; LastPosition = 1;
} }
else else
{ {
LastPosition = OpenValue; LastPosition = 2;
} }
if ((BLState->OutgoingMsgQueue.WriteHead >= BLState->OutgoingMsgQueue.ReadHead) || if ((BLState->OutgoingMsgQueue.WriteHead >= BLState->OutgoingMsgQueue.ReadHead) ||
@ -253,6 +259,12 @@ BlumenLumen_CustomUpdate(gs_data UserData, app_state* State, context* Context)
} }
} }
US_CUSTOM_CLEANUP(BlumenLumen_CustomCleanup)
{
blumen_lumen_state* BLState = (blumen_lumen_state*)UserData.Memory;
BLState->Running = false;
}
internal user_space_desc internal user_space_desc
BlumenLumen_UserSpaceCreate() BlumenLumen_UserSpaceCreate()
{ {
@ -260,6 +272,7 @@ BlumenLumen_UserSpaceCreate()
Result.LoadPatterns = BlumenLumen_LoadPatterns; Result.LoadPatterns = BlumenLumen_LoadPatterns;
Result.CustomInit = BlumenLumen_CustomInit; Result.CustomInit = BlumenLumen_CustomInit;
Result.CustomUpdate = BlumenLumen_CustomUpdate; Result.CustomUpdate = BlumenLumen_CustomUpdate;
Result.CustomCleanup = BlumenLumen_CustomCleanup;
return Result; return Result;
} }

View File

@ -35,6 +35,8 @@ typedef struct blumen_network_msg_queue
// TODO(pjs): Refactor this -> blumen_network_job_state // TODO(pjs): Refactor this -> blumen_network_job_state
struct mic_listen_job_data struct mic_listen_job_data
{ {
bool* Running;
platform_socket_manager* SocketManager; platform_socket_manager* SocketManager;
packet_ringbuffer* MicPacketBuffer; packet_ringbuffer* MicPacketBuffer;
platform_socket_handle_ ListenSocket; platform_socket_handle_ ListenSocket;
@ -44,6 +46,8 @@ struct mic_listen_job_data
struct blumen_lumen_state struct blumen_lumen_state
{ {
bool Running;
packet_ringbuffer MicPacketBuffer; packet_ringbuffer MicPacketBuffer;
blumen_network_msg_queue OutgoingMsgQueue; blumen_network_msg_queue OutgoingMsgQueue;

View File

@ -32,5 +32,15 @@ US_CustomUpdate(user_space_desc* Desc, app_state* State, context* Context)
} }
} }
internal void
US_CustomCleanup(user_space_desc* Desc, app_state* State, context Context)
{
if (Desc->CustomCleanup)
{
Desc->CustomCleanup(Desc->UserData, State, Context);
}
}
#define USERSPACE_CPP #define USERSPACE_CPP
#endif // USERSPACE_CPP #endif // USERSPACE_CPP

View File

@ -14,11 +14,15 @@ typedef US_CUSTOM_INIT(us_custom_init_proc);
#define US_CUSTOM_UPDATE(name) void name(gs_data UserData, app_state* State, context* Context) #define US_CUSTOM_UPDATE(name) void name(gs_data UserData, app_state* State, context* Context)
typedef US_CUSTOM_UPDATE(us_custom_update_proc); typedef US_CUSTOM_UPDATE(us_custom_update_proc);
#define US_CUSTOM_CLEANUP(name) void name(gs_data UserData, app_state* State, context Context)
typedef US_CUSTOM_CLEANUP(us_custom_cleanup_proc);
typedef struct user_space_desc typedef struct user_space_desc
{ {
us_load_patterns_proc* LoadPatterns; us_load_patterns_proc* LoadPatterns;
us_custom_init_proc* CustomInit; us_custom_init_proc* CustomInit;
us_custom_update_proc* CustomUpdate; us_custom_update_proc* CustomUpdate;
us_custom_cleanup_proc* CustomCleanup;
gs_data UserData; gs_data UserData;
} user_space_desc; } user_space_desc;

View File

@ -113,6 +113,7 @@ UPDATE_AND_RENDER(UpdateAndRender)
CLEANUP_APPLICATION(CleanupApplication) CLEANUP_APPLICATION(CleanupApplication)
{ {
app_state* State = (app_state*)Context.MemoryBase; app_state* State = (app_state*)Context.MemoryBase;
US_CustomCleanup(&State->UserSpaceDesc, State, Context);
SACN_Cleanup(&State->SACN, Context); SACN_Cleanup(&State->SACN, Context);
} }

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, Win32SocketPeek, Win32SocketReceive, Win32SocketSend); *Context.SocketManager = CreatePlatformSocketManager(Win32CreateSocket, Win32CloseSocket, Win32SocketQueryStatus, 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; }
@ -600,10 +600,10 @@ WinMain (
Context.CleanupApplication(Context); Context.CleanupApplication(Context);
Win32SocketSystem_Cleanup();
Win32WorkQueue_Cleanup(); Win32WorkQueue_Cleanup();
Win32_TestCode_SocketReading_Cleanup(); //Win32_TestCode_SocketReading_Cleanup();
Win32SocketSystem_Cleanup();
return 0; return 0;
} }

View File

@ -228,6 +228,14 @@ Win32CloseSocket(platform_socket* Socket)
return true; return true;
} }
internal bool
Win32SocketQueryStatus(platform_socket* Socket)
{
SOCKET* Win32Sock = (SOCKET*)Socket->PlatformHandle;
bool Result = (*Win32Sock != INVALID_SOCKET);
return Result;
}
internal u32 internal u32
Win32SocketPeek(platform_socket* Socket) Win32SocketPeek(platform_socket* Socket)
{ {
@ -246,13 +254,14 @@ Win32SocketPeek(platform_socket* Socket)
{ {
// TODO(pjs): Error handling // TODO(pjs): Error handling
s32 Error = WSAGetLastError(); s32 Error = WSAGetLastError();
if (Error == WSAEWOULDBLOCK) switch (Error)
{ {
// NOTE(pjs): case WSAEWOULDBLOCK:
} case WSAENOTCONN:
else {
{ }break;
InvalidCodePath;
InvalidDefaultCase;
} }
} }
return Result; return Result;

View File

@ -236,9 +236,15 @@ Win32WorkQueue_Init(gs_memory_arena* Arena, u32 ThreadCount)
internal void internal void
Win32WorkQueue_Cleanup() Win32WorkQueue_Cleanup()
{ {
u32 Error = 0;
for (u32 Thread = 0; Thread < Win32WorkQueue.ThreadCount; Thread++) for (u32 Thread = 0; Thread < Win32WorkQueue.ThreadCount; Thread++)
{ {
TerminateThread(Win32WorkQueue.Threads[Thread].Handle, 0); u32 Success = TerminateThread(Win32WorkQueue.Threads[Thread].Handle, 0);
if (!Success)
{
Error = GetLastError();
InvalidCodePath;
}
} }
} }

View File

@ -3359,6 +3359,11 @@ CLOSE_SOCKET(PlatformCloseSocket_Stub)
return false; return false;
} }
SOCKET_QUERY_STATUS(PlatformSocketQueryStatus_Stub)
{
return false;
}
SOCKET_PEEK(PlatformSocketPeek_Stub) SOCKET_PEEK(PlatformSocketPeek_Stub)
{ {
return 0; return 0;
@ -3377,6 +3382,7 @@ 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_query_status* SocketQueryStatusProc,
platform_socket_peek* SocketPeekProc, platform_socket_peek* SocketPeekProc,
platform_socket_receive* SocketRecieveProc, platform_socket_receive* SocketRecieveProc,
platform_socket_send* SocketSendProc) platform_socket_send* SocketSendProc)
@ -3384,6 +3390,7 @@ CreatePlatformSocketManager(platform_create_socket* CreateSocketProc,
platform_socket_manager Result = {}; platform_socket_manager Result = {};
Result.CreateSocketProc = CreateSocketProc; Result.CreateSocketProc = CreateSocketProc;
Result.CloseSocketProc = CloseSocketProc; Result.CloseSocketProc = CloseSocketProc;
Result.SocketQueryStatusProc = SocketQueryStatusProc;
Result.SocketPeekProc = SocketPeekProc; Result.SocketPeekProc = SocketPeekProc;
Result.SocketRecieveProc = SocketRecieveProc; Result.SocketRecieveProc = SocketRecieveProc;
Result.SocketSendProc = SocketSendProc; Result.SocketSendProc = SocketSendProc;
@ -3396,6 +3403,10 @@ CreatePlatformSocketManager(platform_create_socket* CreateSocketProc,
{ {
Result.CloseSocketProc = PlatformCloseSocket_Stub; Result.CloseSocketProc = PlatformCloseSocket_Stub;
} }
if (!SocketQueryStatusProc)
{
Result.SocketQueryStatusProc = PlatformSocketQueryStatus_Stub;
}
if (!SocketPeekProc) if (!SocketPeekProc)
{ {
Result.SocketPeekProc = PlatformSocketPeek_Stub; Result.SocketPeekProc = PlatformSocketPeek_Stub;
@ -3464,6 +3475,20 @@ CloseSocket(platform_socket_manager* Manager, platform_socket_handle_ Handle)
return Result; return Result;
} }
// NOTE(pjs): returns true if the socket is connected
// TODO(pjs): make this more descriptive?
internal bool
SocketQueryStatus(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle)
{
bool Result = false;
platform_socket* Socket = SocketManagerGetSocket(Manager, SocketHandle);
if (Socket)
{
Result = Manager->SocketQueryStatusProc(Socket);
}
return Result;
}
internal u32 internal u32
SocketPeek(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle) SocketPeek(platform_socket_manager* Manager, platform_socket_handle_ SocketHandle)
{ {

View File

@ -1104,6 +1104,9 @@ 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_QUERY_STATUS(name) bool name(platform_socket* Socket)
typedef SOCKET_QUERY_STATUS(platform_socket_query_status);
#define SOCKET_PEEK(name) u32 name(platform_socket* Socket) #define SOCKET_PEEK(name) u32 name(platform_socket* Socket)
typedef SOCKET_PEEK(platform_socket_peek); typedef SOCKET_PEEK(platform_socket_peek);
@ -1125,6 +1128,7 @@ typedef struct platform_socket_manager
platform_create_socket* CreateSocketProc; platform_create_socket* CreateSocketProc;
platform_close_socket* CloseSocketProc; platform_close_socket* CloseSocketProc;
platform_socket_query_status* SocketQueryStatusProc;
platform_socket_peek* SocketPeekProc; platform_socket_peek* SocketPeekProc;
platform_socket_receive* SocketRecieveProc; platform_socket_receive* SocketRecieveProc;
platform_socket_send* SocketSendProc; platform_socket_send* SocketSendProc;