Converting to non-blocking socket calls

This commit is contained in:
Peter Slattery 2021-04-08 14:33:37 -04:00
parent 1b473fad6a
commit bbf07cc6e2
4 changed files with 52 additions and 20 deletions

View File

@ -156,7 +156,7 @@ Win32ConnectSocket(platform_socket_manager* Manager, platform_socket* Socket)
// If iMode == 0, blocking is enabled // If iMode == 0, blocking is enabled
// if iMode != 0, non-blocking mode is enabled // if iMode != 0, non-blocking mode is enabled
u_long iMode = 1; u_long iMode = 0;
Error = ioctlsocket(SocketHandle, FIONBIO, &iMode); Error = ioctlsocket(SocketHandle, FIONBIO, &iMode);
if (Error != NO_ERROR) if (Error != NO_ERROR)
{ {
@ -169,6 +169,7 @@ Win32ConnectSocket(platform_socket_manager* Manager, platform_socket* Socket)
u32 Status = WSAGetLastError(); u32 Status = WSAGetLastError();
if (Status == WSAEWOULDBLOCK) if (Status == WSAEWOULDBLOCK)
{ {
// Non-blocking sockets
#if 0 #if 0
TIMEVAL Timeout = { 0, 500 }; TIMEVAL Timeout = { 0, 500 };
fd_set SocketSet = {}; fd_set SocketSet = {};
@ -244,9 +245,12 @@ Win32SocketPeek(platform_socket_manager* Manager, platform_socket* Socket)
char Temp[4]; char Temp[4];
u32 TempSize = 4; u32 TempSize = 4;
OutputDebugString("Pre Peek"); //OutputDebugString("Pre Peek...");
s32 BytesQueued = recv(*Win32Sock, Temp, TempSize, Flags); //s32 BytesQueued = recv(*Win32Sock, Temp, TempSize, Flags);
OutputDebugString("Post Peek"); u_long BytesQueued = 0;
ioctlsocket(*Win32Sock, FIONREAD, &BytesQueued);
//OutputDebugString("Post Peek\n");
if (BytesQueued != SOCKET_ERROR) if (BytesQueued != SOCKET_ERROR)
{ {
Result = (u32)BytesQueued; Result = (u32)BytesQueued;
@ -257,6 +261,14 @@ Win32SocketPeek(platform_socket_manager* Manager, platform_socket* Socket)
switch (Error) switch (Error)
{ {
case WSAEWOULDBLOCK: case WSAEWOULDBLOCK:
{
// NOTE(PS): This case covers non-blocking sockets
// if we peek and there's nothing there, it returns
// this error code. MSDN says its a non-fatal error
// and the operation should be retried later
Result = 0;
} break;
case WSAENOTCONN: case WSAENOTCONN:
case WSAECONNRESET: case WSAECONNRESET:
case WSAECONNABORTED: case WSAECONNABORTED:
@ -267,7 +279,7 @@ Win32SocketPeek(platform_socket_manager* Manager, platform_socket* Socket)
InvalidDefaultCase; InvalidDefaultCase;
} }
} }
return Result; return (s32)Result;
} }
internal gs_data internal gs_data
@ -315,12 +327,20 @@ Win32SocketSend(platform_socket_manager* Manager, platform_socket* Socket, u32 A
s32 LengthSent = sendto(*Win32Sock, (char*)Data.Memory, Data.Size, Flags, (sockaddr*)&SockAddress, sizeof(sockaddr_in)); s32 LengthSent = sendto(*Win32Sock, (char*)Data.Memory, Data.Size, Flags, (sockaddr*)&SockAddress, sizeof(sockaddr_in));
OutputDebugString("Attempting To Send Network Data: "); //OutputDebugString("Attempting To Send Network Data: ");
if (LengthSent == SOCKET_ERROR) if (LengthSent == SOCKET_ERROR)
{ {
s32 Error = WSAGetLastError(); s32 Error = WSAGetLastError();
switch (Error) switch (Error)
{ {
case WSAEWOULDBLOCK:
{
// NOTE(PS): This covers non-blocking sockets
// In this case the message should be tried again
LengthSent = 0;
//OutputDebugString("Not sent, buffered\n");
}break;
case WSAECONNABORTED: case WSAECONNABORTED:
case WSAENETUNREACH: case WSAENETUNREACH:
case WSAECONNRESET: case WSAECONNRESET:
@ -329,16 +349,12 @@ Win32SocketSend(platform_socket_manager* Manager, platform_socket* Socket, u32 A
if (CloseSocket(Manager, Socket)) if (CloseSocket(Manager, Socket))
{ {
Error = 0; Error = 0;
OutputDebugString("Error\n");
} }
}break; }break;
InvalidDefaultCase; InvalidDefaultCase;
} }
if (Error)
{
OutputDebugString("Error\n");
}
} }
else else
{ {

View File

@ -160,18 +160,22 @@ BlumenLumen_MicListenJob(gs_thread_context* Ctx, u8* UserData)
while (MessageQueue_CanRead(*Data->OutgoingMsgQueue)) while (MessageQueue_CanRead(*Data->OutgoingMsgQueue))
{ {
Msg = MessageQueue_Read(Data->OutgoingMsgQueue); Msg = MessageQueue_Peek(Data->OutgoingMsgQueue);
u32 Address = WeathermanIPV4; u32 Address = WeathermanIPV4;
u32 Port = WeathermanPort; u32 Port = WeathermanPort;
s32 Flags = 0; s32 Flags = 0;
SocketSend(Data->SocketManager, ListenSocket, Address, Port, Msg, Flags); s32 LengthSent = SocketSend(Data->SocketManager, ListenSocket, Address, Port, Msg, Flags);
if (LengthSent != 0)
{
// if we actually sent the message, THEN we pull it off the
// message queue
MessageQueue_Read(Data->OutgoingMsgQueue);
} else {
break;
}
} }
Assert(!MessageQueue_CanRead(*Data->OutgoingMsgQueue));
} }
MessageQueue_Clear(Data->OutgoingMsgQueue);
} }
CloseSocket(Data->SocketManager, ListenSocket); CloseSocket(Data->SocketManager, ListenSocket);

