1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-18 09:13:14 +00:00

1. Fixed thread HANDLE leak on Windows KVM

2. Swapped semaphore with spin lock on certain objects
This commit is contained in:
Bryan Roe
2020-11-30 17:20:22 -08:00
parent 78c553d3ad
commit 8a5610c2d8
7 changed files with 164 additions and 231 deletions

View File

@@ -239,7 +239,8 @@ void KVM_StopMessagePump()
if (CUR_HWND != NULL) if (CUR_HWND != NULL)
{ {
PostMessageA(CUR_HWND, WM_QUIT, 0, 0); PostMessageA(CUR_HWND, WM_QUIT, 0, 0);
WaitForSingleObject(CUR_WORKTHREAD, 5000); if (WaitForSingleObject(CUR_WORKTHREAD, 5000) == 0) { CloseHandle(CUR_WORKTHREAD); CUR_WORKTHREAD = NULL; }
if (CUR_APCTHREAD != NULL) { CloseHandle(CUR_APCTHREAD); CUR_APCTHREAD = NULL; }
} }
} }

View File

@@ -559,23 +559,7 @@ typedef struct kvm_data_handler
char buffer[]; char buffer[];
}kvm_data_handler; }kvm_data_handler;
//void __stdcall kvm_relay_feeddata_ex_APC(ULONG_PTR data)
//{
// kvm_data_handler *k = (kvm_data_handler*)data;
//
// k->handler(k->buffer, k->len, k->reserved);
// free((void*)data);
//}
//ILibTransport_DoneState kvm_relay_feeddata_ex(char *buf, int len, void *reserved)
//{
// kvm_data_handler *data = (kvm_data_handler*)ILibMemory_Allocate(sizeof(kvm_data_handler) + len, 0, NULL, NULL);
// data->handler = (ILibKVM_WriteHandler)((void**)reserved)[0];
// data->reserved = ((void**)reserved)[1];
// data->len = len;
// memcpy_s(data->buffer, len, buf, len);
//
// QueueUserAPC((PAPCFUNC)kvm_relay_feeddata_ex_APC, kvmthread, (ULONG_PTR)data);
//}
// Feed network data into the KVM. Return the number of bytes consumed. // Feed network data into the KVM. Return the number of bytes consumed.
// This method consumes as many input commands as it can. // This method consumes as many input commands as it can.
@@ -798,6 +782,7 @@ DWORD WINAPI kvm_server_mainloop_ex(LPVOID parm)
{ {
g_shutdown = 0; g_shutdown = 0;
kvmthread = CreateThread(NULL, 0, kvm_mainloopinput, parm, 0, 0); kvmthread = CreateThread(NULL, 0, kvm_mainloopinput, parm, 0, 0);
CloseHandle(kvmthread);
} }
#endif #endif
@@ -979,7 +964,6 @@ DWORD WINAPI kvm_server_mainloop_ex(LPVOID parm)
KVMDEBUG("kvm_server_mainloop / end3", (int)GetCurrentThreadId()); KVMDEBUG("kvm_server_mainloop / end3", (int)GetCurrentThreadId());
KVMDEBUG("kvm_server_mainloop / end2", (int)GetCurrentThreadId()); KVMDEBUG("kvm_server_mainloop / end2", (int)GetCurrentThreadId());
// if (kvmthread != NULL) { CloseHandle(kvmthread); kvmthread = NULL; }
if (tileInfo != NULL) { if (tileInfo != NULL) {
for (row = 0; row < TILE_HEIGHT_COUNT; row++) free(tileInfo[row]); for (row = 0; row < TILE_HEIGHT_COUNT; row++) free(tileInfo[row]);
free(tileInfo); free(tileInfo);
@@ -1187,6 +1171,7 @@ int kvm_relay_setup(char *exePath, void *processPipeMgr, ILibKVM_WriteHandler wr
if (ThreadRunning == 1 && g_shutdown == 0) { KVMDEBUG("kvm_relay_setup() session already exists", 0); free(parms); return 0; } if (ThreadRunning == 1 && g_shutdown == 0) { KVMDEBUG("kvm_relay_setup() session already exists", 0); free(parms); return 0; }
kvmthread = CreateThread(NULL, 0, kvm_server_mainloop, (void*)parms, 0, 0); kvmthread = CreateThread(NULL, 0, kvm_server_mainloop, (void*)parms, 0, 0);
CloseHandle(kvmthread);
return 1; return 1;
} }
} }

View File

