Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Win32 socket improvement #7974

Closed
vicuna opened this issue Dec 19, 2002 · 2 comments
Closed

Win32 socket improvement #7974

vicuna opened this issue Dec 19, 2002 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Dec 19, 2002

Original bug ID: 1499
Reporter: administrator
Status: closed
Resolution: fixed
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Hello,

I look at socket function in win32 unix library and propose make some
changes in it

  1. otherlibs/win32unix/accept.c - function setsocketopt erase status of
    last accept call, WSAGetLastError must be called before it.

  2. function set_nonblock and clear_nonblock, not implement in current
    version, but I read MSDN, and fount what win32 socket support
    nonblocking mode. I propose include these function in library.

  3. win32 version of select, raise error if call it with empty lists of
    files descr, but linux version handle this situation. I propose emulate
    behaviour of linux version. If select catch empty lists of fd, it call
    win32 function Sleep, for emulate time-out.

--
Artem Prisyznuk
tema@sit.kiev.ua


diff -ru ocaml-3.06/otherlibs/win32unix/accept.c G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/accept.c
--- ocaml-3.06/otherlibs/win32unix/accept.c Fri Jun 7 09:49:42 2002
+++ G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/accept.c Thu Dec 12 11:13:08 2002
@@ -29,6 +29,7 @@
int oldvalue, oldvaluelen, newvalue, retcode;
union sock_addr_union addr;
socklen_param_type addr_len;

  • int err_code = 0;

    oldvaluelen = sizeof(oldvalue);
    retcode = getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
    @@ -43,13 +44,15 @@
    enter_blocking_section();
    snew = accept(sconn, &addr.s_gen, &addr_len);
    leave_blocking_section();

  • if( snew == INVALID_SOCKET )

  • err_code = WSAGetLastError ();
    if (retcode == 0) {
    /* Restore initial mode */
    setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
    (char *) &oldvalue, oldvaluelen);
    }
    if (snew == INVALID_SOCKET) {

  • win32_maperr(WSAGetLastError());
  • win32_maperr(err_code);
    uerror("accept", Nothing);
    }
    Begin_roots2 (fd, adr)
    diff -ru ocaml-3.06/otherlibs/win32unix/connect.c G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/connect.c
    --- ocaml-3.06/otherlibs/win32unix/connect.c Fri Jun 7 09:49:42 2002
    +++ G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/connect.c Thu Dec 12 11:13:08 2002
    @@ -36,3 +36,35 @@
    }
    return Val_unit;
    }

+CAMLprim value unix_set_nonblock(socket)

  • value socket;
    

+{

  • SOCKET s = Socket_val(socket);
  • int retcode;
  • int non_block = 1;
  • retcode = ioctlsocket(s, FIONBIO, &non_block);
  • if (retcode) {
  • win32_maperr(WSAGetLastError());
  • uerror("unix_set_nonblock", Nothing);
  • }
  • return Val_unit;
    +}

+CAMLprim value unix_clear_nonblock(socket)

  • value socket;
    

+{

  • SOCKET s = Socket_val(socket);
  • int retcode;
  • int non_block = 0;
  • union sock_addr_union addr;
  • socklen_param_type addr_len;
  • retcode = ioctlsocket(s, FIONBIO, &non_block);
  • if (retcode) {
  • win32_maperr(WSAGetLastError());
  • uerror("unix_clear_nonblock", Nothing);
  • }
  • return Val_unit;
    +}
    diff -ru ocaml-3.06/otherlibs/win32unix/errmsg.c G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/errmsg.c
    --- ocaml-3.06/otherlibs/win32unix/errmsg.c Fri Dec 7 13:40:44 2001
    +++ G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/errmsg.c Thu Dec 12 11:13:08 2002
    @@ -37,6 +37,8 @@
    sizeof(buffer),
    NULL))
    return copy_string(buffer);
  • return copy_string("unknown error");
  • sprintf(buffer,"unknown error:%d",
  • errnum );
  • return copy_string(buffer);
    }

diff -ru ocaml-3.06/otherlibs/win32unix/select.c G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/select.c
--- ocaml-3.06/otherlibs/win32unix/select.c Tue Apr 30 15:00:48 2002
+++ G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/select.c Thu Dec 12 16:27:04 2002
@@ -57,31 +57,43 @@

