Skip to content

Commit

Permalink
Updated XSocket API, changed socket type with flags
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Nov 24, 2024
1 parent f32c1a1 commit 8312080
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 187 deletions.
9 changes: 7 additions & 2 deletions src/data/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,18 @@ void XArray_Destroy(xarray_t *pArr)
XArray_Clear(pArr);

xpool_t *pPool = pArr->pPool;
uint8_t nAlloc = pArr->nAlloc;
uint8_t nOwnPool = pArr->nOwnPool;

xfree(pPool, pArr->pData);
pArr->pData = NULL;
pArr->pPool = NULL;
pArr->nSize = 0;
pArr->nFixed = 0;
pArr->pPool = NULL;
pArr->nAlloc = 0;
pArr->nOwnPool = 0;

if (pArr->nAlloc) xfreen(pPool, pArr, sizeof(xarray_t));
if (nAlloc) xfreen(pPool, pArr, sizeof(xarray_t));
if (nOwnPool) XPool_Destroy(pPool);
}

Expand Down Expand Up @@ -232,8 +234,11 @@ size_t XArray_CheckSpace(xarray_t *pArr)
if (pArr->pData == NULL)
{
uint8_t nAlloc = pArr->nAlloc;
uint8_t nOwnPool = pArr->nOwnPool;
xarray_clear_cb_t clearCb = pArr->clearCb;

XArray_Init(pArr, pArr->pPool, XARRAY_INITIAL_SIZE, 0);
pArr->nOwnPool = nOwnPool;
pArr->clearCb = clearCb;
pArr->nAlloc = nAlloc;
}
Expand Down
16 changes: 7 additions & 9 deletions src/net/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1170,11 +1170,10 @@ XSTATUS XAPI_Listen(xapi_t *pApi, xapi_endpoint_t *pEndpt)
pApiData->nPort = pEndpt->nPort;
pApiData->eRole = XAPI_SERVER;

xsock_type_t eType = pEndpt->bTLS ?
XSOCK_SSL_PREFERED_SERVER :
XSOCK_TCP_SERVER;
uint32_t nFlags = XSOCK_TCP_SERVER;
if (pEndpt->bTLS) nFlags |= XSOCK_SSL;

XSock_Create(pSock, eType, pEndpt->pAddr, pEndpt->nPort);
XSock_Create(pSock, nFlags, pEndpt->pAddr, pEndpt->nPort);
if (pEndpt->bTLS) XSock_SetSSLCert(pSock, &pEndpt->certs);

XSock_ReuseAddr(pSock, XTRUE);
Expand Down Expand Up @@ -1247,12 +1246,11 @@ XSTATUS XAPI_Connect(xapi_t *pApi, xapi_endpoint_t *pEndpt)
pApiData->pSessionData = pEndpt->pSessionData;
pApiData->nPort = pEndpt->nPort;
pApiData->eRole = XAPI_CLIENT;

xsock_type_t eType = pEndpt->bTLS ?
XSOCK_SSL_PREFERED_CLIENT :
XSOCK_TCP_CLIENT;

XSock_Create(pSock, eType, pApiData->sAddr, pEndpt->nPort);
uint32_t nFlags = XSOCK_TCP_CLIENT;
if (pEndpt->bTLS) nFlags |= XSOCK_SSL;

XSock_Create(pSock, nFlags, pApiData->sAddr, pEndpt->nPort);
XSock_NonBlock(pSock, XTRUE);

if (pApiData->sock.nFD == XSOCK_INVALID)
Expand Down
14 changes: 7 additions & 7 deletions src/net/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1011,20 +1011,20 @@ xhttp_status_t XHTTP_LinkExchange(xhttp_t *pRequest, xhttp_t *pResponse, xlink_t
if (strncmp(pLink->sProtocol, "http", 4))
return XHTTP_StatusCb(pRequest, XHTTP_ERRPROTO);

xsock_type_t eType = XSOCK_TCP_CLIENT;
xsock_t sock;
uint32_t nFlags = XSOCK_TCP_CLIENT;

if (!strncmp(pLink->sProtocol, "https", 5))
{
eType = XSOCK_SSL_PREFERED_CLIENT;
nFlags |= XSOCK_SSL;
XSock_InitSSL();
}

if (xstrused(pLink->sUser) && xstrused(pLink->sPass) &&
XHTTP_SetAuthBasic(pRequest, pLink->sUser, pLink->sPass) <= 0)
return XHTTP_StatusCb(pRequest, XHTTP_ERRAUTH);

if (XSock_Setup(&sock, eType, pLink->sHost) == XSOCK_INVALID)
if (XSock_Setup(&sock, nFlags, pLink->sHost) == XSOCK_INVALID)
return XHTTP_StatusCb(pRequest, XHTTP_ERRCONNECT);

if (pRequest->nTimeout)
Expand Down Expand Up @@ -1083,13 +1083,13 @@ xhttp_status_t XHTTP_LinkPerform(xhttp_t *pHttp, xlink_t *pLink, const uint8_t *
if (strncmp(pLink->sProtocol, "http", 4))
return XHTTP_StatusCb(pHttp, XHTTP_ERRPROTO);

xsock_type_t eType = XSOCK_TCP_CLIENT;
uint32_t nFlags = XSOCK_TCP_CLIENT;
xsock_addr_t addrInfo;
xsock_t sock;

if (!strncmp(pLink->sProtocol, "https", 5))
{
eType = XSOCK_SSL_PREFERED_CLIENT;
nFlags |= XSOCK_SSL;
XSock_InitSSL();
}

Expand All @@ -1101,7 +1101,7 @@ xhttp_status_t XHTTP_LinkPerform(xhttp_t *pHttp, xlink_t *pLink, const uint8_t *
return XHTTP_StatusCb(pHttp, XHTTP_ERRRESOLVE);

addrInfo.nPort = addrInfo.nPort ? addrInfo.nPort :
(XSockType_IsSSL(eType) ? XHTTP_SSL_PORT : XHTTP_DEF_PORT);
(XSockFlags_CheckSSL(nFlags) ? XHTTP_SSL_PORT : XHTTP_DEF_PORT);

if (XHTTP_CHECK_FLAG(pHttp->nCbTypes, XHTTP_STATUS) && pHttp->callback != NULL)
{
Expand All @@ -1120,7 +1120,7 @@ xhttp_status_t XHTTP_LinkPerform(xhttp_t *pHttp, xlink_t *pLink, const uint8_t *
pHttp->callback(pHttp, &cbCtx);
}

if (XSock_Open(&sock, eType, &addrInfo) == XSOCK_INVALID)
if (XSock_Open(&sock, nFlags, &addrInfo) == XSOCK_INVALID)
return XHTTP_StatusCb(pHttp, XHTTP_ERRCONNECT);

if (pHttp->nTimeout)
Expand Down
Loading

0 comments on commit 8312080

Please sign in to comment.