@@ -413,7 +413,6 @@ void ILibAsyncSocket_Destroy(void *socketModule)
#ifndef MICROSTACK_NOTLS #ifndef MICROSTACK_NOTLS
module->SSLConnect = 0; module->SSLConnect = 0;
#endif #endif
sem_destroy(&(module->SendLock));
} }
/*! \fn ILibAsyncSocket_SetReAllocateNotificationCallback(ILibAsyncSocket_SocketModule AsyncSocketToken, ILibAsyncSocket_OnBufferReAllocated Callback) /*! \fn ILibAsyncSocket_SetReAllocateNotificationCallback(ILibAsyncSocket_SocketModule AsyncSocketToken, ILibAsyncSocket_OnBufferReAllocated Callback)
\brief Set the callback handler for when the internal data buffer has been resized \brief Set the callback handler for when the internal data buffer has been resized
@@ -476,7 +475,7 @@ ILibAsyncSocket_SocketModule ILibCreateAsyncSocketModuleWithMemory(void *Chain,
RetVal->MallocSize = initialBufferSize; RetVal->MallocSize = initialBufferSize;
RetVal->LifeTime = ILibGetBaseTimer(Chain); //ILibCreateLifeTime(Chain); RetVal->LifeTime = ILibGetBaseTimer(Chain); //ILibCreateLifeTime(Chain);
sem_init(&(RetVal->SendLock), 0, 1); ILibSpinLock_Init(&(RetVal->SendLock));
ILibAddToChain(Chain, RetVal); ILibAddToChain(Chain, RetVal);
@@ -513,17 +512,15 @@ void ILibAsyncSocket_SendError(ILibAsyncSocket_SocketModule socketModule)
module->PAUSE = 1; module->PAUSE = 1;
ILibAsyncSocket_ClearPendingSend(module); // This causes a segfault ILibAsyncSocket_ClearPendingSend(module); // This causes a segfault
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_SendError", 1, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
// Ensure Calling On_Disconnect with MicroStackThread // Ensure Calling On_Disconnect with MicroStackThread
ILibLifeTime_Add(module->LifeTime, socketModule, 0, &ILibAsyncSocket_Disconnect, NULL); ILibLifeTime_Add(module->LifeTime, socketModule, 0, &ILibAsyncSocket_Disconnect, NULL);
SEM_TRACK(AsyncSocket_TrackLock("ILibAsyncSocket_SendError", 1, module);) ILibSpinLock_Lock(&(module->SendLock));
sem_wait(&(module->SendLock));
} }
sem_t *ILibAsyncSocket_GetSendLock(ILibAsyncSocket_SocketModule socketModule) ILibSpinLock *ILibAsyncSocket_GetSpinLock(ILibAsyncSocket_SocketModule socketModule)
{ {
return(&((struct ILibAsyncSocketModule*)socketModule)->SendLock); return(&((struct ILibAsyncSocketModule*)socketModule)->SendLock);
} }
@@ -558,7 +555,7 @@ ILibAsyncSocket_SendStatus ILibAsyncSocket_SendTo_MultiWrite(ILibAsyncSocket_Soc
#ifndef MICROSTACK_NOTLS #ifndef MICROSTACK_NOTLS
if (module->ssl != NULL) if (module->ssl != NULL)
{ {
if (lockOverride == 0) { sem_wait(&(module->SendLock)); } if (lockOverride == 0) { ILibSpinLock_Lock(&(module->SendLock)); }
for (vi = 0; vi < count; ++vi) for (vi = 0; vi < count; ++vi)
{ {
@@ -657,14 +654,14 @@ ILibAsyncSocket_SendStatus ILibAsyncSocket_SendTo_MultiWrite(ILibAsyncSocket_Soc
ILibAsyncSocket_SendError(module); ILibAsyncSocket_SendError(module);
} }
if (lockOverride == 0) { sem_post(&(module->SendLock)); } if (lockOverride == 0) { ILibSpinLock_UnLock(&(module->SendLock)); }
if (retVal != ILibAsyncSocket_ALL_DATA_SENT && !ILibIsRunningOnChainThread(module->Transport.ChainLink.ParentChain)) ILibForceUnBlockChain(module->Transport.ChainLink.ParentChain); if (retVal != ILibAsyncSocket_ALL_DATA_SENT && !ILibIsRunningOnChainThread(module->Transport.ChainLink.ParentChain)) ILibForceUnBlockChain(module->Transport.ChainLink.ParentChain);
return retVal; return retVal;
} }
#endif #endif
// If we got here, we aren't doing TLS // If we got here, we aren't doing TLS
if (lockOverride == 0) { sem_wait(&(module->SendLock)); } if (lockOverride == 0) { ILibSpinLock_Lock(&(module->SendLock)); }
if (module->internalSocket == ~0) if (module->internalSocket == ~0)
{ {
// Too Bad, the socket closed // Too Bad, the socket closed
@@ -675,7 +672,7 @@ ILibAsyncSocket_SendStatus ILibAsyncSocket_SendTo_MultiWrite(ILibAsyncSocket_Soc
UserFree = va_arg(vlist, ILibAsyncSocket_MemoryOwnership); UserFree = va_arg(vlist, ILibAsyncSocket_MemoryOwnership);
if (UserFree == ILibAsyncSocket_MemoryOwnership_CHAIN) { free(buffer); } if (UserFree == ILibAsyncSocket_MemoryOwnership_CHAIN) { free(buffer); }
} }
if (lockOverride == 0) { sem_post(&(module->SendLock)); } if (lockOverride == 0) { ILibSpinLock_UnLock(&(module->SendLock)); }
va_end(vlist); va_end(vlist);
return ILibAsyncSocket_SEND_ON_CLOSED_SOCKET_ERROR; return ILibAsyncSocket_SEND_ON_CLOSED_SOCKET_ERROR;
} }
@@ -781,7 +778,7 @@ ILibAsyncSocket_SendStatus ILibAsyncSocket_SendTo_MultiWrite(ILibAsyncSocket_Soc
} }
va_end(vlist); va_end(vlist);
if (lockOverride == 0) { sem_post(&(module->SendLock)); } if (lockOverride == 0) { ILibSpinLock_UnLock(&(module->SendLock)); }
if (notok != 0) if (notok != 0)
{ {
retVal = ILibAsyncSocket_BUFFER_TOO_LARGE; retVal = ILibAsyncSocket_BUFFER_TOO_LARGE;
@@ -812,8 +809,7 @@ void ILibAsyncSocket_Disconnect(ILibAsyncSocket_SocketModule socketModule)
ILibRemoteLogging_printf(ILibChainGetLogger(module->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_Microstack_AsyncSocket, ILibRemoteLogging_Flags_VerbosityLevel_1, "AsyncSocket[%p] << DISCONNECT", (void*)module); ILibRemoteLogging_printf(ILibChainGetLogger(module->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_Microstack_AsyncSocket, ILibRemoteLogging_Flags_VerbosityLevel_1, "AsyncSocket[%p] << DISCONNECT", (void*)module);
SEM_TRACK(AsyncSocket_TrackLock("ILibAsyncSocket_Disconnect", 1, module);) ILibSpinLock_Lock(&(module->SendLock));
sem_wait(&(module->SendLock));
module->timeout_handler = NULL; module->timeout_handler = NULL;
module->timeout_milliSeconds = 0; module->timeout_milliSeconds = 0;
@@ -823,9 +819,9 @@ void ILibAsyncSocket_Disconnect(ILibAsyncSocket_SocketModule socketModule)
{ {
SSL_TRACE1("ILibAsyncSocket_Disconnect()"); SSL_TRACE1("ILibAsyncSocket_Disconnect()");
SSL_shutdown(module->ssl); SSL_shutdown(module->ssl);
sem_post(&(module->SendLock)); ILibSpinLock_UnLock(&(module->SendLock));
SSL_free(module->ssl); // Frees SSL session and both BIO buffers at the same time SSL_free(module->ssl); // Frees SSL session and both BIO buffers at the same time
sem_wait(&(module->SendLock)); ILibSpinLock_Lock(&(module->SendLock));
module->ssl = NULL; module->ssl = NULL;
SSL_TRACE2("ILibAsyncSocket_Disconnect()"); SSL_TRACE2("ILibAsyncSocket_Disconnect()");
} }
@@ -852,8 +848,7 @@ void ILibAsyncSocket_Disconnect(ILibAsyncSocket_SocketModule socketModule)
// Since the socket is closing, we need to clear the data that is pending to be sent // Since the socket is closing, we need to clear the data that is pending to be sent
ILibAsyncSocket_ClearPendingSend(socketModule); ILibAsyncSocket_ClearPendingSend(socketModule);
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_Disconnect", 2, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
#ifndef MICROSTACK_NOTLS #ifndef MICROSTACK_NOTLS
if (wasssl == NULL) if (wasssl == NULL)
@@ -879,8 +874,7 @@ void ILibAsyncSocket_Disconnect(ILibAsyncSocket_SocketModule socketModule)
} }
else else
{ {
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_Disconnect", 3, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
} }
} }
@@ -1089,7 +1083,7 @@ ILibAsyncSocket_SendStatus ILibAsyncSocket_ProcessEncryptedBuffer(ILibAsyncSocke
ILibAsyncSocket_SendData *data; ILibAsyncSocket_SendData *data;
ILibAsyncSocket_SendStatus retVal = ILibAsyncSocket_SEND_ON_CLOSED_SOCKET_ERROR; ILibAsyncSocket_SendStatus retVal = ILibAsyncSocket_SEND_ON_CLOSED_SOCKET_ERROR;
sem_wait(&(Reader->SendLock)); ILibSpinLock_Lock(&(Reader->SendLock));
if (Reader->writeBioBuffer->length > 0) if (Reader->writeBioBuffer->length > 0)
{ {
if (Reader->PendingSend_Tail == NULL) if (Reader->PendingSend_Tail == NULL)
@@ -1136,7 +1130,7 @@ ILibAsyncSocket_SendStatus ILibAsyncSocket_ProcessEncryptedBuffer(ILibAsyncSocke
// Don't need to do anything, becuase it'll get picked up in the PostSelect // Don't need to do anything, becuase it'll get picked up in the PostSelect
} }
} }
sem_post(&(Reader->SendLock)); ILibSpinLock_UnLock(&(Reader->SendLock));
return retVal; return retVal;
} }
#endif #endif
@@ -1388,9 +1382,9 @@ void ILibProcessAsyncSocket(struct ILibAsyncSocketModule *Reader, int pendingRea
if (Reader->ssl != NULL) if (Reader->ssl != NULL)
{ {
SSL_free(Reader->ssl); // Frees SSL session and BIO buffer at the same time SSL_free(Reader->ssl); // Frees SSL session and BIO buffer at the same time
sem_wait(&(Reader->SendLock)); ILibSpinLock_Lock(&(Reader->SendLock));
Reader->ssl = NULL; Reader->ssl = NULL;
sem_post(&(Reader->SendLock)); ILibSpinLock_UnLock(&(Reader->SendLock));
} }
#endif #endif
@@ -1532,8 +1526,7 @@ void ILibAsyncSocket_PreSelect(void* socketModule,fd_set *readset, fd_set *write
ILibRemoteLogging_printf(ILibChainGetLogger(module->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_Microstack_AsyncSocket, ILibRemoteLogging_Flags_VerbosityLevel_5, "AsyncSocket[%p] entered PreSelect", (void*)module); ILibRemoteLogging_printf(ILibChainGetLogger(module->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_Microstack_AsyncSocket, ILibRemoteLogging_Flags_VerbosityLevel_5, "AsyncSocket[%p] entered PreSelect", (void*)module);
SEM_TRACK(AsyncSocket_TrackLock("ILibAsyncSocket_PreSelect", 1, module);) ILibSpinLock_Lock(&(module->SendLock));
sem_wait(&(module->SendLock));
if (module->internalSocket != -1) if (module->internalSocket != -1)
{ {
@@ -1556,9 +1549,9 @@ void ILibAsyncSocket_PreSelect(void* socketModule,fd_set *readset, fd_set *write
module->timeout_handler = NULL; module->timeout_handler = NULL;
if (h != NULL) if (h != NULL)
{ {
sem_post(&(module->SendLock)); ILibSpinLock_UnLock(&(module->SendLock));
h(module, module->user); h(module, module->user);
sem_wait(&(module->SendLock)); ILibSpinLock_Lock(&(module->SendLock));
if (module->timeout_milliSeconds != 0) if (module->timeout_milliSeconds != 0)
{ {
*blocktime = module->timeout_milliSeconds; *blocktime = module->timeout_milliSeconds;
@@ -1616,8 +1609,7 @@ void ILibAsyncSocket_PreSelect(void* socketModule,fd_set *readset, fd_set *write
} }
} }
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_PreSelect", 2, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
ILibRemoteLogging_printf(ILibChainGetLogger(module->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_Microstack_AsyncSocket, ILibRemoteLogging_Flags_VerbosityLevel_5, "...AsyncSocket[%p] exited PreSelect", (void*)module); ILibRemoteLogging_printf(ILibChainGetLogger(module->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_Microstack_AsyncSocket, ILibRemoteLogging_Flags_VerbosityLevel_5, "...AsyncSocket[%p] exited PreSelect", (void*)module);
} }
@@ -1706,8 +1698,7 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
UNREFERENCED_PARAMETER( slct ); UNREFERENCED_PARAMETER( slct );
SEM_TRACK(AsyncSocket_TrackLock("ILibAsyncSocket_PostSelect", 1, module);) ILibSpinLock_Lock(&(module->SendLock)); // Lock!
sem_wait(&(module->SendLock)); // Lock!
//if (fd_error != 0) printf("ILibAsyncSocket_PostSelect-ERROR\r\n"); //if (fd_error != 0) printf("ILibAsyncSocket_PostSelect-ERROR\r\n");
//if (fd_read != 0) printf("ILibAsyncSocket_PostSelect-READ\r\n"); //if (fd_read != 0) printf("ILibAsyncSocket_PostSelect-READ\r\n");
@@ -1760,8 +1751,7 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
if (serr != 0) if (serr != 0)
{ {
// Unlock before fireing the event // Unlock before fireing the event
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_PostSelect", 4, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
ILibAsyncSocket_PrivateShutdown(module); ILibAsyncSocket_PrivateShutdown(module);
} }
else else
@@ -1820,8 +1810,7 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
// TODO: Set timeout. If the proxy does not respond, we need to close this connection. // TODO: Set timeout. If the proxy does not respond, we need to close this connection.
// On the other hand... This is not generally a problem, proxies will disconnect after a timeout anyway. // On the other hand... This is not generally a problem, proxies will disconnect after a timeout anyway.
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_PostSelect", 4, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
return; return;
} }
if (module->ProxyState == 2) module->ProxyState = 3; if (module->ProxyState == 2) module->ProxyState = 3;
@@ -1832,8 +1821,7 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
} }
// Unlock before fireing the event // Unlock before fireing the event
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_PostSelect", 4, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
// If we did connect, we got more things to do // If we did connect, we got more things to do
if (triggerWriteSet != 0) if (triggerWriteSet != 0)
@@ -1871,17 +1859,14 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
} }
// Unlock before fireing the event // Unlock before fireing the event
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_PostSelect", 4, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
if (triggerReadSet != 0 || triggerResume != 0) ILibProcessAsyncSocket(module, triggerReadSet); if (triggerReadSet != 0 || triggerResume != 0) ILibProcessAsyncSocket(module, triggerReadSet);
} }
} }
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_PostSelect", 4, module);)
SEM_TRACK(AsyncSocket_TrackLock("ILibAsyncSocket_PostSelect", 1, module);) ILibSpinLock_Lock(&(module->SendLock));
sem_wait(&(module->SendLock));
// Write Handling // Write Handling
if (module->FinConnect > 0 && module->internalSocket != ~0 && fd_write != 0 && module->PendingSend_Head != NULL && (module->ProxyState != 1)) if (module->FinConnect > 0 && module->internalSocket != ~0 && fd_write != 0 && module->PendingSend_Head != NULL && (module->ProxyState != 1))
{ {
@@ -2087,8 +2072,7 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
// This triggers OnSendOK, if all the pending data has been sent. // This triggers OnSendOK, if all the pending data has been sent.
if (module->PendingSend_Head == NULL && bytesSent != -1) { TriggerSendOK = 1; } if (module->PendingSend_Head == NULL && bytesSent != -1) { TriggerSendOK = 1; }
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_PostSelect", 2, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
#ifndef MICROSTACK_NOTLS #ifndef MICROSTACK_NOTLS
if (TriggerSendOK != 0 && (module->ssl == NULL || module->SSLConnect != 0)) if (TriggerSendOK != 0 && (module->ssl == NULL || module->SSLConnect != 0))
#else #else
@@ -2106,8 +2090,7 @@ void ILibAsyncSocket_PostSelect(void* socketModule, int slct, fd_set *readset, f
} }
else else
{ {
SEM_TRACK(AsyncSocket_TrackUnLock("ILibAsyncSocket_PostSelect", 2, module);) ILibSpinLock_UnLock(&(module->SendLock));
sem_post(&(module->SendLock));
} }
ILibRemoteLogging_printf(ILibChainGetLogger(module->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_Microstack_AsyncSocket, ILibRemoteLogging_Flags_VerbosityLevel_5, "...AsyncSocket[%p] exited PostSelect", (void*)module); ILibRemoteLogging_printf(ILibChainGetLogger(module->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_Microstack_AsyncSocket, ILibRemoteLogging_Flags_VerbosityLevel_5, "...AsyncSocket[%p] exited PostSelect", (void*)module);

View File

@@ -230,7 +230,7 @@ typedef struct ILibSparseArray_Root
ILibSparseArray_Node* bucket; ILibSparseArray_Node* bucket;
int bucketSize; int bucketSize;
ILibSparseArray_Bucketizer bucketizer; ILibSparseArray_Bucketizer bucketizer;
sem_t LOCK; ILibSpinLock LOCK;
int userMemorySize; int userMemorySize;
}ILibSparseArray_Root; }ILibSparseArray_Root;
const int ILibMemory_SparseArray_CONTAINERSIZE = sizeof(ILibSparseArray_Root); const int ILibMemory_SparseArray_CONTAINERSIZE = sizeof(ILibSparseArray_Root);
@@ -268,14 +268,14 @@ struct ILibQueueNode
{ {
struct ILibStackNode *Head; struct ILibStackNode *Head;
struct ILibStackNode *Tail; struct ILibStackNode *Tail;
sem_t LOCK; ILibSpinLock LOCK;
}; };
struct HashNode_Root struct HashNode_Root
{ {
struct HashNode *Root; struct HashNode *Root;
int CaseInSensitive; int CaseInSensitive;
void *Reserved; void *Reserved;
sem_t LOCK; ILibSpinLock LOCK;
}; };
struct HashNode struct HashNode
{ {
@@ -300,7 +300,7 @@ typedef struct ILibLinkedListNode
}ILibLinkedListNode; }ILibLinkedListNode;
typedef struct ILibLinkedListNode_Root typedef struct ILibLinkedListNode_Root
{ {
sem_t LOCK; ILibSpinLock LOCK;
long count; long count;
void* Tag; void* Tag;
struct ILibLinkedListNode *Head; struct ILibLinkedListNode *Head;
@@ -4943,7 +4943,7 @@ void ILibClearStack(void **TheStack)
void ILibHashTree_Lock(void *hashtree) void ILibHashTree_Lock(void *hashtree)
{ {
struct HashNode_Root *r = (struct HashNode_Root*)hashtree; struct HashNode_Root *r = (struct HashNode_Root*)hashtree;
sem_wait(&(r->LOCK)); ILibSpinLock_Lock(&(r->LOCK));
} }
/*! \fn ILibHashTree_UnLock(void *hashtree) /*! \fn ILibHashTree_UnLock(void *hashtree)
@@ -4953,7 +4953,7 @@ void ILibHashTree_Lock(void *hashtree)
void ILibHashTree_UnLock(void *hashtree) void ILibHashTree_UnLock(void *hashtree)
{ {
struct HashNode_Root *r = (struct HashNode_Root*)hashtree; struct HashNode_Root *r = (struct HashNode_Root*)hashtree;
sem_post(&(r->LOCK)); ILibSpinLock_UnLock(&(r->LOCK));
} }
/*! \fn ILibDestroyHashTree(void *tree) /*! \fn ILibDestroyHashTree(void *tree)
@@ -4966,7 +4966,6 @@ void ILibDestroyHashTree(void *tree)
struct HashNode *c = r->Root; struct HashNode *c = r->Root;
struct HashNode *n; struct HashNode *n;
sem_destroy(&(r->LOCK));
while (c != NULL) while (c != NULL)
{ {
// //
@@ -5110,7 +5109,7 @@ void* ILibInitHashTreeEx(void *ReservedMemory)
} }
Root->Root = RetVal; Root->Root = RetVal;
if (ReservedMemory == NULL) { sem_init(&(Root->LOCK), 0, 1); } if (ReservedMemory == NULL) { ILibSpinLock_Init(&(Root->LOCK)); }
return(Root); return(Root);
} }
/*! \fn void* ILibInitHashTree_CaseInSensitive() /*! \fn void* ILibInitHashTree_CaseInSensitive()
@@ -7671,8 +7670,7 @@ void* ILibLinkedList_CreateEx(int userMemorySize)
ILibMemory_Allocate(sizeof(ILibLinkedListNode_Root), userMemorySize, (void**)&root, &mem); ILibMemory_Allocate(sizeof(ILibLinkedListNode_Root), userMemorySize, (void**)&root, &mem);
root->ExtraMemory = mem; root->ExtraMemory = mem;
ILibSpinLock_Init(&(root->LOCK));
sem_init(&(root->LOCK), 0, 1);
return root; return root;
} }
@@ -8003,7 +8001,7 @@ void* ILibLinkedList_Node_ResizeAdditional(void *node, size_t additionalSize)
void ILibLinkedList_Lock(void *LinkedList) void ILibLinkedList_Lock(void *LinkedList)
{ {
struct ILibLinkedListNode_Root *r = (struct ILibLinkedListNode_Root*)LinkedList; struct ILibLinkedListNode_Root *r = (struct ILibLinkedListNode_Root*)LinkedList;
sem_wait(&(r->LOCK)); ILibSpinLock_Lock(&(r->LOCK));
} }
/*! \fn void ILibLinkedList_UnLock(void *LinkedList) /*! \fn void ILibLinkedList_UnLock(void *LinkedList)
@@ -8013,7 +8011,7 @@ void ILibLinkedList_Lock(void *LinkedList)
void ILibLinkedList_UnLock(void *LinkedList) void ILibLinkedList_UnLock(void *LinkedList)
{ {
struct ILibLinkedListNode_Root *r = (struct ILibLinkedListNode_Root*)LinkedList; struct ILibLinkedListNode_Root *r = (struct ILibLinkedListNode_Root*)LinkedList;
sem_post(&(r->LOCK)); ILibSpinLock_UnLock(&(r->LOCK));
} }
@@ -8027,7 +8025,6 @@ void ILibLinkedList_Destroy(void *LinkedList)
{ {
struct ILibLinkedListNode_Root *r = (struct ILibLinkedListNode_Root*)LinkedList; struct ILibLinkedListNode_Root *r = (struct ILibLinkedListNode_Root*)LinkedList;
while (r->Head != NULL) ILibLinkedList_Remove(ILibLinkedList_GetNode_Head(LinkedList)); while (r->Head != NULL) ILibLinkedList_Remove(ILibLinkedList_GetNode_Head(LinkedList));
sem_destroy(&(r->LOCK));
free(r); free(r);
} }
@@ -8417,7 +8414,7 @@ ILibSparseArray ILibSparseArray_CreateWithUserMemory(int numberOfBuckets, ILibSp
{ {
ILibSparseArray_Root *retVal = (ILibSparseArray_Root*)ILibMemory_Allocate(sizeof(ILibSparseArray_Root), userMemorySize, NULL, NULL); ILibSparseArray_Root *retVal = (ILibSparseArray_Root*)ILibMemory_Allocate(sizeof(ILibSparseArray_Root), userMemorySize, NULL, NULL);
sem_init(&(retVal->LOCK), 0, 1); ILibSpinLock_Init(&(retVal->LOCK));
retVal->bucketSize = numberOfBuckets; retVal->bucketSize = numberOfBuckets;
retVal->bucketizer = bucketizer; retVal->bucketizer = bucketizer;
retVal->bucket = (ILibSparseArray_Node*)malloc(numberOfBuckets * sizeof(ILibSparseArray_Node)); retVal->bucket = (ILibSparseArray_Node*)malloc(numberOfBuckets * sizeof(ILibSparseArray_Node));
@@ -8638,7 +8635,6 @@ void ILibSparseArray_ClearEx2(ILibSparseArray sarray, ILibSparseArray_OnValue on
void ILibSparseArray_DestroyEx(ILibSparseArray sarray, ILibSparseArray_OnValue onDestroy, void *user) void ILibSparseArray_DestroyEx(ILibSparseArray sarray, ILibSparseArray_OnValue onDestroy, void *user)
{ {
ILibSparseArray_ClearEx(sarray, onDestroy, user); ILibSparseArray_ClearEx(sarray, onDestroy, user);
sem_destroy(&((ILibSparseArray_Root*)sarray)->LOCK);
free(((ILibSparseArray_Root*)sarray)->bucket); free(((ILibSparseArray_Root*)sarray)->bucket);
free(sarray); free(sarray);
} }
@@ -8656,7 +8652,7 @@ void ILibSparseArray_Destroy(ILibSparseArray sarray)
*/ */
void ILibSparseArray_Lock(ILibSparseArray sarray) void ILibSparseArray_Lock(ILibSparseArray sarray)
{ {
sem_wait(&(((ILibSparseArray_Root*)sarray)->LOCK)); ILibSpinLock_Lock(&(((ILibSparseArray_Root*)sarray)));
} }
//! Use the Sparse Array as a synchronization lock, and release it //! Use the Sparse Array as a synchronization lock, and release it
/*! /*!
@@ -8664,7 +8660,7 @@ void ILibSparseArray_Lock(ILibSparseArray sarray)
*/ */
void ILibSparseArray_UnLock(ILibSparseArray sarray) void ILibSparseArray_UnLock(ILibSparseArray sarray)
{ {
sem_post(&(((ILibSparseArray_Root*)sarray)->LOCK)); ILibSpinLock_UnLock(&(((ILibSparseArray_Root*)sarray)->LOCK));
} }
int ILibString_IndexOfFirstWhiteSpace(const char *inString, size_t inStringLength) int ILibString_IndexOfFirstWhiteSpace(const char *inString, size_t inStringLength)
@@ -10540,6 +10536,7 @@ void ILibThread_Join(void *thr)
{ {
#ifdef WIN32 #ifdef WIN32
WaitForSingleObject((HANDLE)thr, INFINITE); WaitForSingleObject((HANDLE)thr, INFINITE);
CloseHandle((HANDLE)thr);
#else #else
#if defined(__APPLE__) || defined(ILIB_NO_TIMEDJOIN) #if defined(__APPLE__) || defined(ILIB_NO_TIMEDJOIN)
if (ILibMemory_CanaryOK(thr) && ((ILibThread_AppleThread*)thr)->joinable!=0) if (ILibMemory_CanaryOK(thr) && ((ILibThread_AppleThread*)thr)->joinable!=0)

View File

@@ -78,7 +78,7 @@ void WebClient_TrackUnLock(const char* MethodName, int Occurance, void *data)
#include "ILibRemoteLogging.h" #include "ILibRemoteLogging.h"
#include "ILibCrypto.h" #include "ILibCrypto.h"
extern sem_t *ILibAsyncSocket_GetSendLock(ILibAsyncSocket_SocketModule socketModule); extern ILibSpinLock *ILibAsyncSocket_GetSpinLock(ILibAsyncSocket_SocketModule socketModule);
#ifndef MICROSTACK_NOTLS #ifndef MICROSTACK_NOTLS
int ILibWebClientDataObjectIndex = -1; int ILibWebClientDataObjectIndex = -1;
@@ -169,7 +169,7 @@ struct ILibWebClientManager
void *timer; void *timer;
int idleCount; int idleCount;
sem_t QLock; ILibSpinLock QLock;
void *user; void *user;
@@ -523,7 +523,6 @@ void ILibDestroyWebClient(void *object)
ILibQueue_Destroy(manager->backlogQueue); ILibQueue_Destroy(manager->backlogQueue);
ILibDestroyHashTree(manager->idleTable); ILibDestroyHashTree(manager->idleTable);
ILibDestroyHashTree(manager->DataTable); ILibDestroyHashTree(manager->DataTable);
sem_destroy(&(manager->QLock));
free(manager->socks); free(manager->socks);
#ifndef MICROSTACK_NOTLS #ifndef MICROSTACK_NOTLS
@@ -605,8 +604,7 @@ void ILibWebClient_TimerSink(void *object)
void *data; void *data;
void *DisconnectSocket = NULL; void *DisconnectSocket = NULL;
SEM_TRACK(WebClient_TrackLock("ILibWebClient_TimerSink", 1, wcdo->Parent);) ILibSpinLock_Lock(&(wcdo->Parent->QLock));
sem_wait(&(wcdo->Parent->QLock));
if (ILibQueue_IsEmpty(wcdo->RequestQueue)!=0) if (ILibQueue_IsEmpty(wcdo->RequestQueue)!=0)
{ {
// //
@@ -634,8 +632,7 @@ void ILibWebClient_TimerSink(void *object)
wcdo2 = (struct ILibWebClientDataObject*)ILibGetEntry(wcdo->Parent->DataTable, key, keyLength); wcdo2 = (struct ILibWebClientDataObject*)ILibGetEntry(wcdo->Parent->DataTable, key, keyLength);
ILibDeleteEntry(wcdo->Parent->DataTable, key, keyLength); ILibDeleteEntry(wcdo->Parent->DataTable, key, keyLength);
ILibDeleteEntry(wcdo->Parent->idleTable, key, keyLength); ILibDeleteEntry(wcdo->Parent->idleTable, key, keyLength);
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_TimerSink",2,wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
ILibWebClient_DestroyWebClientDataObject(wcdo2); ILibWebClient_DestroyWebClientDataObject(wcdo2);
return; return;
} }
@@ -652,8 +649,7 @@ void ILibWebClient_TimerSink(void *object)
wcdo->DisconnectSent=0; wcdo->DisconnectSent=0;
} }
} }
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_TimerSink", 3, wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
// //
// Let the user know, the socket has been disconnected // Let the user know, the socket has been disconnected
// //
@@ -761,21 +757,18 @@ void ILibWebClient_FinishedResponse(ILibAsyncSocket_SocketModule socketModule, s
// so this finished response isn't valid anymore // so this finished response isn't valid anymore
if (wcdo->SOCK == NULL || ILibAsyncSocket_IsFree(wcdo->SOCK)) if (wcdo->SOCK == NULL || ILibAsyncSocket_IsFree(wcdo->SOCK))
{ {
SEM_TRACK(WebClient_TrackLock("ILibWebClient_FinishedResponse", 1, wcdo->Parent);) ILibSpinLock_Lock(&(wcdo->Parent->QLock));
sem_wait(&(wcdo->Parent->QLock));
wr = (struct ILibWebRequest*)ILibQueue_DeQueue(wcdo->RequestQueue); wr = (struct ILibWebRequest*)ILibQueue_DeQueue(wcdo->RequestQueue);
if (wr != NULL) if (wr != NULL)
{ {
wr->connectionCloseWasSpecified = 2; wr->connectionCloseWasSpecified = 2;
ILibWebClient_DestroyWebRequest(wr); ILibWebClient_DestroyWebRequest(wr);
} }
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_FinishedResponse", 2, wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
return; return;
} }
SEM_TRACK(WebClient_TrackLock("ILibWebClient_FinishedResponse", 1, wcdo->Parent);) ILibSpinLock_Lock(&(wcdo->Parent->QLock));
sem_wait(&(wcdo->Parent->QLock));
wr = (struct ILibWebRequest*)ILibQueue_DeQueue(wcdo->RequestQueue); wr = (struct ILibWebRequest*)ILibQueue_DeQueue(wcdo->RequestQueue);
if (wr != NULL) if (wr != NULL)
{ {
@@ -798,8 +791,7 @@ void ILibWebClient_FinishedResponse(ILibAsyncSocket_SocketModule socketModule, s
} }
} }
} }
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_FinishedResponse", 2 ,wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
//{{{ REMOVE_THIS_FOR_HTTP/1.0_ONLY_SUPPORT--> }}} //{{{ REMOVE_THIS_FOR_HTTP/1.0_ONLY_SUPPORT--> }}}
if (wr != NULL) if (wr != NULL)
@@ -898,14 +890,12 @@ void ILibWebClient_ProcessChunk(ILibAsyncSocket_SocketModule socketModule, struc
// //
if (wcdo->Parent!=NULL) if (wcdo->Parent!=NULL)
{ {
SEM_TRACK(WebClient_TrackLock("ILibWebClient_ProcessChunk",1,wcdo->Parent);) ILibSpinLock_Lock(&(wcdo->Parent->QLock));
sem_wait(&(wcdo->Parent->QLock));
} }
wr = (struct ILibWebRequest*)ILibQueue_PeekQueue(wcdo->RequestQueue); wr = (struct ILibWebRequest*)ILibQueue_PeekQueue(wcdo->RequestQueue);
if (wcdo->Parent!=NULL) if (wcdo->Parent!=NULL)
{ {
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_ProcessChunk",2,wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
} }
if (wcdo->chunk==NULL) if (wcdo->chunk==NULL)
@@ -1250,7 +1240,7 @@ ILibAsyncSocket_SendStatus ILibWebClient_WebSocket_Send(ILibWebClient_StateObjec
#endif #endif
#endif #endif
sem_wait(ILibAsyncSocket_GetSendLock(wcdo->SOCK)); ILibSpinLock_Lock(ILibAsyncSocket_GetSpinLock(wcdo->SOCK));
while (i < _bufferLen) while (i < _bufferLen)
{ {
if (i < 0) { i = 0; } if (i < 0) { i = 0; }
@@ -1304,7 +1294,7 @@ ILibAsyncSocket_SendStatus ILibWebClient_WebSocket_Send(ILibWebClient_StateObjec
RetVal = ILibAsyncSocket_SendTo_MultiWrite(wcdo->SOCK, NULL, 2 | ILibAsyncSocket_LOCK_OVERRIDE, header, (size_t)headerLen, ILibAsyncSocket_MemoryOwnership_USER, buffer, (size_t)bufferLen, ILibAsyncSocket_MemoryOwnership_USER); RetVal = ILibAsyncSocket_SendTo_MultiWrite(wcdo->SOCK, NULL, 2 | ILibAsyncSocket_LOCK_OVERRIDE, header, (size_t)headerLen, ILibAsyncSocket_MemoryOwnership_USER, buffer, (size_t)bufferLen, ILibAsyncSocket_MemoryOwnership_USER);
} }
} }
sem_post(ILibAsyncSocket_GetSendLock(wcdo->SOCK)); ILibSpinLock_UnLock(ILibAsyncSocket_GetSpinLock(wcdo->SOCK));
return RetVal; return RetVal;
} }
void ILibWebClient_WebSocket_SetPingPongHandler(ILibWebClient_StateObject obj, ILibWebClient_WebSocket_PingHandler pingHandler, ILibWebClient_WebSocket_PongHandler pongHandler, void *user) void ILibWebClient_WebSocket_SetPingPongHandler(ILibWebClient_StateObject obj, ILibWebClient_WebSocket_PingHandler pingHandler, ILibWebClient_WebSocket_PongHandler pongHandler, void *user)
@@ -1514,14 +1504,12 @@ ILibWebClient_DataResults ILibWebClient_OnData(ILibAsyncSocket_SocketModule sock
if (wcdo->Server == 0) if (wcdo->Server == 0)
{ {
SEM_TRACK(WebClient_TrackLock("ILibWebClient_OnData", 1, wcdo->Parent);) ILibSpinLock_Lock(&(wcdo->Parent->QLock));
sem_wait(&(wcdo->Parent->QLock));
} }
wr = (struct ILibWebRequest*)ILibQueue_PeekQueue(wcdo->RequestQueue); wr = (struct ILibWebRequest*)ILibQueue_PeekQueue(wcdo->RequestQueue);
if (wcdo->Server == 0) if (wcdo->Server == 0)
{ {
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_OnData", 2, wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
} }
if (wr == NULL) if (wr == NULL)
{ {
@@ -2083,11 +2071,9 @@ void ILibWebClient_OnConnect(ILibAsyncSocket_SocketModule socketModule, int Conn
// Success: Send First Request // Success: Send First Request
ILibAsyncSocket_GetLocalInterface(socketModule, (struct sockaddr*)&(wcdo->LocalIP)); ILibAsyncSocket_GetLocalInterface(socketModule, (struct sockaddr*)&(wcdo->LocalIP));
SEM_TRACK(WebClient_TrackLock("ILibWebClient_OnConnect", 1, wcdo->Parent);) ILibSpinLock_Lock(&(wcdo->Parent->QLock));
sem_wait(&(wcdo->Parent->QLock));
r = (struct ILibWebRequest*)ILibQueue_PeekQueue(wcdo->RequestQueue); r = (struct ILibWebRequest*)ILibQueue_PeekQueue(wcdo->RequestQueue);
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_OnConnect", 2, wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
if (r != NULL) if (r != NULL)
{ {
if (r->ConnectSink != NULL) { r->ConnectSink(r->requestToken); } if (r->ConnectSink != NULL) { r->ConnectSink(r->requestToken); }
@@ -2128,16 +2114,14 @@ void ILibWebClient_OnConnect(ILibAsyncSocket_SocketModule socketModule, int Conn
// //
// Retried enough times, give up // Retried enough times, give up
// //
SEM_TRACK(WebClient_TrackLock("ILibWebClient_OnConnect", 3, wcdo->Parent);) ILibSpinLock_Lock(&(wcdo->Parent->QLock));
sem_wait(&(wcdo->Parent->QLock));
keyLength = ILibCreateTokenStr((struct sockaddr*)&(wcdo->remote), wcdo->IndexNumber, key); keyLength = ILibCreateTokenStr((struct sockaddr*)&(wcdo->remote), wcdo->IndexNumber, key);
ILibDeleteEntry(wcdo->Parent->DataTable, key, keyLength); ILibDeleteEntry(wcdo->Parent->DataTable, key, keyLength);
ILibDeleteEntry(wcdo->Parent->idleTable, key, keyLength); ILibDeleteEntry(wcdo->Parent->idleTable, key, keyLength);
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_OnConnect", 4, wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
ILibWebClient_DestroyWebClientDataObject(wcdo); ILibWebClient_DestroyWebClientDataObject(wcdo);
} }
@@ -2197,11 +2181,9 @@ void ILibWebClient_OnDisconnectSink(ILibAsyncSocket_SocketModule socketModule, v
// //
ILibAsyncSocket_GetBuffer(socketModule,&buffer,&BeginPointer,&EndPointer); ILibAsyncSocket_GetBuffer(socketModule,&buffer,&BeginPointer,&EndPointer);
SEM_TRACK(WebClient_TrackLock("ILibWebClient_OnDisconnect", 1, wcdo->Parent);) ILibSpinLock_Lock(&(wcdo->Parent->QLock));
sem_wait(&(wcdo->Parent->QLock));
wr = (struct ILibWebRequest*)ILibQueue_DeQueue(wcdo->RequestQueue); wr = (struct ILibWebRequest*)ILibQueue_DeQueue(wcdo->RequestQueue);
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_OnDisconnect", 2, wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
wcdo->InitialRequestAnswered = 1; wcdo->InitialRequestAnswered = 1;
//{{{ REMOVE_THIS_FOR_HTTP/1.0_ONLY_SUPPORT--> }}} //{{{ REMOVE_THIS_FOR_HTTP/1.0_ONLY_SUPPORT--> }}}
//wcdo->PipelineFlag = PIPELINE_NO; //wcdo->PipelineFlag = PIPELINE_NO;
@@ -2232,11 +2214,9 @@ void ILibWebClient_OnDisconnectSink(ILibAsyncSocket_SocketModule socketModule, v
if (wcdo->Closing != 0) { return; } if (wcdo->Closing != 0) { return; }
SEM_TRACK(WebClient_TrackLock("ILibWebClient_OnDisconnect", 3, wcdo->Parent);) ILibSpinLock_Lock(&(wcdo->Parent->QLock));
sem_wait(&(wcdo->Parent->QLock));
wr = (struct ILibWebRequest*)ILibQueue_PeekQueue(wcdo->RequestQueue); wr = (struct ILibWebRequest*)ILibQueue_PeekQueue(wcdo->RequestQueue);
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_OnDisconnect", 4, wcdo->Parent);) ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
sem_post(&(wcdo->Parent->QLock));
if (wr != NULL) if (wr != NULL)
{ {
@@ -2306,8 +2286,7 @@ void ILibWebClient_PreProcess(void* WebClientModule, fd_set *readset, fd_set *wr
// Try and satisfy as many things as we can. If we have resources // Try and satisfy as many things as we can. If we have resources
// grab a socket and go // grab a socket and go
// //
SEM_TRACK(WebClient_TrackLock("ILibWebClient_PreProcess",1,wcm);) ILibSpinLock_Lock(&(wcm->QLock));
sem_wait(&(wcm->QLock));
while (xOK == 0 && ILibQueue_IsEmpty(wcm->backlogQueue) == 0) while (xOK == 0 && ILibQueue_IsEmpty(wcm->backlogQueue) == 0)
{ {
xOK = 1; xOK = 1;
@@ -2376,8 +2355,7 @@ void ILibWebClient_PreProcess(void* WebClientModule, fd_set *readset, fd_set *wr
if (ILibQueue_IsEmpty(wcm->backlogQueue) != 0) break; if (ILibQueue_IsEmpty(wcm->backlogQueue) != 0) break;
} }
} }
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_PreProcess", 2, wcm);) ILibSpinLock_UnLock(&(wcm->QLock));
sem_post(&(wcm->QLock));
} }
// //
@@ -2503,7 +2481,7 @@ ILibWebClient_RequestManager ILibCreateWebClient(int PoolSize, void *Chain)
RetVal->socksLength = PoolSize; RetVal->socksLength = PoolSize;
RetVal->socks = (void**)malloc(PoolSize * sizeof(void*)); RetVal->socks = (void**)malloc(PoolSize * sizeof(void*));
if (RetVal->socks == NULL) ILIBCRITICALEXIT(254); if (RetVal->socks == NULL) ILIBCRITICALEXIT(254);
sem_init(&(RetVal->QLock), 0, 1); ILibSpinLock_Init(&(RetVal->QLock));
RetVal->ChainLink.ParentChain = Chain; RetVal->ChainLink.ParentChain = Chain;
RetVal->backlogQueue = ILibQueue_Create(); RetVal->backlogQueue = ILibQueue_Create();
@@ -2708,8 +2686,7 @@ ILibWebClient_RequestToken ILibWebClient_PipelineRequestEx2(
// //
// Does the client already have a connection to the server? // Does the client already have a connection to the server?
// //
SEM_TRACK(WebClient_TrackLock("ILibWebClient_PipelineRequestEx", 1, wcm);) ILibSpinLock_Lock(&(wcm->QLock));
sem_wait(&(wcm->QLock));
if (wcm->MaxConnectionsToSameServer > 1) if (wcm->MaxConnectionsToSameServer > 1)
{ {
@@ -2827,8 +2804,7 @@ ILibWebClient_RequestToken ILibWebClient_PipelineRequestEx2(
} }
} }
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_PipelineRequestEx", 2, wcm);) ILibSpinLock_UnLock(&(wcm->QLock));
sem_post(&(wcm->QLock));
if (ForceUnBlock != 0) ILibForceUnBlockChain(wcm->ChainLink.ParentChain); if (ForceUnBlock != 0) ILibForceUnBlockChain(wcm->ChainLink.ParentChain);
SESSION_TRACK(request->requestToken, NULL, "PipelinedRequestEx"); SESSION_TRACK(request->requestToken, NULL, "PipelinedRequestEx");
return(request->requestToken); return(request->requestToken);
@@ -2932,8 +2908,7 @@ void ILibWebClient_DeleteRequests(ILibWebClient_RequestManager WebClientToken, s
// //
// Are there any pending requests to this IP/Port combo? // Are there any pending requests to this IP/Port combo?
// //
SEM_TRACK(WebClient_TrackLock("ILibWebClient_DeleteRequests", 1, wcm);) ILibSpinLock_Lock(&(wcm->QLock));
sem_wait(&(wcm->QLock));
if (ILibHasEntry(wcm->DataTable, RequestToken, RequestTokenLength) != 0) if (ILibHasEntry(wcm->DataTable, RequestToken, RequestTokenLength) != 0)
{ {
// //
@@ -2958,8 +2933,7 @@ void ILibWebClient_DeleteRequests(ILibWebClient_RequestManager WebClientToken, s
ILibLifeTime_Remove(wcdo->Parent->timer, wcdo); ILibLifeTime_Remove(wcdo->Parent->timer, wcdo);
} }
} }
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_DeleteRequests",2,wcm);) ILibSpinLock_UnLock(&(wcm->QLock));
sem_post(&(wcm->QLock));
} }
@@ -3065,8 +3039,7 @@ void ILibWebClient_CancelRequestEx2(ILibWebClient_StateObject wcdo, void *userRe
{ {
PendingRequestQ = ILibQueue_Create(); PendingRequestQ = ILibQueue_Create();
SEM_TRACK(WebClient_TrackLock("ILibWebClient_CancelRequest",1,_wcdo->Parent);) ILibSpinLock_Lock(&(_wcdo->Parent->QLock));
sem_wait(&(_wcdo->Parent->QLock));
head = node = ILibLinkedList_GetNode_Head(_wcdo->RequestQueue); head = node = ILibLinkedList_GetNode_Head(_wcdo->RequestQueue);
while (node != NULL) while (node != NULL)
@@ -3107,13 +3080,11 @@ void ILibWebClient_CancelRequestEx2(ILibWebClient_StateObject wcdo, void *userRe
_wcdo->Closing = 2; _wcdo->Closing = 2;
_wcdo->CancelRequest = 1; _wcdo->CancelRequest = 1;
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_CancelRequest", 2, _wcdo->Parent);) ILibSpinLock_UnLock(&(_wcdo->Parent->QLock));
sem_post(&(_wcdo->Parent->QLock));
ILibWebClient_Disconnect(_wcdo); ILibWebClient_Disconnect(_wcdo);
SEM_TRACK(WebClient_TrackLock("ILibWebClient_CancelRequest", 3, _wcdo->Parent);) ILibSpinLock_Lock(&(_wcdo->Parent->QLock));
sem_wait(&(_wcdo->Parent->QLock));
_wcdo->Closing = 0; _wcdo->Closing = 0;
_wcdo->CancelRequest = 0; _wcdo->CancelRequest = 0;
@@ -3131,8 +3102,7 @@ void ILibWebClient_CancelRequestEx2(ILibWebClient_StateObject wcdo, void *userRe
} }
} }
SEM_TRACK(WebClient_TrackUnLock("ILibWebClient_CancelRequest", 2, _wcdo->Parent);) ILibSpinLock_UnLock(&(_wcdo->Parent->QLock));
sem_post(&(_wcdo->Parent->QLock));
wr = (struct ILibWebRequest*)ILibQueue_DeQueue(PendingRequestQ); wr = (struct ILibWebRequest*)ILibQueue_DeQueue(PendingRequestQ);
while (wr != NULL) while (wr != NULL)
@@ -3803,14 +3773,14 @@ void ILibWebClient_RequestToken_ConnectionHandler_Set(ILibWebClient_RequestToken
if (wcdo != NULL) if (wcdo != NULL)
{ {
sem_wait(&(wcdo->Parent->QLock)); ILibSpinLock_Lock(&(wcdo->Parent->QLock));
wr = (struct ILibWebRequest*)ILibQueue_PeekTail(wcdo->RequestQueue); wr = (struct ILibWebRequest*)ILibQueue_PeekTail(wcdo->RequestQueue);
if (wr != NULL) if (wr != NULL)
{ {
wr->ConnectSink = OnConnect; wr->ConnectSink = OnConnect;
wr->DisconnectSink = OnDisconnect; wr->DisconnectSink = OnDisconnect;
} }
sem_post(&(wcdo->Parent->QLock)); ILibSpinLock_UnLock(&(wcdo->Parent->QLock));
} }
} }
#ifdef MICROSTACK_PROXY #ifdef MICROSTACK_PROXY

View File

@@ -694,7 +694,7 @@ typedef struct ILibStun_dTlsSession
unsigned int userTSN; unsigned int userTSN;
long long lastResent; long long lastResent;
int state; // 0 = Free, 1 = Setup, 2 = Connecting, 3 = Disconnecting, 4 = Handshake int state; // 0 = Free, 1 = Setup, 2 = Connecting, 3 = Disconnecting, 4 = Handshake
sem_t Lock; ILibSpinLock Lock;
ILibSparseArray DataChannelMetaDeta; ILibSparseArray DataChannelMetaDeta;
ILibSparseArray DataChannelMetaDetaValues; ILibSparseArray DataChannelMetaDetaValues;
@@ -1322,9 +1322,9 @@ int ILibSCTP_GetUser4(void* module) { return ((struct ILibStun_dTlsSession*)modu
int ILibSCTP_GetPendingBytesToSend(void* sctpSession) int ILibSCTP_GetPendingBytesToSend(void* sctpSession)
{ {
int r; int r;
sem_wait(&(((struct ILibStun_dTlsSession*)sctpSession)->Lock)); ILibSpinLock_Lock(&(((struct ILibStun_dTlsSession*)sctpSession)->Lock));
r = ((struct ILibStun_dTlsSession*)sctpSession)->holdingByteCount; r = ((struct ILibStun_dTlsSession*)sctpSession)->holdingByteCount;
sem_post(&(((struct ILibStun_dTlsSession*)sctpSession)->Lock)); ILibSpinLock_UnLock(&(((struct ILibStun_dTlsSession*)sctpSession)->Lock));
if (r < 0) return 0 - r; if (r < 0) return 0 - r;
return 0; return 0;
} }
@@ -1618,7 +1618,7 @@ void ILibWebRTC_CloseDataChannel_Timeout(void *obj)
struct ILibStun_dTlsSession *o = ILibWebRTC_DTLS_FROM_TIMER_OBJECT(obj); struct ILibStun_dTlsSession *o = ILibWebRTC_DTLS_FROM_TIMER_OBJECT(obj);
int newTimeout; int newTimeout;
sem_wait(&o->Lock); ILibSpinLock_Lock(&o->Lock);
++o->reconfigFailures; ++o->reconfigFailures;
newTimeout = RTO_MIN * (0x01 << o->reconfigFailures); // Exponential Backoff newTimeout = RTO_MIN * (0x01 << o->reconfigFailures); // Exponential Backoff
@@ -1630,7 +1630,7 @@ void ILibWebRTC_CloseDataChannel_Timeout(void *obj)
o->pendingReconfigPacket = NULL; o->pendingReconfigPacket = NULL;
o->reconfigFailures = 0; o->reconfigFailures = 0;
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
if (dup != NULL) { ILibWebRTC_PropagateChannelCloseEx(dup, o); } if (dup != NULL) { ILibWebRTC_PropagateChannelCloseEx(dup, o); }
} }
@@ -1640,7 +1640,7 @@ void ILibWebRTC_CloseDataChannel_Timeout(void *obj)
ILibRemoteLogging_printf(ILibChainGetLogger(o->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "RECONFIG CHUNK retry attempt %d on Dtls Session: %d", o->reconfigFailures, o->sessionId); ILibRemoteLogging_printf(ILibChainGetLogger(o->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "RECONFIG CHUNK retry attempt %d on Dtls Session: %d", o->reconfigFailures, o->sessionId);
ILibLifeTime_AddEx(o->parent->Timer, ILibWebRTC_DTLS_TO_TIMER_OBJECT(o), newTimeout, &ILibWebRTC_CloseDataChannel_Timeout, NULL); ILibLifeTime_AddEx(o->parent->Timer, ILibWebRTC_DTLS_TO_TIMER_OBJECT(o), newTimeout, &ILibWebRTC_CloseDataChannel_Timeout, NULL);
ILibStun_SendSctpPacket(o->parent, o->sessionId, o->pendingReconfigPacket, (int)ntohs(((unsigned short*)(o->pendingReconfigPacket + 14))[0])); ILibStun_SendSctpPacket(o->parent, o->sessionId, o->pendingReconfigPacket, (int)ntohs(((unsigned short*)(o->pendingReconfigPacket + 14))[0]));
sem_post(&o->Lock); ILibSpinLock_UnLock(&o->Lock);
} }
} }
@@ -3402,9 +3402,9 @@ ILibTransport_DoneState ILibSCTP_SendEx(void* sctpSession, unsigned short stream
ILibTransport_DoneState r; ILibTransport_DoneState r;
struct ILibStun_dTlsSession* obj = (struct ILibStun_dTlsSession*)sctpSession; struct ILibStun_dTlsSession* obj = (struct ILibStun_dTlsSession*)sctpSession;
if (obj->state != 2) return ILibTransport_DoneState_ERROR; // Error if (obj->state != 2) return ILibTransport_DoneState_ERROR; // Error
sem_wait(&(obj->Lock)); ILibSpinLock_Lock(&(obj->Lock));
r = ILibStun_SctpSendData(obj->parent, obj->sessionId, streamId, dataType, data, datalen); r = ILibStun_SctpSendData(obj->parent, obj->sessionId, streamId, dataType, data, datalen);
sem_post(&(obj->Lock)); ILibSpinLock_UnLock(&(obj->Lock));
RCTPDEBUG(printf("ILibSCTP_Send() len=%d, r=%d\r\n", datalen, r);) RCTPDEBUG(printf("ILibSCTP_Send() len=%d, r=%d\r\n", datalen, r);)
return r; return r;
} }
@@ -3416,7 +3416,7 @@ void ILibStun_SctpProcessStreamData(struct ILibStun_Module *obj, int session, un
// TODO: Add error case for when invalid streamId is specified. Bryan // TODO: Add error case for when invalid streamId is specified. Bryan
sem_wait(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_Lock(&(obj->dTlsSessions[session]->Lock));
if(pid == 50) if(pid == 50)
{ {
// WebRTC Control // WebRTC Control
@@ -3434,9 +3434,9 @@ void ILibStun_SctpProcessStreamData(struct ILibStun_Module *obj, int session, un
ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "Data Channel ACK on session: %u for StreamId: %u", session, streamId); ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "Data Channel ACK on session: %u for StreamId: %u", session, streamId);
sem_post(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_UnLock(&(obj->dTlsSessions[session]->Lock));
if (obj->OnWebRTCDataChannelAck != NULL) { obj->OnWebRTCDataChannelAck(obj, obj->dTlsSessions[session], streamId); } if (obj->OnWebRTCDataChannelAck != NULL) { obj->OnWebRTCDataChannelAck(obj, obj->dTlsSessions[session], streamId); }
sem_wait(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_Lock(&(obj->dTlsSessions[session]->Lock));
} }
break; break;
case 0x03: // WebRTC Data Channel Protocol: DATA_CHANNEL_OPEN case 0x03: // WebRTC Data Channel Protocol: DATA_CHANNEL_OPEN
@@ -3497,9 +3497,9 @@ void ILibStun_SctpProcessStreamData(struct ILibStun_Module *obj, int session, un
ILibSparseArray_Add(obj->dTlsSessions[session]->DataChannelMetaDeta, streamId, attributes.Raw); ILibSparseArray_Add(obj->dTlsSessions[session]->DataChannelMetaDeta, streamId, attributes.Raw);
ILibSparseArray_Add(obj->dTlsSessions[session]->DataChannelMetaDetaValues, streamId, attributesData.Raw); ILibSparseArray_Add(obj->dTlsSessions[session]->DataChannelMetaDetaValues, streamId, attributesData.Raw);
sem_post(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_UnLock(&(obj->dTlsSessions[session]->Lock));
if (obj->OnWebRTCDataChannel != NULL) { sendAck = obj->OnWebRTCDataChannel(obj, obj->dTlsSessions[session], streamId, data + 12, channelNameLength); } if (obj->OnWebRTCDataChannel != NULL) { sendAck = obj->OnWebRTCDataChannel(obj, obj->dTlsSessions[session], streamId, data + 12, channelNameLength); }
sem_wait(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_Lock(&(obj->dTlsSessions[session]->Lock));
if (sendAck == 0) if (sendAck == 0)
{ {
@@ -3519,10 +3519,10 @@ void ILibStun_SctpProcessStreamData(struct ILibStun_Module *obj, int session, un
// This is a full data block // This is a full data block
if (obj->OnData != NULL && obj->dTlsSessions[session]->state == 2) if (obj->OnData != NULL && obj->dTlsSessions[session]->state == 2)
{ {
sem_post(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_UnLock(&(obj->dTlsSessions[session]->Lock));
obj->OnData(obj, obj->dTlsSessions[session], streamId, pid, data, datalen, &(obj->dTlsSessions[session]->User)); obj->OnData(obj, obj->dTlsSessions[session], streamId, pid, data, datalen, &(obj->dTlsSessions[session]->User));
if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state != 2) return; if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state != 2) return;
sem_wait(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_Lock(&(obj->dTlsSessions[session]->Lock));
} }
// ILibStun_SctpSendData(obj, session, 0, pid, data, datalen); // ECHO // ILibStun_SctpSendData(obj, session, 0, pid, data, datalen); // ECHO
} }
@@ -3557,10 +3557,10 @@ void ILibStun_SctpProcessStreamData(struct ILibStun_Module *obj, int session, un
{ {
if (obj->OnData != NULL && obj->dTlsSessions[session]->state == 2) if (obj->OnData != NULL && obj->dTlsSessions[session]->state == 2)
{ {
sem_post(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_UnLock(&(obj->dTlsSessions[session]->Lock));
obj->OnData(obj, obj->dTlsSessions[session], streamId, pid, acc->buffer, acc->bufferPtr, &(obj->dTlsSessions[session]->User)); obj->OnData(obj, obj->dTlsSessions[session], streamId, pid, acc->buffer, acc->bufferPtr, &(obj->dTlsSessions[session]->User));
if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state != 2) return; if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state != 2) return;
sem_wait(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_Lock(&(obj->dTlsSessions[session]->Lock));
} }
acc->bufferPtr = -1; acc->bufferPtr = -1;
} }
@@ -3595,7 +3595,7 @@ void ILibStun_SctpProcessStreamData(struct ILibStun_Module *obj, int session, un
} }
} }
sem_post(&(obj->dTlsSessions[session]->Lock)); ILibSpinLock_UnLock(&(obj->dTlsSessions[session]->Lock));
} }
void ILibStun_SctpDisconnect_Final(void *obj) void ILibStun_SctpDisconnect_Final(void *obj)
@@ -3646,8 +3646,7 @@ void ILibStun_SctpDisconnect_Final(void *obj)
// Free the session // Free the session
o->state = 0; o->state = 0;
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
sem_destroy(&(o->Lock));
ILibRemoteLogging_printf(ILibChainGetLogger(o->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "Cleaning up Dtls Session Object for session: %d", o->sessionId); ILibRemoteLogging_printf(ILibChainGetLogger(o->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "Cleaning up Dtls Session Object for session: %d", o->sessionId);
ILibLifeTime_Remove(o->parent->Timer, ILibWebRTC_DTLS_TO_TIMER_OBJECT(o)); ILibLifeTime_Remove(o->parent->Timer, ILibWebRTC_DTLS_TO_TIMER_OBJECT(o));
@@ -3669,16 +3668,16 @@ void ILibStun_SctpDisconnect_Continue(void *chain, void *j)
ILibWebRTC_CloseDataChannel_ALL(o); ILibWebRTC_CloseDataChannel_ALL(o);
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
if (o->state < 1 || o->state > 2) { sem_post(&(o->Lock)); return; } if (o->state < 1 || o->state > 2) { ILibSpinLock_UnLock(&(o->Lock)); return; }
// Send the event // Send the event
if (o->state == 2 && obj->OnConnect != NULL) if (o->state == 2 && obj->OnConnect != NULL)
{ {
o->state = 3; o->state = 3;
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
obj->OnConnect(obj, o, 0); obj->OnConnect(obj, o, 0);
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
else else
{ {
@@ -3949,9 +3948,9 @@ void ILibStun_SctpResent(struct ILibStun_dTlsSession *obj)
void ILibStun_SctpOnTimeout(void *object) void ILibStun_SctpOnTimeout(void *object)
{ {
struct ILibStun_dTlsSession *obj = ILibWebRTC_DTLS_FROM_SCTP_HEARTBEAT_TIMER_OBJECT(object); struct ILibStun_dTlsSession *obj = ILibWebRTC_DTLS_FROM_SCTP_HEARTBEAT_TIMER_OBJECT(object);
sem_wait(&(obj->Lock)); ILibSpinLock_Lock(&(obj->Lock));
// printf("Timer state=%d, value=%d\r\n", obj->state, obj->timervalue); // printf("Timer state=%d, value=%d\r\n", obj->state, obj->timervalue);
if (obj->state < 1 || obj->state > 2) { sem_post(&(obj->Lock)); return; } // Check if we are still needed, connecting or connected state only. if (obj->state < 1 || obj->state > 2) { ILibSpinLock_UnLock(&(obj->Lock)); return; } // Check if we are still needed, connecting or connected state only.
obj->timervalue++; obj->timervalue++;
if (obj->timervalue < 80) if (obj->timervalue < 80)
@@ -3963,7 +3962,7 @@ void ILibStun_SctpOnTimeout(void *object)
{ {
// Close the connection // Close the connection
printf("************************************\n"); printf("************************************\n");
sem_post(&(obj->Lock)); ILibSpinLock_UnLock(&(obj->Lock));
ILibRemoteLogging_printf(ILibChainGetLogger(obj->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP Timeout on Session: %d", obj->sessionId); ILibRemoteLogging_printf(ILibChainGetLogger(obj->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP Timeout on Session: %d", obj->sessionId);
ILibStun_SctpDisconnect(obj->parent, obj->sessionId); ILibStun_SctpDisconnect(obj->parent, obj->sessionId);
return; return;
@@ -3978,7 +3977,7 @@ void ILibStun_SctpOnTimeout(void *object)
} }
ILibLifeTime_AddEx(obj->parent->Timer, ILibWebRTC_DTLS_TO_SCTP_HEARTBEAT_TIMER_OBJECT(obj), 100 + (200 * obj->timervalue), &ILibStun_SctpOnTimeout, NULL); ILibLifeTime_AddEx(obj->parent->Timer, ILibWebRTC_DTLS_TO_SCTP_HEARTBEAT_TIMER_OBJECT(obj), 100 + (200 * obj->timervalue), &ILibStun_SctpOnTimeout, NULL);
sem_post(&(obj->Lock)); ILibSpinLock_UnLock(&(obj->Lock));
} }
int ILibSCTP_AddOptionalVariableParameter(char* insertionPoint, unsigned short parameterType, void *parameterData, int parameterDataLen) int ILibSCTP_AddOptionalVariableParameter(char* insertionPoint, unsigned short parameterType, void *parameterData, int parameterDataLen)
@@ -4266,10 +4265,10 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
ILibLifeTime_Add(obj->Timer, ILibWebRTC_DTLS_TO_CONSENT_FRESHNESS_TIMER_OBJECT(o), ILibStun_MaxConsentFreshnessTimeoutSeconds, ILibStun_WebRTC_ConsentFreshness_Start, NULL); ILibLifeTime_Add(obj->Timer, ILibWebRTC_DTLS_TO_CONSENT_FRESHNESS_TIMER_OBJECT(o), ILibStun_MaxConsentFreshnessTimeoutSeconds, ILibStun_WebRTC_ConsentFreshness_Start, NULL);
} }
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
// Decode the rest of the header // Decode the rest of the header
if (o->tag != ((unsigned int*)buffer)[1] && ((unsigned int*)buffer)[1] != 0) { sem_post(&(o->Lock)); return; } // Check the verification tag if (o->tag != ((unsigned int*)buffer)[1] && ((unsigned int*)buffer)[1] != 0) { ILibSpinLock_UnLock(&(o->Lock)); return; } // Check the verification tag
// Setup the response packet // Setup the response packet
rptr = &(o->rpacketptr); rptr = &(o->rpacketptr);
@@ -4355,9 +4354,9 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
ILibSparseArray dup = ILibSparseArray_Move(o->DataChannelMetaDeta); ILibSparseArray dup = ILibSparseArray_Move(o->DataChannelMetaDeta);
ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "......Resetting all streams"); ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "......Resetting all streams");
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
ILibWebRTC_PropagateChannelCloseEx(dup, o); ILibWebRTC_PropagateChannelCloseEx(dup, o);
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
else else
{ {
@@ -4375,10 +4374,10 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
ILibSparseArray_Remove(o->DataChannelMetaDetaValues, streamId); // Clear associated data with this stream ID ILibSparseArray_Remove(o->DataChannelMetaDetaValues, streamId); // Clear associated data with this stream ID
if(obj->OnWebRTCDataChannelClosed != NULL) if(obj->OnWebRTCDataChannelClosed != NULL)
{ {
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
obj->OnWebRTCDataChannelClosed(obj, o, streamId); obj->OnWebRTCDataChannelClosed(obj, o, streamId);
if (obj->dTlsSessions[session] == NULL) { return; } if (obj->dTlsSessions[session] == NULL) { return; }
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
} }
else else
@@ -4497,9 +4496,9 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
ILibSparseArray_Remove(o->DataChannelMetaDetaValues, streamId); // Clear associated data with this stream ID ILibSparseArray_Remove(o->DataChannelMetaDetaValues, streamId); // Clear associated data with this stream ID
if (obj->OnWebRTCDataChannelClosed != NULL) if (obj->OnWebRTCDataChannelClosed != NULL)
{ {
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
obj->OnWebRTCDataChannelClosed(obj, o, streamId); obj->OnWebRTCDataChannelClosed(obj, o, streamId);
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
} }
outRequest->Streams[streamCount-1] = htons(streamId); outRequest->Streams[streamCount-1] = htons(streamId);
@@ -4524,9 +4523,9 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
o->pendingReconfigPacket = NULL; o->pendingReconfigPacket = NULL;
// We need to shed the Lock before we call up the stack // We need to shed the Lock before we call up the stack
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
ILibWebRTC_PropagateChannelCloseEx(arr, o); ILibWebRTC_PropagateChannelCloseEx(arr, o);
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
} }
} }
@@ -4552,10 +4551,10 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
if (obj->OnConnect != NULL && o->state == 1) if (obj->OnConnect != NULL && o->state == 1)
{ {
o->state = 2; o->state = 2;
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
obj->OnConnect(obj, o, 1); obj->OnConnect(obj, o, 1);
if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state == 0) return; if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state == 0) return;
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
} }
case RCTP_CHUNK_TYPE_INITACK: case RCTP_CHUNK_TYPE_INITACK:
@@ -4615,7 +4614,7 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
if (chunksize < 20 || session == -1 || o->state != 1) break; if (chunksize < 20 || session == -1 || o->state != 1) break;
o->sessionId = session; o->sessionId = session;
o->tag = ((unsigned int*)(buffer + ptr + 4))[0]; o->tag = ((unsigned int*)(buffer + ptr + 4))[0];
if (o->tag == 0) { sem_post(&(o->Lock)); return; } // This tag can't be zeroes if (o->tag == 0) { ILibSpinLock_UnLock(&(o->Lock)); return; } // This tag can't be zeroes
o->receiverCredits = ntohl(((unsigned int*)(buffer + ptr + 8))[0]); o->receiverCredits = ntohl(((unsigned int*)(buffer + ptr + 8))[0]);
#if ILibSCTP_MaxSenderCredits > 0 #if ILibSCTP_MaxSenderCredits > 0
if (o->receiverCredits > ILibSCTP_MaxSenderCredits) o->receiverCredits = ILibSCTP_MaxSenderCrtedits; // Since we do real-time KVM, reduce the buffering. if (o->receiverCredits > ILibSCTP_MaxSenderCredits) o->receiverCredits = ILibSCTP_MaxSenderCrtedits; // Since we do real-time KVM, reduce the buffering.
@@ -5039,11 +5038,11 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
// If we can now send more packets, notify the application // If we can now send more packets, notify the application
if (obj->OnSendOK != NULL && o->holdingCount == 0 && oldHoldCount > 0) if (obj->OnSendOK != NULL && o->holdingCount == 0 && oldHoldCount > 0)
{ {
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
// printf("SendOK\r\n"); // printf("SendOK\r\n");
obj->OnSendOK(obj, o, o->User); obj->OnSendOK(obj, o, o->User);
if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state != 2) return; if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state != 2) return;
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
} }
break; break;
@@ -5064,7 +5063,7 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
{ {
unsigned short len = ntohs(chunkHdr->chunkLength) - 4; unsigned short len = ntohs(chunkHdr->chunkLength) - 4;
unsigned short start = 0; unsigned short start = 0;
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
while(start < len) while(start < len)
{ {
@@ -5083,7 +5082,7 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
case RCTP_CHUNK_TYPE_SHUTDOWN: case RCTP_CHUNK_TYPE_SHUTDOWN:
case RCTP_CHUNK_TYPE_SHUTDOWNACK: case RCTP_CHUNK_TYPE_SHUTDOWNACK:
case RCTP_CHUNK_TYPE_ERROR: case RCTP_CHUNK_TYPE_ERROR:
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP: %d received [SHUTDOWN/ERROR] (%u)", session, chunktype); ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP: %d received [SHUTDOWN/ERROR] (%u)", session, chunktype);
ILibStun_SctpDisconnect(obj, session); ILibStun_SctpDisconnect(obj, session);
return; return;
@@ -5099,10 +5098,10 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
if (obj->OnConnect != NULL && o->state == 1) if (obj->OnConnect != NULL && o->state == 1)
{ {
o->state = 2; o->state = 2;
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
obj->OnConnect(obj, o, 1); obj->OnConnect(obj, o, 1);
if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state == 0) return; if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state == 0) return;
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
break; break;
case RCTP_CHUNK_TYPE_FWDTSN: case RCTP_CHUNK_TYPE_FWDTSN:
@@ -5224,10 +5223,10 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
if ((chunkflags & ILibSCTP_UnorderedFlag) != ILibSCTP_UnorderedFlag || ((chunkflags & 0x03) != 0x03)) if ((chunkflags & ILibSCTP_UnorderedFlag) != ILibSCTP_UnorderedFlag || ((chunkflags & 0x03) != 0x03))
{ {
// Only continue processing here, if 'Unordered Delivery' was not specified, or we have a partial fragment // Only continue processing here, if 'Unordered Delivery' was not specified, or we have a partial fragment
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
ILibStun_SctpProcessStreamData(obj, session, streamId, streamSeq, chunkflags, pid, data->UserData, chunksize - 16); ILibStun_SctpProcessStreamData(obj, session, streamId, streamSeq, chunkflags, pid, data->UserData, chunksize - 16);
if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state == 0) return; if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state == 0) return;
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
// Pull as many packets as we can out of the receive hold queue // Pull as many packets as we can out of the receive hold queue
@@ -5253,10 +5252,10 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
if (ILibSCTP_GetHoldingQueueFlags(ILibLinkedList_GetNode_Head(o->receiveHoldBuffer))->DataPropagated == 0) if (ILibSCTP_GetHoldingQueueFlags(ILibLinkedList_GetNode_Head(o->receiveHoldBuffer))->DataPropagated == 0)
{ {
// Only propagate this up, if it hasn't been propagated already // Only propagate this up, if it hasn't been propagated already
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
ILibStun_SctpProcessStreamData(obj, session, streamId, streamSeq, chunkflagsx, pid, payload->UserData, chunksizex - 16); ILibStun_SctpProcessStreamData(obj, session, streamId, streamSeq, chunkflagsx, pid, payload->UserData, chunksizex - 16);
if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state == 0) return; if (obj->dTlsSessions[session] == NULL || obj->dTlsSessions[session]->state == 0) return;
sem_wait(&(o->Lock)); ILibSpinLock_Lock(&(o->Lock));
} }
free(payload); free(payload);
@@ -5311,7 +5310,7 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_2, "...Buffer Index = %d", ptr); ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_2, "...Buffer Index = %d", ptr);
} }
ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "...Stopped Reading SCTP, rptr = %d", *rptr); ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "...Stopped Reading SCTP, rptr = %d", *rptr);
if (session == -1) { sem_post(&(o->Lock)); return; } if (session == -1) { ILibSpinLock_UnLock(&(o->Lock)); return; }
if (*rptr > 12) if (*rptr > 12)
{ {
#ifdef _REMOTELOGGING #ifdef _REMOTELOGGING
@@ -5325,7 +5324,7 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
ILibStun_SendSctpPacket(obj, session, rpacket, *rptr); ILibStun_SendSctpPacket(obj, session, rpacket, *rptr);
} }
*rptr = 0; *rptr = 0;
sem_post(&(o->Lock)); ILibSpinLock_UnLock(&(o->Lock));
} }
//! Pause processing of inbound SCTP data //! Pause processing of inbound SCTP data
@@ -5337,9 +5336,9 @@ void ILibSCTP_Pause(void* sctpSession)
struct ILibStun_dTlsSession* obj = (struct ILibStun_dTlsSession*)sctpSession; struct ILibStun_dTlsSession* obj = (struct ILibStun_dTlsSession*)sctpSession;
ILibRemoteLogging_printf(ILibChainGetLogger(obj->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP: %d was PAUSED by the USER", obj->sessionId); ILibRemoteLogging_printf(ILibChainGetLogger(obj->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP: %d was PAUSED by the USER", obj->sessionId);
sem_wait(&(obj->Lock)); ILibSpinLock_Lock(&(obj->Lock));
obj->flags |= DTLS_PAUSE_FLAG; obj->flags |= DTLS_PAUSE_FLAG;
sem_post(&(obj->Lock)); ILibSpinLock_UnLock(&(obj->Lock));
} }
//! Resume processing of inbound SCTP data //! Resume processing of inbound SCTP data
/*! /*!
@@ -5356,7 +5355,7 @@ void ILibSCTP_Resume(void* sctpSession)
ILibRemoteLogging_printf(ILibChainGetLogger(obj->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP: %d RESUME operation begin", obj->sessionId); ILibRemoteLogging_printf(ILibChainGetLogger(obj->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP: %d RESUME operation begin", obj->sessionId);
sem_wait(&(obj->Lock)); ILibSpinLock_Lock(&(obj->Lock));
if((obj->flags & DTLS_PAUSE_FLAG) == DTLS_PAUSE_FLAG) if((obj->flags & DTLS_PAUSE_FLAG) == DTLS_PAUSE_FLAG)
{ {
obj->flags ^= DTLS_PAUSE_FLAG; obj->flags ^= DTLS_PAUSE_FLAG;
@@ -5370,10 +5369,10 @@ void ILibSCTP_Resume(void* sctpSession)
// This packet is the next expected packet for the user // This packet is the next expected packet for the user
obj->userTSN = ntohl(payload->TSN); obj->userTSN = ntohl(payload->TSN);
pulled += payload->length; pulled += payload->length;
sem_post(&(obj->Lock)); ILibSpinLock_UnLock(&(obj->Lock));
ILibStun_SctpProcessStreamData(obj->parent, obj->sessionId, ntohs(payload->StreamID), ntohs(payload->StreamSequenceNumber), payload->flags, ntohl(payload->ProtocolID), payload->UserData, ntohs(payload->length) - 16); ILibStun_SctpProcessStreamData(obj->parent, obj->sessionId, ntohs(payload->StreamID), ntohs(payload->StreamSequenceNumber), payload->flags, ntohl(payload->ProtocolID), payload->UserData, ntohs(payload->length) - 16);
if (sobj->dTlsSessions[sessionID] == NULL || sobj->dTlsSessions[sessionID]->state == 0) return; // Referencing Dtls object this way, in case it was closed/freed by the user in the last call if (sobj->dTlsSessions[sessionID] == NULL || sobj->dTlsSessions[sessionID]->state == 0) return; // Referencing Dtls object this way, in case it was closed/freed by the user in the last call
sem_wait(&(obj->Lock)); ILibSpinLock_Lock(&(obj->Lock));
free(payload); free(payload);
ILibLinkedList_Remove(node); ILibLinkedList_Remove(node);
} }
@@ -5383,7 +5382,7 @@ void ILibSCTP_Resume(void* sctpSession)
} }
} }
if (pulled > 0) ReceiveHoldBuffer_Decrement(obj->receiveHoldBuffer, pulled); if (pulled > 0) ReceiveHoldBuffer_Decrement(obj->receiveHoldBuffer, pulled);
sem_post(&(obj->Lock)); ILibSpinLock_UnLock(&(obj->Lock));
ILibRemoteLogging_printf(ILibChainGetLogger(obj->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP: %d RESUME operation complete", sessionID); ILibRemoteLogging_printf(ILibChainGetLogger(obj->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_SCTP, ILibRemoteLogging_Flags_VerbosityLevel_1, "SCTP: %d RESUME operation complete", sessionID);
} }
@@ -5578,9 +5577,9 @@ ILibWebRTC_DataChannel_CloseStatus ILibWebRTC_CloseDataChannelEx(void *WebRTCMod
struct ILibStun_dTlsSession* obj = (struct ILibStun_dTlsSession*)WebRTCModule; struct ILibStun_dTlsSession* obj = (struct ILibStun_dTlsSession*)WebRTCModule;
ILibWebRTC_DataChannel_CloseStatus retVal; ILibWebRTC_DataChannel_CloseStatus retVal;
sem_wait(&obj->Lock); ILibSpinLock_Lock(&obj->Lock);
retVal = ILibWebRTC_CloseDataChannelEx2(WebRTCModule, streamIds, streamIdLength); retVal = ILibWebRTC_CloseDataChannelEx2(WebRTCModule, streamIds, streamIdLength);
sem_post(&obj->Lock); ILibSpinLock_UnLock(&obj->Lock);
return retVal; return retVal;
} }
@@ -5630,7 +5629,6 @@ void ILibStun_CreateDtlsSession(struct ILibStun_Module *obj, int sessionId, int
} }
else else
{ {
sem_destroy(&(obj->dTlsSessions[sessionId]->Lock));
ILibWebRTC_DestroySparseArrayTables(obj->dTlsSessions[sessionId]); ILibWebRTC_DestroySparseArrayTables(obj->dTlsSessions[sessionId]);
memset(obj->dTlsSessions[sessionId], 0, sizeof(struct ILibStun_dTlsSession)); memset(obj->dTlsSessions[sessionId], 0, sizeof(struct ILibStun_dTlsSession));
@@ -5650,7 +5648,7 @@ void ILibStun_CreateDtlsSession(struct ILibStun_Module *obj, int sessionId, int
obj->dTlsSessions[sessionId]->iceStateSlot = iceSlot; obj->dTlsSessions[sessionId]->iceStateSlot = iceSlot;
obj->dTlsSessions[sessionId]->state = 4; // Bryan: Changed this to 4 from 1, because we need to call SSL_do_handshake to determine when DTLS was successful obj->dTlsSessions[sessionId]->state = 4; // Bryan: Changed this to 4 from 1, because we need to call SSL_do_handshake to determine when DTLS was successful
obj->dTlsSessions[sessionId]->sessionId = sessionId; obj->dTlsSessions[sessionId]->sessionId = sessionId;
sem_init(&(obj->dTlsSessions[sessionId]->Lock), 0, 1); ILibSpinLock_Init(&(obj->dTlsSessions[sessionId]->Lock));
obj->dTlsSessions[sessionId]->parent = obj; obj->dTlsSessions[sessionId]->parent = obj;
memcpy_s(obj->dTlsSessions[sessionId]->remoteInterface, sizeof(struct sockaddr_in6), remoteInterface, INET_SOCKADDR_LENGTH(remoteInterface->sin6_family)); memcpy_s(obj->dTlsSessions[sessionId]->remoteInterface, sizeof(struct sockaddr_in6), remoteInterface, INET_SOCKADDR_LENGTH(remoteInterface->sin6_family));
obj->dTlsSessions[sessionId]->senderCredits = 4 * ILibRUDP_StartMTU; obj->dTlsSessions[sessionId]->senderCredits = 4 * ILibRUDP_StartMTU;
@@ -6057,7 +6055,7 @@ void ILibStun_OnUDP(ILibAsyncUDPSocket_SocketModule socketModule, char* buffer,
// If we have an existing dTLS session, process the data. This can also happen right after we create the session above. // If we have an existing dTLS session, process the data. This can also happen right after we create the session above.
if (existingSession != -1 && (obj->dTlsSessions[existingSession]->state == 1 || obj->dTlsSessions[existingSession]->state == 2 || obj->dTlsSessions[existingSession]->state == 4)) if (existingSession != -1 && (obj->dTlsSessions[existingSession]->state == 1 || obj->dTlsSessions[existingSession]->state == 2 || obj->dTlsSessions[existingSession]->state == 4))
{ {
sem_wait(&(obj->dTlsSessions[existingSession]->Lock)); ILibSpinLock_Lock(&(obj->dTlsSessions[existingSession]->Lock));
BIO_write(SSL_get_rbio(obj->dTlsSessions[existingSession]->ssl), buffer, bufferLength); BIO_write(SSL_get_rbio(obj->dTlsSessions[existingSession]->ssl), buffer, bufferLength);
if (obj->dTlsSessions[existingSession]->state == 4) if (obj->dTlsSessions[existingSession]->state == 4)
{ {
@@ -6088,12 +6086,12 @@ void ILibStun_OnUDP(ILibAsyncUDPSocket_SocketModule socketModule, char* buffer,
// SSL_WANT_READ most likely, so do nothing for now // SSL_WANT_READ most likely, so do nothing for now
break; break;
} }
sem_post(&(obj->dTlsSessions[existingSession]->Lock)); //ToDo: Bryan/Is this right? ILibSpinLock_UnLock(&(obj->dTlsSessions[existingSession]->Lock)); //ToDo: Bryan/Is this right?
} }
else else
{ {
i = SSL_read(obj->dTlsSessions[existingSession]->ssl, tbuffer, 4096); i = SSL_read(obj->dTlsSessions[existingSession]->ssl, tbuffer, 4096);
sem_post(&(obj->dTlsSessions[existingSession]->Lock)); ILibSpinLock_UnLock(&(obj->dTlsSessions[existingSession]->Lock));
if (i > 0) if (i > 0)
{ {
// We got new dTLS data // We got new dTLS data
@@ -6114,7 +6112,7 @@ void ILibStun_OnUDP(ILibAsyncUDPSocket_SocketModule socketModule, char* buffer,
if (obj->dTlsSessions[existingSession] != NULL && obj->dTlsSessions[existingSession]->state != 0) if (obj->dTlsSessions[existingSession] != NULL && obj->dTlsSessions[existingSession]->state != 0)
{ {
sem_wait(&(obj->dTlsSessions[existingSession]->Lock)); ILibSpinLock_Lock(&(obj->dTlsSessions[existingSession]->Lock));
if(obj->dTlsSessions[existingSession]->writeBIOBuffer->length > 0) if(obj->dTlsSessions[existingSession]->writeBIOBuffer->length > 0)
{ {
BIO_clear_retry_flags(obj->dTlsSessions[existingSession]->writeBIO); // Klocwork reports this could block, but this is a memory bio, so it will never block. BIO_clear_retry_flags(obj->dTlsSessions[existingSession]->writeBIO); // Klocwork reports this could block, but this is a memory bio, so it will never block.
@@ -6140,7 +6138,7 @@ void ILibStun_OnUDP(ILibAsyncUDPSocket_SocketModule socketModule, char* buffer,
if(obj->dTlsSessions[existingSession]->state == 4) {ILibWebRTC_DTLS_HandshakeDetect(obj, "S ", obj->dTlsSessions[existingSession]->writeBIOBuffer->data, 0, (int)obj->dTlsSessions[existingSession]->writeBIOBuffer->length);} if(obj->dTlsSessions[existingSession]->state == 4) {ILibWebRTC_DTLS_HandshakeDetect(obj, "S ", obj->dTlsSessions[existingSession]->writeBIOBuffer->data, 0, (int)obj->dTlsSessions[existingSession]->writeBIOBuffer->length);}
ignore_result(BIO_reset(obj->dTlsSessions[existingSession]->writeBIO)); ignore_result(BIO_reset(obj->dTlsSessions[existingSession]->writeBIO));
} }
sem_post(&(obj->dTlsSessions[existingSession]->Lock)); ILibSpinLock_UnLock(&(obj->dTlsSessions[existingSession]->Lock));
} }
} }
} }

View File

@@ -102,7 +102,7 @@ typedef struct ILibWebServer_Session_SystemData
int CloseOverrideFlag; // Close Override Flag int CloseOverrideFlag; // Close Override Flag
void *DisconnectFlagPointer; // DisconnectFlagPointer void *DisconnectFlagPointer; // DisconnectFlagPointer
sem_t SessionLock; // Session Lock ILibSpinLock SessionLock; // Session Lock
int ReferenceCounter; // Reference Counter; int ReferenceCounter; // Reference Counter;
int OverrideVirDirStruct; // Override VirDir Struct int OverrideVirDirStruct; // Override VirDir Struct
@@ -467,7 +467,7 @@ void ILibWebServer_OnConnect(void *AsyncServerSocketModule, void *ConnectionToke
ws->Reserved_Transport.PendingBytesPtr = (ILibTransport_PendingBytesToSendPtr)&ILibWebServer_Session_GetPendingBytesToSend; ws->Reserved_Transport.PendingBytesPtr = (ILibTransport_PendingBytesToSendPtr)&ILibWebServer_Session_GetPendingBytesToSend;
ws->ParentExtraMemory = wsm->ChainLink.ExtraMemoryPtr; ws->ParentExtraMemory = wsm->ChainLink.ExtraMemoryPtr;
sem_init(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock), 0, 1); // Initialize the SessionLock ILibSpinLock_Init(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock)); // Initialize the SessionLock
ILibWebServer_Session_GetSystemData(ws)->ReferenceCounter = 1; // Reference Counter, Initial count should be 1 ILibWebServer_Session_GetSystemData(ws)->ReferenceCounter = 1; // Reference Counter, Initial count should be 1
ILibWebServer_Session_GetSystemData(ws)->AsyncServerSocket = AsyncServerSocketModule; ILibWebServer_Session_GetSystemData(ws)->AsyncServerSocket = AsyncServerSocketModule;
ILibWebServer_Session_GetSystemData(ws)->ConnectionToken = ConnectionToken; ILibWebServer_Session_GetSystemData(ws)->ConnectionToken = ConnectionToken;
@@ -475,7 +475,7 @@ void ILibWebServer_OnConnect(void *AsyncServerSocketModule, void *ConnectionToke
//printf("#### ALLOCATED (%d) ####\r\n", ConnectionToken); //printf("#### ALLOCATED (%d) ####\r\n", ConnectionToken);
sem_init(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock), 0, 1); // Initialize the SessionLock ILibSpinLock_Init(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock)); // Initialize the SessionLock
ws->Parent = wsm; ws->Parent = wsm;
ws->User = wsm->User; ws->User = wsm->User;
*user = ws; *user = ws;
@@ -691,9 +691,9 @@ int ILibWebServer_ProcessWebSocketData(struct ILibWebServer_Session *ws, char* b
{ {
int sendResponse = 0; int sendResponse = 0;
// CONNECTION-CLOSE // CONNECTION-CLOSE
sem_wait(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock)); ILibSpinLock_Lock(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock));
if (ILibWebServer_Session_GetSystemData(ws)->WebSocketCloseFrameSent == 0) { ILibWebServer_Session_GetSystemData(ws)->WebSocketCloseFrameSent = 1; sendResponse = 1; } if (ILibWebServer_Session_GetSystemData(ws)->WebSocketCloseFrameSent == 0) { ILibWebServer_Session_GetSystemData(ws)->WebSocketCloseFrameSent = 1; sendResponse = 1; }
sem_post(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock)); ILibSpinLock_UnLock(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock));
if (sendResponse != 0) if (sendResponse != 0)
{ {
@@ -725,9 +725,9 @@ ILibExportMethod void ILibWebServer_WebSocket_Close(struct ILibWebServer_Session
int sent = 0; int sent = 0;
unsigned short code = htons(1000); // Normal Closure unsigned short code = htons(1000); // Normal Closure
sem_wait(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock)); ILibSpinLock_Lock(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock));
if (ILibWebServer_Session_GetSystemData(ws)->WebSocketCloseFrameSent == 0) { ILibWebServer_Session_GetSystemData(ws)->WebSocketCloseFrameSent = 1; sent = 1; } if (ILibWebServer_Session_GetSystemData(ws)->WebSocketCloseFrameSent == 0) { ILibWebServer_Session_GetSystemData(ws)->WebSocketCloseFrameSent = 1; sent = 1; }
sem_post(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock)); ILibSpinLock_UnLock(&(ILibWebServer_Session_GetSystemData(ws)->SessionLock));
if (sent != 0) if (sent != 0)
{ {
@@ -1539,13 +1539,13 @@ ILibExportMethod enum ILibWebServer_Status ILibWebServer_WebSocket_Send(struct I
} }
} }
sem_wait(&(ILibWebServer_Session_GetSystemData(session)->SessionLock)); // We need to do this, because we need to be able to correctly interleave sends ILibSpinLock_Lock(&(ILibWebServer_Session_GetSystemData(session)->SessionLock)); // We need to do this, because we need to be able to correctly interleave sends
RetVal = (enum ILibWebServer_Status)ILibAsyncServerSocket_Send(ILibWebServer_Session_GetSystemData(session)->AsyncServerSocket, ILibWebServer_Session_GetSystemData(session)->ConnectionToken, header, headerLen, ILibAsyncSocket_MemoryOwnership_USER); RetVal = (enum ILibWebServer_Status)ILibAsyncServerSocket_Send(ILibWebServer_Session_GetSystemData(session)->AsyncServerSocket, ILibWebServer_Session_GetSystemData(session)->ConnectionToken, header, headerLen, ILibAsyncSocket_MemoryOwnership_USER);
if (bufferLen > 0) if (bufferLen > 0)
{ {
RetVal = (enum ILibWebServer_Status)ILibAsyncServerSocket_Send(ILibWebServer_Session_GetSystemData(session)->AsyncServerSocket, ILibWebServer_Session_GetSystemData(session)->ConnectionToken, buffer, bufferLen, userFree); RetVal = (enum ILibWebServer_Status)ILibAsyncServerSocket_Send(ILibWebServer_Session_GetSystemData(session)->AsyncServerSocket, ILibWebServer_Session_GetSystemData(session)->ConnectionToken, buffer, bufferLen, userFree);
} }
sem_post(&(ILibWebServer_Session_GetSystemData(session)->SessionLock)); ILibSpinLock_UnLock(&(ILibWebServer_Session_GetSystemData(session)->SessionLock));
return RetVal; return RetVal;
} }
@@ -2018,9 +2018,9 @@ void ILibWebServer_AddRef(struct ILibWebServer_Session *session)
{ {
SESSION_TRACK(session, "AddRef"); SESSION_TRACK(session, "AddRef");
sem_wait(&(ILibWebServer_Session_GetSystemData(session)->SessionLock)); ILibSpinLock_Lock(&(ILibWebServer_Session_GetSystemData(session)->SessionLock));
++ILibWebServer_Session_GetSystemData(session)->ReferenceCounter; ++ILibWebServer_Session_GetSystemData(session)->ReferenceCounter;
sem_post(&(ILibWebServer_Session_GetSystemData(session)->SessionLock)); ILibSpinLock_UnLock(&(ILibWebServer_Session_GetSystemData(session)->SessionLock));
} }
/*! \fn ILibWebServer_Release(struct ILibWebServer_Session *session) /*! \fn ILibWebServer_Release(struct ILibWebServer_Session *session)
@@ -2034,7 +2034,7 @@ void ILibWebServer_Release(struct ILibWebServer_Session *session)
int OkToFree = 0; int OkToFree = 0;
SESSION_TRACK(session, "Release"); SESSION_TRACK(session, "Release");
sem_wait(&(ILibWebServer_Session_GetSystemData(session)->SessionLock)); ILibSpinLock_Lock(&(ILibWebServer_Session_GetSystemData(session)->SessionLock));
if (--ILibWebServer_Session_GetSystemData(session)->ReferenceCounter == 0) if (--ILibWebServer_Session_GetSystemData(session)->ReferenceCounter == 0)
{ {
// //
@@ -2043,7 +2043,7 @@ void ILibWebServer_Release(struct ILibWebServer_Session *session)
// //
OkToFree = 1; OkToFree = 1;
} }
sem_post(&(ILibWebServer_Session_GetSystemData(session)->SessionLock)); ILibSpinLock_UnLock(&(ILibWebServer_Session_GetSystemData(session)->SessionLock));
if (session->SessionInterrupted == 0) if (session->SessionInterrupted == 0)
{ {
ILibLifeTime_Remove(((struct ILibWebServer_StateModule*)session->Parent)->LifeTime, session); ILibLifeTime_Remove(((struct ILibWebServer_StateModule*)session->Parent)->LifeTime, session);
@@ -2056,7 +2056,6 @@ void ILibWebServer_Release(struct ILibWebServer_Session *session)
ILibWebClient_DestroyWebClientDataObject(ILibWebServer_Session_GetSystemData(session)->WebClientDataObject); ILibWebClient_DestroyWebClientDataObject(ILibWebServer_Session_GetSystemData(session)->WebClientDataObject);
} }
SESSION_TRACK(session, "** Destroyed **"); SESSION_TRACK(session, "** Destroyed **");
sem_destroy(&(ILibWebServer_Session_GetSystemData(session)->SessionLock));
if (ILibWebServer_Session_GetSystemData(session)->DigestTable != NULL) { ILibDestroyHashTree(ILibWebServer_Session_GetSystemData(session)->DigestTable); } if (ILibWebServer_Session_GetSystemData(session)->DigestTable != NULL) { ILibDestroyHashTree(ILibWebServer_Session_GetSystemData(session)->DigestTable); }
if (ILibWebServer_Session_GetSystemData(session)->WebSocketFragmentBuffer != NULL) { free(ILibWebServer_Session_GetSystemData(session)->WebSocketFragmentBuffer); } if (ILibWebServer_Session_GetSystemData(session)->WebSocketFragmentBuffer != NULL) { free(ILibWebServer_Session_GetSystemData(session)->WebSocketFragmentBuffer); }
if (ILibWebServer_Session_GetSystemData(session)->WebSocket_Request != NULL) { ILibDestructPacket((struct packetheader*)ILibWebServer_Session_GetSystemData(session)->WebSocket_Request); } if (ILibWebServer_Session_GetSystemData(session)->WebSocket_Request != NULL) { ILibDestructPacket((struct packetheader*)ILibWebServer_Session_GetSystemData(session)->WebSocket_Request); }