私は現在、ゲーム構築の開始段階にあり、ゲームのサーバー側としてプロローグを使用して、ボードでのプレイの検証チェックを行っています。
現在2つの問題を抱えています。
1: 中止せずにサーバーを閉じることができないように見えるため、ソケットを開いたままにします。コードは次のとおりです
サーバ:
:- use_module(library(socket)).
create_server(Port) :-
tcp_socket(Socket),
tcp_bind(Socket, Port),
tcp_listen(Socket, 5),
tcp_open_socket(Socket, AcceptFd, _),
dispatch(AcceptFd).
dispatch(AcceptFd) :-
tcp_accept(AcceptFd, Socket, Peer),
thread_create(process_client(Socket, Peer), _,
[ detached(true)
]),
dispatch(AcceptFd).
process_client(Socket, _Peer) :-
setup_call_cleanup(tcp_open_socket(Socket, In, Out),
handle_service(In, Out),
close_connection(In, Out)).
close_connection(In, Out) :-
close(In, [force(true)]),
close(Out, [force(true)]).
handle_service(In, Out) :-
read(In, Int),
writeln(Int),
( Int == end_of_file
-> true
;
call_test(Int,Term),
format(Out, 'seen(~q).~n', [Term]),
flush_output(Out),
handle_service(In, Out)
).
call_test(test,Term):-Term = 'really test'.
call_test(validate(teste),Term):-
String = "validate(test)",
string_to_list(String,List),
read_from_chars(List,Stringf),
writeln(Stringf),
Term = 'foo'.
クライアント:
:- use_module(library(streampool)).
create_client(Host, Port) :-
setup_call_catcher_cleanup(tcp_socket(Socket),
tcp_connect(Socket, Host:Port),
exception(_),
tcp_close_socket(Socket)),
setup_call_cleanup(tcp_open_socket(Socket, In, Out),
chat_to_server(In, Out),
close_connection(In, Out)).
chat_to_server(In, Out) :-
read(Term),
( Term == end_of_file
-> true
; format(Out, '~q .~n', [Term]),
flush_output(Out),
read(In, Reply),
write(Reply),
%format('Reply: ~q.~n', [Reply]),
chat_to_server(In, Out)
).
close_connection(In, Out) :-
close(In, [force(true)]),
close(Out, [force(true)]).
ctrl+D (end_of_file) を使用して問題なくクライアントを閉じることができますが、サーバーが閉じません... end_of_file を受け取り、end_of_file を出力しますが、閉じません。サーバーに直接挿入する場合でも。私は何を間違っていますか?
2: 使用する述語の名前と引数を含む文字列を C++ から Swipl に渡す必要があります。誰かがこれを行う方法を教えてください。または、少なくとも正しい方向に向けてください。
どうもありがとうございました。