mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-27 07:59:41 +08:00
Pre-allocate client/server/tmp/etc buffers with client structure
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI MacOS / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI Windows / ${{ matrix.target }} (windows-2022) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (windows-2022) (push) Waiting to run
C/C++ CI cmake / ubuntu-latest (wolfSSL) (push) Waiting to run
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI MacOS / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI Windows / ${{ matrix.target }} (windows-2022) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (windows-2022) (push) Waiting to run
C/C++ CI cmake / ubuntu-latest (wolfSSL) (push) Waiting to run
This commit is contained in:
1 parent
b9297dc4cf
commit
a20f646169
36 files changed
+431
-492
No files matched your search
+88
-78
@@ -70,112 +70,122 @@ int sockrecvfrom(struct clientparam *param, SOCKET sock, struct sockaddr * sin,
|
||||
return res;
|
||||
}
|
||||
|
||||
int sockgetcharcli(struct clientparam * param, int timeosec, int timeousec){
|
||||
int len;
|
||||
|
||||
if(!param->clibuf){
|
||||
if(!(param->clibuf = malloc(SRVBUFSIZE))) return 0;
|
||||
param->clibufsize = SRVBUFSIZE;
|
||||
param->clioffset = param->cliinbuf = 0;
|
||||
/* Starts an empty buffer over from its beginning, and goes back to the
|
||||
connection's own buffer if a filter needed a bigger one. Does nothing
|
||||
while anything is left unread. */
|
||||
void bufrelease(struct clientparam * param, DIRECTION which){
|
||||
if(which){
|
||||
if(param->srvinbuf != param->srvoffset) return;
|
||||
param->srvinbuf = param->srvoffset = 0;
|
||||
if(param->srvbuf == param->srvbufdata) return;
|
||||
free(param->srvbuf);
|
||||
param->srvbuf = param->srvbufdata;
|
||||
param->srvbufsize = SRVBUFSIZE(param->srv);
|
||||
}
|
||||
if(param->cliinbuf && param->clioffset < param->cliinbuf){
|
||||
return (int)param->clibuf[param->clioffset++];
|
||||
else {
|
||||
if(param->cliinbuf != param->clioffset) return;
|
||||
param->cliinbuf = param->clioffset = 0;
|
||||
if(param->clibuf == param->clibufdata) return;
|
||||
free(param->clibuf);
|
||||
param->clibuf = param->clibufdata;
|
||||
param->clibufsize = SRVBUFSIZE(param->srv);
|
||||
}
|
||||
param->clioffset = param->cliinbuf = 0;
|
||||
if ((len = sockrecvfrom(param, param->clisock, (struct sockaddr *)¶m->sincr, param->clibuf, param->clibufsize, timeosec*1000 + timeousec))<=0) return EOF;
|
||||
param->cliinbuf = len;
|
||||
param->clioffset = 1;
|
||||
return (int)*param->clibuf;
|
||||
}
|
||||
|
||||
/* Put bytes back in front of whatever the client has not been read yet, so a
|
||||
service which has already taken a request off the socket can hand it to
|
||||
another one, which reads it the way it reads anything else. */
|
||||
int pushbackcli(struct clientparam * param, const unsigned char * data, int len){
|
||||
unsigned left = 0;
|
||||
unsigned need;
|
||||
/* Makes room for size bytes from the start of what is unread, which is
|
||||
moved there: a body to be filtered whole is read into one buffer, which
|
||||
may have to be bigger than the connection's own. Returns 1 when that
|
||||
much cannot be had. */
|
||||
int bufroom(struct clientparam * param, DIRECTION which, unsigned long size){
|
||||
unsigned char **buf = which? ¶m->srvbuf : ¶m->clibuf;
|
||||
unsigned char *own = which? param->srvbufdata : param->clibufdata;
|
||||
unsigned *inbuf = which? ¶m->srvinbuf : ¶m->cliinbuf;
|
||||
unsigned *offset = which? ¶m->srvoffset : ¶m->clioffset;
|
||||
unsigned *bufsize = which? ¶m->srvbufsize : ¶m->clibufsize;
|
||||
unsigned left = *inbuf - *offset;
|
||||
unsigned char *nb;
|
||||
|
||||
if(len <= 0) return 0;
|
||||
if(param->clibuf) left = param->cliinbuf - param->clioffset;
|
||||
need = (unsigned)len + left;
|
||||
|
||||
if(!param->clibuf){
|
||||
if(!(param->clibuf = malloc(need > SRVBUFSIZE? need : SRVBUFSIZE))) return 1;
|
||||
param->clibufsize = need > SRVBUFSIZE? need : SRVBUFSIZE;
|
||||
if(*offset){
|
||||
memmove(*buf, *buf + *offset, left);
|
||||
*inbuf = left;
|
||||
*offset = 0;
|
||||
}
|
||||
else if(param->clibufsize < need){
|
||||
unsigned char *nb = realloc(param->clibuf, need);
|
||||
|
||||
if(!nb) return 1;
|
||||
param->clibuf = nb;
|
||||
param->clibufsize = need;
|
||||
}
|
||||
if(left) memmove(param->clibuf + len, param->clibuf + param->clioffset, left);
|
||||
memcpy(param->clibuf, data, (size_t)len);
|
||||
param->clioffset = 0;
|
||||
param->cliinbuf = need;
|
||||
if(size <= *bufsize) return 0;
|
||||
if((unsigned long)(unsigned)size != size || !(nb = malloc(size))) return 1;
|
||||
memcpy(nb, *buf, left);
|
||||
if(*buf != own) free(*buf);
|
||||
*buf = nb;
|
||||
*bufsize = (unsigned)size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long sockfillbuffcli(struct clientparam * param, unsigned long size, int timeosec){
|
||||
int sockgetcharcli(struct clientparam * param, int timeosec, int timeousec){
|
||||
int len;
|
||||
unsigned keep = 0;
|
||||
|
||||
if(!param->clibuf) return 0;
|
||||
if(param->cliinbuf == param->clioffset){
|
||||
param->cliinbuf = param->clioffset = 0;
|
||||
if(param->cliinbuf && param->clioffset < param->cliinbuf){
|
||||
return (int)param->clibuf[param->clioffset++];
|
||||
}
|
||||
else if(param->clioffset){
|
||||
memmove(param->clibuf, param->clibuf + param->clioffset, param->cliinbuf - param->clioffset);
|
||||
param->cliinbuf -= param->clioffset;
|
||||
param->clioffset = 0;
|
||||
if(param->climark == -2) return EOF; /* nothing more until it is cleared */
|
||||
if(param->climark >= 0){
|
||||
/* what was read since the mark stays, moved to the start */
|
||||
keep = param->cliinbuf - (unsigned)param->climark;
|
||||
if(param->climark) memmove(param->clibuf, param->clibuf + param->climark, keep);
|
||||
param->climark = 0;
|
||||
param->clioffset = param->cliinbuf = keep;
|
||||
if(keep >= param->clibufsize){
|
||||
param->climark = -2;
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
if(size <= param->cliinbuf) return size;
|
||||
size -= param->cliinbuf;
|
||||
if((len = sockrecvfrom(param, param->clisock, (struct sockaddr *)¶m->sincr, param->clibuf + param->cliinbuf, (param->clibufsize - param->cliinbuf) < size? param->clibufsize - param->cliinbuf:size, timeosec*1000)) > 0){
|
||||
param->cliinbuf += len;
|
||||
}
|
||||
return param->cliinbuf;
|
||||
else bufrelease(param, CLIENT);
|
||||
if ((len = sockrecvfrom(param, param->clisock, (struct sockaddr *)¶m->sincr, param->clibuf + keep, param->clibufsize - keep, timeosec*1000 + timeousec))<=0) return EOF;
|
||||
param->cliinbuf = keep + len;
|
||||
param->clioffset = keep + 1;
|
||||
return (int)param->clibuf[keep];
|
||||
}
|
||||
|
||||
unsigned long sockfillbuffsrv(struct clientparam * param, unsigned long size, int timeosec){
|
||||
/* Waits until size bytes are in the buffer, from its start, without taking
|
||||
them: for a look at what comes before it is read the usual way, or for
|
||||
something which has to be had whole. Stops short at the end of the
|
||||
buffer, at the end of the stream, or when nothing comes in time; returns
|
||||
how much is there, at most size. */
|
||||
unsigned long sockfillbuf(struct clientparam * param, DIRECTION which, unsigned long size, int timeosec){
|
||||
unsigned char *buf = which? param->srvbuf : param->clibuf;
|
||||
unsigned *inbuf = which? ¶m->srvinbuf : ¶m->cliinbuf;
|
||||
unsigned *offset = which? ¶m->srvoffset : ¶m->clioffset;
|
||||
unsigned bufsize = which? param->srvbufsize : param->clibufsize;
|
||||
unsigned want;
|
||||
int len;
|
||||
|
||||
if(!param->srvbuf) return 0;
|
||||
if(param->srvinbuf == param->srvoffset){
|
||||
param->srvinbuf = param->srvoffset = 0;
|
||||
if(*offset){
|
||||
memmove(buf, buf + *offset, *inbuf - *offset);
|
||||
*inbuf -= *offset;
|
||||
*offset = 0;
|
||||
}
|
||||
else if(param->srvoffset){
|
||||
memmove(param->srvbuf, param->srvbuf + param->srvoffset, param->srvinbuf - param->srvoffset);
|
||||
param->srvinbuf -= param->srvoffset;
|
||||
param->srvoffset = 0;
|
||||
while(*inbuf < size && *inbuf < bufsize){
|
||||
want = (size - *inbuf < bufsize - *inbuf)? (unsigned)(size - *inbuf) : bufsize - *inbuf;
|
||||
len = which?
|
||||
sockrecvfrom(param, param->remsock, (struct sockaddr *)¶m->sinsr, buf + *inbuf, (int)want, timeosec*1000)
|
||||
: sockrecvfrom(param, param->clisock, (struct sockaddr *)¶m->sincr, buf + *inbuf, (int)want, timeosec*1000);
|
||||
if(len <= 0) break;
|
||||
*inbuf += len;
|
||||
if(which){
|
||||
param->nreads++;
|
||||
param->statssrv64 += len;
|
||||
}
|
||||
}
|
||||
if(size <= param->srvinbuf) return size;
|
||||
size -= param->srvinbuf;
|
||||
if((len = sockrecvfrom(param, param->remsock, (struct sockaddr *)¶m->sinsr, param->srvbuf + param->srvinbuf, (param->srvbufsize - param->srvinbuf) < size? param->srvbufsize - param->srvinbuf:size, timeosec*1000)) > 0){
|
||||
param->srvinbuf += len;
|
||||
param->nreads++;
|
||||
param->statssrv64 += len;
|
||||
}
|
||||
return param->srvinbuf;
|
||||
return *inbuf < size? *inbuf : size;
|
||||
}
|
||||
|
||||
|
||||
int sockgetcharsrv(struct clientparam * param, int timeosec, int timeousec){
|
||||
int len;
|
||||
int bufsize;
|
||||
|
||||
if(!param->srvbuf){
|
||||
bufsize = SRVBUFSIZE;
|
||||
if(param->ndatfilterssrv > 0 && bufsize < 32768) bufsize = 32768;
|
||||
if(!(param->srvbuf = malloc(bufsize))) return 0;
|
||||
param->srvbufsize = bufsize;
|
||||
param->srvoffset = param->srvinbuf = 0;
|
||||
|
||||
}
|
||||
if(param->srvinbuf && param->srvoffset < param->srvinbuf){
|
||||
return (int)param->srvbuf[param->srvoffset++];
|
||||
}
|
||||
param->srvoffset = param->srvinbuf = 0;
|
||||
bufrelease(param, SERVER);
|
||||
if ((len = sockrecvfrom(param, param->remsock, (struct sockaddr *)¶m->sinsr, param->srvbuf, param->srvbufsize, timeosec*1000 + timeousec))<=0) return EOF;
|
||||
param->srvinbuf = len;
|
||||
param->srvoffset = 1;
|
||||
|
||||
Reference in new issue
Block a user