2001-04-09 20:54:03 +01:00
|
|
|
|
/*************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* YAP Prolog *
|
|
|
|
|
* *
|
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
|
* *
|
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
|
* *
|
|
|
|
|
**************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* File: sockets.yap *
|
|
|
|
|
* Last rev: *
|
|
|
|
|
* mods: *
|
|
|
|
|
* comments: Socket predicates *
|
|
|
|
|
* *
|
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
|
|
socket(S,D) :-
|
|
|
|
|
socket(S, 'SOCK_STREAM', 0, D).
|
|
|
|
|
|
|
|
|
|
socket_accept(S,F) :-
|
|
|
|
|
socket_accept(S, _, F).
|
|
|
|
|
|
|
|
|
|
socket_select(Socks, OutSocks, TimeOut, Streams, OutStreams) :-
|
2002-01-07 06:28:04 +00:00
|
|
|
|
'$check_list_for_sockets'(Socks, socket_select(Socks, OutSocks, TimeOut, Streams, OutStreams)),
|
|
|
|
|
'$check_list_for_sockets'(Streams, socket_select(Socks, OutSocks, TimeOut, Streams, OutStreams)),
|
2001-09-03 17:05:59 +01:00
|
|
|
|
'$select_cp_fds'(Socks, Streams, Fds),
|
2001-04-09 20:54:03 +01:00
|
|
|
|
'$check_select_time'(TimeOut, Sec, USec, socket_select(Socks, OutSocks, TimeOut, Streams, OutStreams)),
|
|
|
|
|
'$socket_select'(Fds, Sec, USec, NFds),
|
|
|
|
|
'$cp_socket_fds'(Socks, NFds, OutSocks, NFdsS),
|
|
|
|
|
'$cp_stream_fds'(Streams, NFdsS, OutStreams).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* check whether a list of options is valid */
|
2002-01-07 06:28:04 +00:00
|
|
|
|
'$check_list_for_sockets'(V,G) :- var(V), !,
|
2001-04-09 20:54:03 +01:00
|
|
|
|
throw(error(instantiation_error,G)).
|
2002-01-07 06:28:04 +00:00
|
|
|
|
'$check_list_for_sockets'([],_) :- !.
|
|
|
|
|
'$check_list_for_sockets'([_|T],G) :- !,
|
|
|
|
|
<EFBFBD> '$check_list_for_sockets'(T,G).
|
|
|
|
|
'$check_list_for_sockets'(T,G) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
|
throw(error(type_error(list,T),G)).
|
|
|
|
|
|
|
|
|
|
'$select_cp_fds'([], Fds, Fds).
|
2001-09-03 17:05:59 +01:00
|
|
|
|
'$select_cp_fds'([_-Fd|L], Fds0, [Fd|Fds]) :-
|
|
|
|
|
'$select_cp_fds'(L, Fds0, Fds).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
2001-10-30 16:42:05 +00:00
|
|
|
|
'$check_select_time'(V, _, _, Goal) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
|
var(V), !,
|
2001-10-30 16:42:05 +00:00
|
|
|
|
throw(error(instantiation_error,Goal)).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
'$check_select_time'(off, -1, -1, _).
|
2001-10-30 16:42:05 +00:00
|
|
|
|
'$check_select_time'(Sec0:USec0, Sec, USec, _) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
|
Sec is Sec0,
|
2001-10-30 16:42:05 +00:00
|
|
|
|
USec is USec0,
|
2001-04-09 20:54:03 +01:00
|
|
|
|
Sec > 0, USec > 0.
|
|
|
|
|
|
|
|
|
|
'$cp_socket_fds'([], Fds, [], Fds).
|
2001-09-03 17:05:59 +01:00
|
|
|
|
'$cp_socket_fds'([_|Scks], [[]|Fds], Out, StrFds) :- !,
|
|
|
|
|
'$cp_socket_fds'(Scks, Fds, Out, StrFds).
|
|
|
|
|
'$cp_socket_fds'([T-Socket|Scks], [Socket|Fds], [T-connection(Client,Stream)|Out], StrFds) :-
|
|
|
|
|
socket_accept(Socket, Client, Stream),
|
2001-04-09 20:54:03 +01:00
|
|
|
|
'$cp_socket_fds'(Scks, Fds, Out, StrFds).
|
|
|
|
|
|
2001-10-30 16:42:05 +00:00
|
|
|
|
'$cp_stream_fds'([], _, []).
|
2001-09-03 17:05:59 +01:00
|
|
|
|
'$cp_stream_fds'([_|Strs], [[]|Fds], Out) :- !,
|
|
|
|
|
'$cp_stream_fds'(Strs, Fds, Out).
|
|
|
|
|
'$cp_stream_fds'([Stream|Strs], [Stream|Fds], [Stream|Out]) :-
|
2001-04-09 20:54:03 +01:00
|
|
|
|
'$cp_stream_fds'(Strs, Fds, Out).
|
2001-09-03 17:05:59 +01:00
|
|
|
|
|
|
|
|
|
socket_buffering(Sock, Flag, InSize, OutSize) :-
|
|
|
|
|
var(OutSize), OutSize \= InSize, !,
|
|
|
|
|
throw(error(instantiation_error,socket_buffering(Sock, Flag, InSize, OutSize))).
|
|
|
|
|
socket_buffering(Sock, Flag, InSize, OutSize) :-
|
2002-01-03 17:15:50 +00:00
|
|
|
|
'$convert_sock_buff'(OutSize, OutNumb),
|
2001-09-03 17:05:59 +01:00
|
|
|
|
'$socket_buffering'(Sock, Flag, InNumb, OutNumb),
|
2002-01-03 17:15:50 +00:00
|
|
|
|
'$convert_sock_buff'(InSize, InNumb).
|
2001-09-03 17:05:59 +01:00
|
|
|
|
|
2002-01-03 17:15:50 +00:00
|
|
|
|
'$convert_sock_buff'(V, V) :- var(V), !.
|
2001-09-03 17:05:59 +01:00
|
|
|
|
'$convert_sock_buff'(unbuf, 1) :- !.
|
2001-10-30 16:42:05 +00:00
|
|
|
|
'$convert_sock_buff'(fullbuf, _).
|
2001-09-03 17:05:59 +01:00
|
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|