View File

@ -33,7 +33,7 @@ gs_const_string VoicePatternFolder = ConstString("data/blumen_animations/audio_r
// NOTE: There is no need to modify the MotorOpenTimesCount variable - // NOTE: There is no need to modify the MotorOpenTimesCount variable -
// it is a compile time constant that gets calculated automatically // it is a compile time constant that gets calculated automatically
global time_range MotorOpenTimes[] = { global time_range MotorOpenTimes[] = {
{ 08, 45, 17, 45 }, { 12, 45, 17, 45 },
{ 19, 00, 23, 00 }, { 19, 00, 23, 00 },
}; };
global u32 MotorOpenTimesCount = CArrayLength(MotorOpenTimes); // do not edit global u32 MotorOpenTimesCount = CArrayLength(MotorOpenTimes); // do not edit

View File

@ -44,7 +44,7 @@ MessageQueue_CanRead(blumen_network_msg_queue Queue)
} }
internal gs_data internal gs_data
MessageQueue_Read(blumen_network_msg_queue* Queue) MessageQueue_Peek(blumen_network_msg_queue* Queue)
{ {
gs_data Result = {}; gs_data Result = {};
u32 ReadIndex = Queue->ReadHead; u32 ReadIndex = Queue->ReadHead;
@ -53,7 +53,19 @@ MessageQueue_Read(blumen_network_msg_queue* Queue)
ReadIndex = 0; ReadIndex = 0;
} }
Result = Queue->Buffers[ReadIndex]; Result = Queue->Buffers[ReadIndex];
Queue->ReadHead = ReadIndex; return Result;
}
internal gs_data
MessageQueue_Read(blumen_network_msg_queue* Queue)
{
gs_data Result = {};
u32 ReadIndex = Queue->ReadHead++;
if (ReadIndex >= BLUMEN_MESSAGE_QUEUE_COUNT)
{
Queue->ReadHead = 0;
}
Result = Queue->Buffers[ReadIndex];
return Result; return Result;
} }