Begin_roots3 (readfds, writefds, exceptfds)
Begin_roots3 (read_list, write_list, except_list)

  • fdlist_to_fdset(readfds, &read);
  • fdlist_to_fdset(writefds, &write);
  • fdlist_to_fdset(exceptfds, &except);
  • tm = Double_val(timeout);
  • if (tm < 0.0)
  •  tvp = (struct timeval *) NULL;
    
  • else {
  •  tv.tv_sec = (int) tm;
    
  •  tv.tv_usec = (int) (1e6 * (tm - (int) tm));
    
  •  tvp = &tv;
    
  • }
  • enter_blocking_section();
  • retcode = select(FD_SETSIZE, &read, &write, &except, tvp);
  • leave_blocking_section();
  • if (retcode == -1) {
  •  win32_maperr(WSAGetLastError());
    
  •  uerror("select", Nothing);
    
  • }
  • read_list = fdset_to_fdlist(readfds, &read);
  • write_list = fdset_to_fdlist(writefds, &write);
  • except_list = fdset_to_fdlist(exceptfds, &except);
  • res = alloc_small(3, 0);
  • Field(res, 0) = read_list;
  • Field(res, 1) = write_list;
  • Field(res, 2) = except_list;
  • tm = Double_val(timeout);

  • if ( ! (readfds == Val_int(0) && writefds == Val_int(0) &&

  •   	exceptfds == Val_int(0) ) ) {
    
  •   fdlist_to_fdset(readfds, &read);
    
  •   fdlist_to_fdset(writefds, &write);
    
  •   fdlist_to_fdset(exceptfds, &except);
    
  •   if (tm < 0.0)
    
  •     tvp = (struct timeval *) NULL;
    
  •   else {
    
  •     tv.tv_sec = (int) tm;
    
  •     tv.tv_usec = (int) (1e6 * (tm - (int) tm));
    
  •     tvp = &tv;
    
  •   }
    
  •   enter_blocking_section();
    
  •   retcode = select(FD_SETSIZE, &read, &write, &except, tvp);
    
  •   leave_blocking_section();
    
  •   if (retcode == -1) {
    
  •     win32_maperr(WSAGetLastError());
    
  •     uerror("select", Nothing);
    
  •   }
    
  •   read_list = fdset_to_fdlist(readfds, &read);
    
  •   write_list = fdset_to_fdlist(writefds, &write);
    
  •   except_list = fdset_to_fdlist(exceptfds, &except);
    
  • } else {

  •   if ( tm > 0.0 ) {
    
  •   	enter_blocking_section();
    
  •   	Sleep( (int)tm * 1000 );
    
  •   	leave_blocking_section();
    
  •   }
    
  •   read_list = Val_int(0);
    
  •   write_list = Val_int(0);
    
  •   except_list = Val_int(0);
    
  • }

  • res = alloc_small(3, 0);

  • Field(res, 0) = read_list;

  • Field(res, 1) = write_list;

  • Field(res, 2) = except_list;
    End_roots();
    End_roots();
    return res;
    diff -ru ocaml-3.06/otherlibs/win32unix/unix.ml G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/unix.ml
    --- ocaml-3.06/otherlibs/win32unix/unix.ml Fri Jul 12 09:47:54 2002
    +++ G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/unix.ml Thu Dec 12 11:13:08 2002
    @@ -306,8 +306,13 @@
    external dup : file_descr -> file_descr = "unix_dup"
    external dup2 : file_descr -> file_descr -> unit = "unix_dup2"

+external set_nonblock : file_descr -> unit = "unix_set_nonblock"
+external clear_nonblock : file_descr -> unit = "unix_clear_nonblock"
+(*
let set_nonblock fd = ()
let clear_nonblock fd = ()
+*)

external set_close_on_exec : file_descr -> unit = "win_set_close_on_exec"
external clear_close_on_exec : file_descr -> unit = "win_clear_close_on_exec"
diff -ru ocaml-3.06/otherlibs/win32unix/unixsupport.c G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/unixsupport.c
--- ocaml-3.06/otherlibs/win32unix/unixsupport.c Fri Jun 7 09:49:42 2002
+++ G:\cygwin\home\tema\ocaml\ocaml-3.06/otherlibs/win32unix/unixsupport.c Thu Dec 12 11:13:08 2002
@@ -76,6 +76,8 @@
/* Mapping of Windows error codes to POSIX error codes */

struct error_entry { unsigned long win_code; int range; int posix_code; };
+#define EWOULDBLOCK -WSAEWOULDBLOCK
+#define EINPROGRESS -WSAEINPROGRESS

static struct error_entry win_error_table[] = {
{ ERROR_INVALID_FUNCTION, 0, EINVAL},
@@ -142,6 +144,8 @@
#ifdef WSAENFILE
{ WSAENFILE, 0, ENFILE },
#endif

  • { WSAEWOULDBLOCK, 0, EWOULDBLOCK},
  • { WSAEINPROGRESS, 0, EINPROGRESS},
    { WSAENOTEMPTY, 0, ENOTEMPTY },
    { 0, -1, 0 }
    };
    @@ -164,8 +168,6 @@

/* Windows socket errors */

-#define EWOULDBLOCK -WSAEWOULDBLOCK
-#define EINPROGRESS -WSAEINPROGRESS
#define EALREADY -WSAEALREADY
#define ENOTSOCK -WSAENOTSOCK
#define EDESTADDRREQ -WSAEDESTADDRREQ


@vicuna
Copy link
Author

vicuna commented Jan 6, 2003

Comment author: administrator

  1. otherlibs/win32unix/accept.c - function setsocketopt erase status of
    last accept call, WSAGetLastError must be called before it.

  2. function set_nonblock and clear_nonblock, not implement in current
    version, but I read MSDN, and fount what win32 socket support
    nonblocking mode. I propose include these function in library.

  3. win32 version of select, raise error if call it with empty lists of
    files descr, but linux version handle this situation. I propose emulate
    behaviour of linux version. If select catch empty lists of fd, it call
    win32 function Sleep, for emulate time-out.

These changes are well taken. I'll apply them. Thanks for the patch,

  • Xavier Leroy

@vicuna
Copy link
Author

vicuna commented Jan 6, 2003

Comment author: administrator

Fixed/implemented as suggested 2003-01-06 by XL.

@vicuna vicuna closed this as completed Jan 6, 2003
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant