src/Pure/General/socket_io.ML
author wenzelm
Mon, 01 Mar 2021 22:22:12 +0100
changeset 73340 0ffcad1f6130
parent 69799 18cb541a975f
child 74143 8d20b1cf0d5d
permissions -rw-r--r--
tuned --- fewer warnings;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/socket_io.ML
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
     2
    Author:     Timothy Bourke, NICTA
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
     4
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
     5
Stream IO over TCP sockets.  Following example 10.2 in "The Standard
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
     6
ML Basis Library" by Emden R. Gansner and John H. Reppy.
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
     7
*)
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
     8
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
     9
signature SOCKET_IO =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    10
sig
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    11
  val make_streams: Socket.active INetSock.stream_sock -> BinIO.instream * BinIO.outstream
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    12
  val open_streams: string -> BinIO.instream * BinIO.outstream
69483
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    13
  val with_streams: (BinIO.instream * BinIO.outstream -> 'a) -> string -> 'a
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    14
end;
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    15
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    16
structure Socket_IO: SOCKET_IO =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    17
struct
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    18
69799
18cb541a975f more robust: avoid duplicate Socket.close;
wenzelm
parents: 69483
diff changeset
    19
fun close_permissive socket =
18cb541a975f more robust: avoid duplicate Socket.close;
wenzelm
parents: 69483
diff changeset
    20
  Socket.close socket handle OS.SysErr _ => ();
18cb541a975f more robust: avoid duplicate Socket.close;
wenzelm
parents: 69483
diff changeset
    21
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    22
fun make_streams socket =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    23
  let
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    24
    val (host, port) = INetSock.fromAddr (Socket.Ctl.getSockName socket);
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    25
    val name = NetHostDB.toString host ^ ":" ^ string_of_int port;
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    26
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    27
    val rd =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    28
      BinPrimIO.RD {
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    29
        name = name,
62354
fdd6989cc8a0 SML/NJ is no longer supported;
wenzelm
parents: 54342
diff changeset
    30
        chunkSize = 4096,
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    31
        readVec = SOME (fn n => Socket.recvVec (socket, n)),
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    32
        readArr = SOME (fn buffer => Socket.recvArr (socket, buffer)),
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    33
        readVecNB = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    34
        readArrNB = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    35
        block = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    36
        canInput = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    37
        avail = fn () => NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    38
        getPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    39
        setPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    40
        endPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    41
        verifyPos = NONE,
69799
18cb541a975f more robust: avoid duplicate Socket.close;
wenzelm
parents: 69483
diff changeset
    42
        close = fn () => close_permissive socket,
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    43
        ioDesc = NONE
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    44
      };
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    45
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    46
    val wr =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    47
      BinPrimIO.WR {
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    48
        name = name,
62354
fdd6989cc8a0 SML/NJ is no longer supported;
wenzelm
parents: 54342
diff changeset
    49
        chunkSize = 4096,
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    50
        writeVec = SOME (fn buffer => Socket.sendVec (socket, buffer)),
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    51
        writeArr = SOME (fn buffer => Socket.sendArr (socket, buffer)),
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    52
        writeVecNB = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    53
        writeArrNB = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    54
        block = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    55
        canOutput = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    56
        getPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    57
        setPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    58
        endPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    59
        verifyPos = NONE,
69799
18cb541a975f more robust: avoid duplicate Socket.close;
wenzelm
parents: 69483
diff changeset
    60
        close = fn () => close_permissive socket,
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    61
        ioDesc = NONE
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    62
      };
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    63
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    64
    val in_stream =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    65
      BinIO.mkInstream
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    66
        (BinIO.StreamIO.mkInstream (rd, Word8Vector.fromList []));
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    67
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    68
    val out_stream =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    69
      BinIO.mkOutstream
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    70
        (BinIO.StreamIO.mkOutstream (wr, IO.BLOCK_BUF));
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    71
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    72
  in (in_stream, out_stream) end;
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    73
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    74
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    75
fun open_streams socket_name =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    76
  let
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    77
    fun err () = error ("Bad socket name: " ^ quote socket_name);
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    78
    val (host, port) =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    79
      (case space_explode ":" socket_name of
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    80
        [h, p] =>
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    81
         (case NetHostDB.getByName h of SOME host => host | NONE => err (),
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    82
          case Int.fromString p of SOME port => port | NONE => err ())
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    83
      | _ => err ());
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    84
    val socket: Socket.active INetSock.stream_sock = INetSock.TCP.socket ();
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    85
    val _ = Socket.connect (socket, INetSock.toAddr (NetHostDB.addr host, port));
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    86
  in make_streams socket end;
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    87
69483
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    88
fun with_streams f =
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    89
  Thread_Attributes.uninterruptible (fn restore_attributes => fn socket_name =>
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    90
    let
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    91
      val streams = open_streams socket_name;
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    92
      val result = Exn.capture (restore_attributes f) streams;
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    93
    in BinIO.closeIn (#1 streams); BinIO.closeOut (#2 streams); Exn.release result end);
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    94
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    95
end;