Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/system/Basis/Implementation/Sockets/pre-sock.sml
ViewVC logotype

Annotation of /sml/trunk/src/system/Basis/Implementation/Sockets/pre-sock.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1394 - (view) (download)

1 : mblume 1393 (* pre-soc.ksml
2 : monnier 416 *
3 :     * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
4 :     *
5 :     * These are some common type definitions used in the sockets library. This
6 :     * structure is called Sock, so that the signatures will compile.
7 :     *
8 :     *)
9 :     local
10 :     structure SysWord = SysWordImp
11 :     structure Word8 = Word8Imp
12 :     structure Word = WordImp
13 :     in
14 : mblume 1393 structure Socket = struct
15 : monnier 416
16 :     (* the raw representation address data *)
17 :     type addr = Word8Vector.vector
18 :    
19 :     (* the raw representation of an address family *)
20 :     type af = CInterface.system_const
21 :    
22 : mblume 1389 (* the raw representation of a socket:
23 :     * a file descriptor for now and a mutable flag indicating
24 :     * (with a value of true) if the socket is currently set
25 :     * to non-blocking *)
26 :     type sockFD = int
27 :     type socket = { fd: sockFD, nb: bool ref }
28 : monnier 416
29 :     (* sockets are polymorphic; the instantiation of the type variables
30 :     * provides a way to distinguish between different kinds of sockets.
31 :     *)
32 :     datatype ('sock, 'af) sock = SOCK of socket
33 :     datatype 'af sock_addr = ADDR of addr
34 :    
35 : mblume 1393 (* witness types for the socket parameter *)
36 :     datatype dgram = DGRAM
37 :     datatype 'a stream = STREAM
38 :     datatype passive = PASSIVE
39 :     datatype active = ACTIVE
40 :    
41 :     structure AF = struct
42 :     datatype addr_family = AF of af
43 :     end
44 :    
45 :     structure SOCK = struct
46 :     (* socket types *)
47 :     datatype sock_type = SOCKTY of CInterface.system_const
48 :     end
49 :    
50 :     datatype shutdown_mode = NO_RECVS | NO_SENDS | NO_RECVS_OR_SENDS
51 :     type sock_desc = OS.IO.iodesc
52 :    
53 :     (* Sock I/O option types *)
54 :     type out_flags = {don't_route : bool, oob : bool}
55 :     type in_flags = {peek : bool, oob : bool}
56 :    
57 : monnier 416 (** Utility functions for parsing/unparsing network addresses **)
58 :     local
59 :     structure SysW = SysWord
60 :     structure SCvt = StringCvt
61 :     fun toW (getc, strm) = let
62 :     fun scan radix strm = (case (SysW.scan radix getc strm)
63 :     of NONE => NONE
64 :     | (SOME(w, strm)) => SOME(w, strm)
65 :     (* end case *))
66 :     in
67 :     case (getc strm)
68 :     of NONE => NONE
69 :     | (SOME(#"0", strm')) => (case (getc strm')
70 :     of NONE => SOME(0w0, strm')
71 :     | (SOME((#"x" | #"X"), strm'')) => scan SCvt.HEX strm''
72 :     | _ => scan SCvt.OCT strm
73 :     (* end case *))
74 :     | _ => scan SCvt.DEC strm
75 :     (* end case *)
76 :     end
77 :     (* check that the word is representable in the given number of bits; raise
78 :     * Overflow if not.
79 :     *)
80 :     fun chk (w, bits) =
81 :     if (SysW.>= (SysW.>>(0wxffffffff, Word.-(0w32, bits)), w))
82 :     then w
83 :     else raise General.Overflow
84 :     (* Scan a sequence of numbers separated by #"." *)
85 :     fun scan getc strm = (case toW (getc, strm)
86 :     of NONE => NONE
87 :     | SOME(w, strm') => scanRest getc ([w], strm')
88 :     (* end case *))
89 :     and scanRest getc (l, strm) = (case getc strm
90 :     of SOME(#".", strm') => (case toW (getc, strm')
91 :     of NONE => SOME(List.rev l, strm)
92 :     | SOME(w, strm'') => scanRest getc (w::l, strm'')
93 :     (* end case *))
94 :     | _ => SOME(List.rev l, strm)
95 :     (* end case *))
96 :     in
97 :     fun toWords getc strm = (case (scan getc strm)
98 :     of SOME([a, b, c, d], strm) =>
99 :     SOME([chk(a, 0w8), chk(b, 0w8), chk(c, 0w8), chk(d, 0w8)], strm)
100 :     | SOME([a, b, c], strm) =>
101 :     SOME([chk(a, 0w8), chk(b, 0w8), chk(c, 0w16)], strm)
102 :     | SOME([a, b], strm) =>
103 :     SOME([chk(a, 0w8), chk(b, 0w24)], strm)
104 :     | SOME([a], strm) =>
105 :     SOME([chk(a, 0w32)], strm)
106 :     | _ => NONE
107 :     (* end case *))
108 :     fun fromBytes (a, b, c, d) = let
109 :     val fmt = Word8.fmt StringCvt.DEC
110 :     in
111 :     concat [fmt a, ".", fmt b, ".", fmt c, ".", fmt d]
112 :     end
113 :     end
114 :    
115 :     end (* PreSock *)
116 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0