:
DLL, , .
, DLL.
(TLS, 7). DLL, . , .
, . , , TLS . , , .
, TLS , .
: DLL
12.4 DLL, ( "CS", character string ), (socket streaming functions): SendCSMessage ReceiveCSMessage, DllMain (. 5). , ReceiveMessage, , 12.1 12.2, .
DllMain TLS DLL.
( DLL_THREAD_DETACH) ; , , , , .
, , , , , DLL .
DLL , 12.1 12.2, Web- .
12.4. SendReceiveSKST: DLL
/* SendReceiveSKST. DLL . */
/* */
/* ('\0'), . */
/* */
/* . */
/* */
/* (Thread Local Storage, TLS), */
/* " ". */
#define _NOEXCLUSIONS
#include "EvryThng.h"
#include "ClntSrvr.h" /* . */
|
|
typedef struct STATIC_BUF_T {
/* "static_buf" "static_buf_len" . */
/* ( ) , */
/* . */
char static_buf[MAX_RQRS_LEN];
LONG32 static_buf_len;
} STATIC_BUF;
static DWORD TlsIx = 0; /* TLS .*/
/* :
static char static_buf [MAX_RQRS_LEN];
static LONG32 static_buf_len; */
/* DLL. */
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
STATIC_BUF * pBuf;
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
TlsIx = TlsAlloc();
/* , . */
case DLL_THREAD_ATTACH:
/* , . */
TlsSetValue(TlsIx, NULL);
return TRUE; /* . */
case DLL_PROCESS_DETACH:
/* . */
pBuf = TlsGetValue(TlsIx);
if (pBuf!= NULL) {
free(pBuf);
pBuf = NULL;
}
return TRUE;
case DLL_THREAD_DETACH:
pBuf = TlsGetValue(TlsIx);
if (pBuf!= NULL) {
free(pBuf);
pBuf = NULL;
}
return TRUE;
}
}
_declspec(dllexport)
BOOL ReceiveCSMessage(REQUEST *pRequest, SOCKET sd) {
/* TRUE . */
BOOL Disconnect = FALSE;
LONG32 nRemainRecv = 0, nXfer, k; /* . */
LPSTR pBuffer, message;
CHAR TempBuf[MAX_RQRS_LEN + 1];
STATIC_BUF *p;
p = (STATIC_BUF *)TlsGetValue(TlsIx);
if (p == NULL) { /* . */
/* , */
/* . TLS . */
= malloc(sizeof(STATIC_BUF));
TlsSetValue(TlsIx, p);
if (p == NULL) return TRUE; /* . */
p->static_buf_len = 0; /* . */
}
message = pRequest->Record;
/* , . */
for (k = 0; k < p->static_buf_len && p->static_buf[k]!= '\0'; k++) {
message[k] = p->static_buf[k];
} /* k . */
if (k < p->static_buf_len) { /* . */
message[k] = '\0';
p->static_buf_len = (k + 1); /* . */
memcpy(p->static_buf, &(p->static_buf[k + 1]), p->static_buf_len);
return FALSE; /* . */
}
/* . .*/
nRemainRecv = sizeof(TempBuf) 1 p->static_buf_len;
pBuffer = message + p->static_buf_len;
p->static_buf_len = 0;
while (nRemainRecv > 0 &&!Disconnect) {
nXfer = recv(sd, TempBuf, nRemainRecv, 0);
if (nXfer <= 0) {
Disconnect = TRUE;
continue;
}
nRemainRecv = nXfer;
/* , . */
for (k =0; k < nXfer && TempBuf[k]!= '\0'; k++) {
*pBuffer = TempBuf[k];
|
|
pBuffer++;
}
if (k >= nXfer) { /* , */
nRemainRecv = nXfer;
} else { /* . */
*pBuffer = '\0';
nRemainRecv = 0;
memcpy(p->static_buf, &TempBuf[k + 1], nXfer k 1);
p->static_buf_len = nXfer k 1;
}
}
return Disconnect;
}
_declspec(dllexport)
BOOL SendCSMessage(RESPONSE *pResponse, SOCKET sd) {
/* sd. */
BOOL Disconnect = FALSE;
LONG32 nRemainSend, nXfer;
LPSTR pBuffer;
pBuffer = pResponse->Record;
nRemainSend = strlen(pBuffer) + 1;
while (nRemainSend > 0 &&!Disconnect) {
/* , . */
nXfer = send(sd, pBuffer, nRemainSend, 0);
if (nXfer <= 0) {
fprintf(stderr, "\n ");
Disconnect = TRUE;
}
nRemainSend =nXfer;
pBuffer += nXfer;
}
return Disconnect;
}