如何使用Socket.select
How to use Socket.select
如何在标准 ML 中使用 Socket.select
?
根据 docs,我应该将三个套接字列表和一个超时 option
传递给它,并在任何套接字准备就绪时传递函数 returns做某事(我假设,但不确定输出列表中的套接字 仅 需要注意的套接字)。然而,
- 输入似乎既不是元组,也不是四个参数。我该如何构建合适的输入结构?
select
获取 returns 的 sock_desc
列表,似乎没有办法从其 [=14] 中获取 socket
=].似乎也没有一种构建有效地图的方法,因为似乎不可能订购两个 sock_desc
s,只能比较它们是否相等。一旦我从 select
获得了 return 值,我该如何使用 returned 套接字做任何有用的事情,比如写出响应,或者在它们上调用 accept
?
- 输入参数是一个包含四个字段的记录,因此您的代码应如下所示:
Socket.select {
rds = readSocketDescs,
wrs = writeSocketDescs,
exs = exnSocketDescs,
timeout = SOME (Time.fromSeconds 10)
}
- 是的,不确定,可能您需要自己使用列表来保留映射。效率不高,但我看不出你还能做什么。
(**
* Produces a list of all the pairs in `listPair`, whose keys are present
* in `whiteList`. Example:
*
* ```sml
* - filterListPair op= [(1,"a"), (2,"b"), (3,"c")] [2,3];
* val it = [(2,"b"),(3,"c")] : (int * string) list
* ```
*)
fun filterListPair eq listPair whiteList =
let
fun recur listPair whiteList result =
case (listPair, whiteList) of
([], _) => result
| (_, []) => result
| ((x, y) :: xs, k :: ks) =>
if eq (x, k)
then recur xs ks ((x, y) :: result)
else recur xs whiteList result
in
List.rev (recur listPair whiteList [])
end
val sockets = [ (* what have you *) ]
val descsToSockets = List.map (fn s => (Socket.sockDesc s, s)) sockets
val { rds, wrs, exs } = Socket.select {
rds = readSocketDescs,
wrs = writeSocketDescs,
exs = exnSocketDescs,
timeout = SOME (Time.fromSeconds 10)
}
(*
* The contract of Socket.select ensures that the order in input lists
* is preserved in the output lists, so we can use `filterListPair`.
*)
val selectedReadSockets =
filterListPair Socket.sameDesc descsToSockets rds
如何在标准 ML 中使用 Socket.select
?
根据 docs,我应该将三个套接字列表和一个超时 option
传递给它,并在任何套接字准备就绪时传递函数 returns做某事(我假设,但不确定输出列表中的套接字 仅 需要注意的套接字)。然而,
- 输入似乎既不是元组,也不是四个参数。我该如何构建合适的输入结构?
select
获取 returns 的sock_desc
列表,似乎没有办法从其 [=14] 中获取socket
=].似乎也没有一种构建有效地图的方法,因为似乎不可能订购两个sock_desc
s,只能比较它们是否相等。一旦我从select
获得了 return 值,我该如何使用 returned 套接字做任何有用的事情,比如写出响应,或者在它们上调用accept
?
- 输入参数是一个包含四个字段的记录,因此您的代码应如下所示:
Socket.select {
rds = readSocketDescs,
wrs = writeSocketDescs,
exs = exnSocketDescs,
timeout = SOME (Time.fromSeconds 10)
}
- 是的,不确定,可能您需要自己使用列表来保留映射。效率不高,但我看不出你还能做什么。
(**
* Produces a list of all the pairs in `listPair`, whose keys are present
* in `whiteList`. Example:
*
* ```sml
* - filterListPair op= [(1,"a"), (2,"b"), (3,"c")] [2,3];
* val it = [(2,"b"),(3,"c")] : (int * string) list
* ```
*)
fun filterListPair eq listPair whiteList =
let
fun recur listPair whiteList result =
case (listPair, whiteList) of
([], _) => result
| (_, []) => result
| ((x, y) :: xs, k :: ks) =>
if eq (x, k)
then recur xs ks ((x, y) :: result)
else recur xs whiteList result
in
List.rev (recur listPair whiteList [])
end
val sockets = [ (* what have you *) ]
val descsToSockets = List.map (fn s => (Socket.sockDesc s, s)) sockets
val { rds, wrs, exs } = Socket.select {
rds = readSocketDescs,
wrs = writeSocketDescs,
exs = exnSocketDescs,
timeout = SOME (Time.fromSeconds 10)
}
(*
* The contract of Socket.select ensures that the order in input lists
* is preserved in the output lists, so we can use `filterListPair`.
*)
val selectedReadSockets =
filterListPair Socket.sameDesc descsToSockets rds