File: initWinSock.c

package info (click to toggle)
hugs98 98.200311-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 12,964 kB
  • ctags: 8,084
  • sloc: ansic: 67,521; haskell: 61,497; xml: 4,566; sh: 3,264; cpp: 1,936; yacc: 1,094; makefile: 915; cs: 883; sed: 10
file content (63 lines) | stat: -rw-r--r-- 1,167 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "HsNet.h"
#include "HsFFI.h"

#if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__)

static int winsock_inited = 0;
static int winsock_uninited = 0;

/* Initialising WinSock... */
HsInt
initWinSock ()
{
  WORD wVersionRequested;
  WSADATA wsaData;  
  int err;
#ifdef __HUGS__
  int optval = SO_SYNCHRONOUS_NONALERT;
#endif

  if (!winsock_inited) {
    wVersionRequested = MAKEWORD( 1, 1 );

    err = WSAStartup ( wVersionRequested, &wsaData );
    
    if ( err != 0 ) {
       return err;
    }

    if ( LOBYTE( wsaData.wVersion ) != 1 ||
       HIBYTE( wsaData.wVersion ) != 1 ) {
      WSACleanup();
      return (-1);
    }
#ifdef __HUGS__
    /* By default, socket() creates sockets in overlapped mode
     * (so that async I/O is possible). The CRT can only handle
     * non-overlapped sockets, so turn off overlap mode here.
     */
    setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
	       &optval, sizeof(optval));
#endif

    winsock_inited = 1;
  }
  return 0;
}

static void
shutdownHandler(void)
{
  WSACleanup();
}

void
shutdownWinSock()
{
    if (!winsock_uninited) {
	atexit(shutdownHandler);
	winsock_uninited = 1;
    }
}

#endif