File: inet-sock.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (68 lines) | stat: -rw-r--r-- 2,197 bytes parent folder | download | duplicates (3)
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
64
65
66
67
68
(* Copyright (C) 2002-2008 Henry Cejtin, Matthew Fluet, Suresh
 *    Jagannathan, and Stephen Weeks.
 *
 * MLton is released under a HPND-style license.
 * See the file MLton-LICENSE for details.
 *)

structure INetSock:> INET_SOCK =
   struct
      structure Prim = PrimitiveFFI.Socket.INetSock

      datatype inet = INET (* a phantom type*)
      type 'sock_type sock = (inet, 'sock_type) Socket.sock
      type 'mode stream_sock = 'mode Socket.stream sock
      type dgram_sock = Socket.dgram sock
      type sock_addr = inet Socket.sock_addr

      val inetAF = Net.AddrFamily.fromRep PrimitiveFFI.Socket.AF.INET

      fun toAddr (in_addr, port) =
         let
            val port = Word16.fromInt port
                       handle Overflow => PosixError.raiseSys PosixError.inval
            val port = Net.Word16.hton port
            val (sa, salen, finish) = Socket.newSockAddr ()
            val _ = Prim.toAddr (NetHostDB.inAddrToWord8Vector in_addr,
                                 port, sa, salen)

         in
            finish ()
         end

      fun any port = toAddr (NetHostDB.any (), port)

      fun fromAddr sa =
        let
          val () = Prim.fromAddr (Socket.unpackSockAddr sa)
          val port = Prim.getPort ()
          val port = Net.Word16.ntoh port
          val port = Word16.toInt port
          val (ia, finish) = NetHostDB.newInAddr ()
          val _ = Prim.getInAddr (NetHostDB.preInAddrToWord8Array ia)
        in
          (finish (), port)
        end

      structure UDP =
         struct
            fun socket' prot = GenericSock.socket' (inetAF, Socket.SOCK.dgram, prot)
            fun socket () = socket' 0
         end

      structure TCP =
         struct
            structure Prim = Prim.Ctl

            fun socket' prot = GenericSock.socket' (inetAF, Socket.SOCK.stream, prot)
            fun socket () = socket' 0

            fun getNODELAY sock =
               Socket.CtlExtra.getSockOptBool
               (Prim.IPPROTO_TCP, Prim.TCP_NODELAY) sock

            fun setNODELAY (sock, optval) =
               Socket.CtlExtra.setSockOptBool
               (Prim.IPPROTO_TCP, Prim.TCP_NODELAY) (sock,optval)
         end
   end