src/Pure/General/socket_io.ML
author wenzelm
Fri, 24 Nov 2023 11:31:15 +0100
changeset 79047 13afea5203f1
parent 79046 926fc9ca7360
child 79049 10b6add456d0
permissions -rw-r--r--
clarified signature: more general make_streams;
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 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
    12
  val with_streams: (BinIO.instream * BinIO.outstream -> 'a) -> string -> 'a
74143
8d20b1cf0d5d clarified signature;
wenzelm
parents: 69799
diff changeset
    13
  val with_streams': (BinIO.instream * BinIO.outstream -> 'a) -> string -> 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
79047
13afea5203f1 clarified signature: more general make_streams;
wenzelm
parents: 79046
diff changeset
    22
fun make_streams socket_name socket =
50800
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 rd =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    25
      BinPrimIO.RD {
79046
wenzelm
parents: 78718
diff changeset
    26
        name = socket_name,
62354
fdd6989cc8a0 SML/NJ is no longer supported;
wenzelm
parents: 54342
diff changeset
    27
        chunkSize = 4096,
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    28
        readVec = SOME (fn n => Socket.recvVec (socket, n)),
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    29
        readArr = SOME (fn buffer => Socket.recvArr (socket, buffer)),
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    30
        readVecNB = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    31
        readArrNB = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    32
        block = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    33
        canInput = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    34
        avail = fn () => NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    35
        getPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    36
        setPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    37
        endPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    38
        verifyPos = NONE,
69799
18cb541a975f more robust: avoid duplicate Socket.close;
wenzelm
parents: 69483
diff changeset
    39
        close = fn () => close_permissive socket,
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    40
        ioDesc = NONE
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    41
      };
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    42
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    43
    val wr =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    44
      BinPrimIO.WR {
79046
wenzelm
parents: 78718
diff changeset
    45
        name = socket_name,
62354
fdd6989cc8a0 SML/NJ is no longer supported;
wenzelm
parents: 54342
diff changeset
    46
        chunkSize = 4096,
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    47
        writeVec = SOME (fn buffer => Socket.sendVec (socket, buffer)),
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    48
        writeArr = SOME (fn buffer => Socket.sendArr (socket, buffer)),
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    49
        writeVecNB = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    50
        writeArrNB = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    51
        block = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    52
        canOutput = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    53
        getPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    54
        setPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    55
        endPos = NONE,
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    56
        verifyPos = NONE,
69799
18cb541a975f more robust: avoid duplicate Socket.close;
wenzelm
parents: 69483
diff changeset
    57
        close = fn () => close_permissive socket,
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    58
        ioDesc = NONE
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    59
      };
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    60
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    61
    val in_stream =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    62
      BinIO.mkInstream
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    63
        (BinIO.StreamIO.mkInstream (rd, Word8Vector.fromList []));
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    64
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    65
    val out_stream =
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    66
      BinIO.mkOutstream
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    67
        (BinIO.StreamIO.mkOutstream (wr, IO.BLOCK_BUF));
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    68
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    69
  in (in_stream, out_stream) end;
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    70
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    71
79046
wenzelm
parents: 78718
diff changeset
    72
fun open_streams name =
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    73
  let
79046
wenzelm
parents: 78718
diff changeset
    74
    fun err () = error ("Bad socket name: " ^ quote name);
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    75
    val (host, port) =
79046
wenzelm
parents: 78718
diff changeset
    76
      (case space_explode ":" name of
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    77
        [h, p] =>
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    78
         (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
    79
          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
    80
      | _ => err ());
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    81
    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
    82
    val _ = Socket.connect (socket, INetSock.toAddr (NetHostDB.addr host, port));
79047
13afea5203f1 clarified signature: more general make_streams;
wenzelm
parents: 79046
diff changeset
    83
13afea5203f1 clarified signature: more general make_streams;
wenzelm
parents: 79046
diff changeset
    84
    val (socket_host, socket_port) = INetSock.fromAddr (Socket.Ctl.getSockName socket);
13afea5203f1 clarified signature: more general make_streams;
wenzelm
parents: 79046
diff changeset
    85
    val socket_name = NetHostDB.toString socket_host ^ ":" ^ string_of_int socket_port;
13afea5203f1 clarified signature: more general make_streams;
wenzelm
parents: 79046
diff changeset
    86
  in make_streams socket_name socket end
79046
wenzelm
parents: 78718
diff changeset
    87
  handle OS.SysErr (msg, _) => error (msg ^ ": failed to open socket " ^ name);
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
    88
69483
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    89
fun with_streams f =
79046
wenzelm
parents: 78718
diff changeset
    90
  Thread_Attributes.uninterruptible (fn run => fn name =>
69483
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    91
    let
79046
wenzelm
parents: 78718
diff changeset
    92
      val streams = open_streams name;
78716
97dfba4405e3 tuned signature;
wenzelm
parents: 75577
diff changeset
    93
      val result = Exn.capture (run f) streams;
78718
wenzelm
parents: 78716
diff changeset
    94
      val _ = BinIO.closeIn (#1 streams);
wenzelm
parents: 78716
diff changeset
    95
      val _ = BinIO.closeOut (#2 streams);
wenzelm
parents: 78716
diff changeset
    96
    in Exn.release result end);
69483
023d92df3d84 more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents: 69124
diff changeset
    97
79046
wenzelm
parents: 78718
diff changeset
    98
fun with_streams' f name password =
74143
8d20b1cf0d5d clarified signature;
wenzelm
parents: 69799
diff changeset
    99
  with_streams (fn streams =>
79046
wenzelm
parents: 78718
diff changeset
   100
    (Byte_Message.write_line (#2 streams) (Bytes.string password); f streams)) name;
74143
8d20b1cf0d5d clarified signature;
wenzelm
parents: 69799
diff changeset
   101
50800
c0fb2839d1a9 recovered buffered sockets from 11f622794ad6 -- requires Poly/ML 5.5.x;
wenzelm
parents:
diff changeset
   102
end;