Skip to content
Snippets Groups Projects
Commit 74073bc0 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Remove sendfilesocket() and recvfilesocket()

Make js_socket_sendfilesocket() suck a lot less.
This commit brought to you with limited rants by Synchronet 3.20b

"Warning: Your BBS may become habit forming."

You could run Synchronet or you could settle for mediocrity.

Once in a great while, there comes BBS software that really makes waves.
Get out your surfboard.

The best BBS software is the most expensive BBS software.
NOT!

If you had three wishes, you could toss the other two.

They couldn't top Synchronet.  So we did.

Accept the inevitable, switch to Synchronet
parent e6a5b4dd
No related branches found
No related tags found
1 merge request!488Overhaul LZH code
Pipeline #7577 passed
...@@ -68,7 +68,7 @@ static bool js_socket_peek_byte(JSContext *cx, js_socket_private_t *p); ...@@ -68,7 +68,7 @@ static bool js_socket_peek_byte(JSContext *cx, js_socket_private_t *p);
static JSBool js_socket_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp); static JSBool js_socket_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
static ptrdiff_t js_socket_recv(JSContext *cx, js_socket_private_t *p, void *buf, size_t len, int flags, int timeout); static ptrdiff_t js_socket_recv(JSContext *cx, js_socket_private_t *p, void *buf, size_t len, int flags, int timeout);
static JSBool js_socket_resolve(JSContext *cx, JSObject *obj, jsid id); static JSBool js_socket_resolve(JSContext *cx, JSObject *obj, jsid id);
static off_t js_socket_sendfilesocket(js_socket_private_t *p, int file, off_t *offset, off_t count); static off_t js_socket_sendfilesocket(js_socket_private_t *p, int file);
static ptrdiff_t js_socket_sendsocket(js_socket_private_t *p, const void *msg, size_t len, int flush); static ptrdiff_t js_socket_sendsocket(js_socket_private_t *p, const void *msg, size_t len, int flush);
static JSBool js_socket_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp); static JSBool js_socket_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp);
static JSBool js_install_event(JSContext *cx, uintN argc, jsval *arglist, bool once); static JSBool js_install_event(JSContext *cx, uintN argc, jsval *arglist, bool once);
...@@ -393,64 +393,45 @@ static ptrdiff_t js_socket_sendsocket(js_socket_private_t *p, const void *msg, s ...@@ -393,64 +393,45 @@ static ptrdiff_t js_socket_sendsocket(js_socket_private_t *p, const void *msg, s
return total; return total;
} }
static off_t js_socket_sendfilesocket(js_socket_private_t *p, int file, off_t *offset, off_t count) static off_t js_socket_sendfilesocket(js_socket_private_t *p, int file)
{ {
char buf[1024*16]; char buf[1024*16];
off_t len;
int rd;
int wr=0;
off_t total = 0; off_t total = 0;
int i;
if(p->session==-1)
return (int)sendfilesocket(p->sock, file, offset, count);
len=filelength(file);
if(offset!=NULL)
if(lseek(file,*offset,SEEK_SET)<0)
return(-1);
if(count<1 || count>len) {
count=len;
count-=tell(file); /* don't try to read beyond EOF */
}
if(count<0) {
errno=EINVAL;
return(-1);
}
while(total<count) { for (;;) {
rd=read(file,buf,sizeof(buf)); ssize_t rd = read(file, buf, sizeof(buf));
if(rd==-1) { if (rd < 0) {
if (p->session != -1)
do_CryptFlush(p); do_CryptFlush(p);
return (-1); return (-1);
} }
if (rd == 0) if (rd == 0)
break; break;
for(i=wr=0;i<rd;i+=wr) { size_t sent = 0;
wr=js_socket_sendsocket(p,buf+i,rd-i,false); while (sent < rd) {
if(wr>0) ptrdiff_t wr = js_socket_sendsocket(p, buf + sent, rd - sent, false);
continue; if (wr > 0) {
if(wr==SOCKET_ERROR && SOCKET_ERRNO==EWOULDBLOCK) { sent += wr;
}
else if (wr == SOCKET_ERROR && SOCKET_ERRNO == EWOULDBLOCK) {
wr=0; wr=0;
SLEEP(1); SLEEP(1);
continue;
} }
else {
if (p->session != -1)
do_CryptFlush(p); do_CryptFlush(p);
return (wr); return (wr);
} }
if(i!=rd) { }
if (sent != rd) {
if (p->session != -1)
do_CryptFlush(p); do_CryptFlush(p);
return (-1); return (-1);
} }
total += rd; total += rd;
} }
if(offset!=NULL) if (p->session != -1)
(*offset)+=total;
do_CryptFlush(p); do_CryptFlush(p);
return (total); return (total);
} }
...@@ -1328,7 +1309,7 @@ js_sendfile(JSContext *cx, uintN argc, jsval *arglist) ...@@ -1328,7 +1309,7 @@ js_sendfile(JSContext *cx, uintN argc, jsval *arglist)
return(JS_TRUE); return(JS_TRUE);
} }
len = js_socket_sendfilesocket(p, file, NULL, 0); len = js_socket_sendfilesocket(p, file);
close(file); close(file);
if(len > 0) { if(len > 0) {
dbprintf(false, p, "sent %"PRIdOFF" bytes",len); dbprintf(false, p, "sent %"PRIdOFF" bytes",len);
......
...@@ -153,115 +153,6 @@ socket_option_t* getSocketOptionList(void) ...@@ -153,115 +153,6 @@ socket_option_t* getSocketOptionList(void)
return(socket_options); return(socket_options);
} }
// TODO: Only called with *offset == NULL and count == 0...
off_t sendfilesocket(int sock, int file, off_t *offset, off_t count)
{
char buf[1024*16];
off_t len;
off_t total=0;
// TODO: Race condition here... length may change.
// But note that js_socket_sendfilesocket() reimplements all of this
// for encrypted sockets.
len = filelength(file);
if(offset!=NULL)
if(lseek(file,*offset,SEEK_SET)<0)
return(-1);
if (count < 1 || count > len) {
count = len;
count -= tell(file); /* don't try to read beyond EOF (why not? --- Deuce) */
}
if (count < 0) {
errno = EINVAL;
return(-1);
}
while (total < count) {
ssize_t rd = read(file, buf, sizeof(buf));
ssize_t sent = 0;
if (rd < 0) // Error
return(-1);
if (rd == 0) // EOF
break;
while (sent < rd) {
ssize_t wr = sendsocket(sock, buf + sent, rd - sent);
if (wr > 0 && wr <= (rd - sent)) {
sent += wr;
}
else if (wr == SOCKET_ERROR && SOCKET_ERRNO == EWOULDBLOCK) {
SLEEP(1);
}
else {
// TODO: This is sketchy to return 0 on write failure
return(wr);
}
}
total += rd;
}
if (offset != NULL)
(*offset) += total;
return(total);
}
off_t recvfilesocket(int sock, int file, off_t *offset, off_t count)
{
/* Writes a file from a socket -
*
* sock - Socket to read from
* file - File descriptior to write to
* MUST be open and writeable
* offset - pointer to file offset to start writing at
* is set to offset writing STOPPED
* on return
* count - number of bytes to read/write
*
* returns -1 if an error occurse, otherwise
* returns number ob bytes written and sets offset
* to the new offset
*/
char* buf;
ssize_t rd;
ssize_t wr;
if(count<1) {
errno=ERANGE;
return(-1);
}
if((buf=(char*)malloc((size_t)count))==NULL) {
errno=ENOMEM;
return(-1);
}
if(offset!=NULL) {
if(lseek(file,*offset,SEEK_SET)<0) {
free(buf);
return(-1);
}
}
rd=read(sock,buf,(size_t)count);
if(rd!=count) {
free(buf);
return(-1);
}
wr=write(file,buf,rd);
if(offset!=NULL)
(*offset)+=wr;
free(buf);
return(wr);
}
/* Return true if connected, optionally sets *rd_p to true if read data available */ /* Return true if connected, optionally sets *rd_p to true if read data available */
/* /*
* The exact conditions where rd_p is set to true and the return value * The exact conditions where rd_p is set to true and the return value
......
...@@ -239,9 +239,6 @@ extern "C" { ...@@ -239,9 +239,6 @@ extern "C" {
DLLEXPORT socket_option_t* getSocketOptionList(void); DLLEXPORT socket_option_t* getSocketOptionList(void);
DLLEXPORT int getSocketOptionByName(const char* name, int* level); DLLEXPORT int getSocketOptionByName(const char* name, int* level);
DLLEXPORT off_t sendfilesocket(int sock, int file, off_t* offset, off_t count);
DLLEXPORT off_t recvfilesocket(int sock, int file, off_t* offset, off_t count);
DLLEXPORT bool socket_check(SOCKET sock, bool* rd_p, bool* wr_p, DWORD timeout); DLLEXPORT bool socket_check(SOCKET sock, bool* rd_p, bool* wr_p, DWORD timeout);
DLLEXPORT int retry_bind(SOCKET s, const struct sockaddr *addr, socklen_t addrlen DLLEXPORT int retry_bind(SOCKET s, const struct sockaddr *addr, socklen_t addrlen
,uint retries, uint wait_secs, const char* prot ,uint retries, uint wait_secs, const char* prot
......
...@@ -976,7 +976,7 @@ do_xp_play_sample(unsigned char *sampo, size_t sz, int *freed) ...@@ -976,7 +976,7 @@ do_xp_play_sample(unsigned char *sampo, size_t sz, int *freed)
size_t wr = 0; size_t wr = 0;
while (wr < sz) { while (wr < sz) {
ssize_t i = write(dsp, samp + wr, sz - wr); ssize_t i = write(dsp, samp + wr, sz - wr);
if (i >= 0 && i <= (sz - wr)) if (i >= 0)
wr += i; wr += i;
else else
return false; return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment