File: INetSock.sml

package info (click to toggle)
polyml 5.2.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 19,692 kB
  • ctags: 17,567
  • sloc: cpp: 37,221; sh: 9,591; asm: 4,120; ansic: 428; makefile: 203; ml: 191; awk: 91; sed: 10
file content (113 lines) | stat: -rw-r--r-- 3,338 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(*
    Title:      Standard Basis Library: Internet Sockets
    Author:     David Matthews
    Copyright   David Matthews 2000

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*)

(* G&R 2004 status: Checked, no change. *)

signature INET_SOCK =
sig
	type inet

	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 : Socket.AF.addr_family
	val toAddr : NetHostDB.in_addr * int -> sock_addr
	val fromAddr : sock_addr -> NetHostDB.in_addr * int
	val any : int -> sock_addr
	structure UDP :
	sig
		val socket : unit -> dgram_sock
		val socket' : int -> dgram_sock
	end
	structure TCP :
	sig
		val socket : unit -> 'mode stream_sock
		val socket' : int -> 'mode stream_sock
		val getNODELAY : 'mode stream_sock -> bool
		val setNODELAY : 'mode stream_sock * bool -> unit
	end
end;

structure INetSock :> INET_SOCK =
struct
	abstype inet = ABSTRACT with end;

	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 : Socket.AF.addr_family =
		case Socket.AF.fromString "INET" of
			NONE => raise OS.SysErr("Missing address family", NONE)
		|	SOME s => s

	local
		val doCall = RunCall.run_call2 RuntimeCalls.POLY_SYS_network
	in
		fun toAddr(iaddr: NetHostDB.in_addr, port: int) : sock_addr =
			doCall(40, (port, iaddr))
	end

	local
		val doCall1 = RunCall.run_call2 RuntimeCalls.POLY_SYS_network
		and doCall2 = RunCall.run_call2 RuntimeCalls.POLY_SYS_network
	in
		fun fromAddr (s: sock_addr) : NetHostDB.in_addr * int =
			if Socket.familyOfAddr s <> inetAF
			then raise Match
			else (doCall1(42, s), doCall2(41, s))
	end

	local
		val doCall = RunCall.run_call2 RuntimeCalls.POLY_SYS_network
		val iAddrAny: NetHostDB.in_addr = doCall(13, ())
	in
	fun any (p: int) : sock_addr = toAddr(iAddrAny, p)
	end
	
	local
		val doCall1 = RunCall.run_call2 RuntimeCalls.POLY_SYS_network
		val doCall2 = RunCall.run_call2 RuntimeCalls.POLY_SYS_network
	in
		structure UDP =
		struct
			fun socket () = GenericSock.socket(inetAF, Socket.SOCK.dgram)
			fun socket' p = GenericSock.socket'(inetAF, Socket.SOCK.dgram, p)
		end

		structure TCP =
		struct
			fun socket () = GenericSock.socket(inetAF, Socket.SOCK.stream)
			fun socket' p = GenericSock.socket'(inetAF, Socket.SOCK.stream, p)

			fun getNODELAY(s: 'mode stream_sock): bool =
				doCall1(16, RunCall.unsafeCast s)

			fun setNODELAY (s: 'mode stream_sock, b: bool): unit =
				doCall2(15, (RunCall.unsafeCast  s, b))
		end
	end	

end;