2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5 Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
7 Stockfish is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "syzygy/tbprobe.h"
32 #include <grpc/grpc.h>
33 #include <grpc++/server.h>
34 #include <grpc++/server_builder.h>
35 #include "hashprobe.grpc.pb.h"
38 using grpc::ServerBuilder;
39 using grpc::ServerContext;
41 using grpc::StatusCode;
42 using namespace hashprobe;
44 class HashProbeImpl final : public HashProbe::Service {
46 Status Probe(ServerContext* context,
47 const HashProbeRequest* request,
48 HashProbeResponse *response) {
49 Position pos(request->fen(), /*isChess960=*/false, Threads.main());
50 if (!pos.pos_is_ok()) {
51 return Status(StatusCode::INVALID_ARGUMENT, "Invalid FEN");
54 bool invert = (pos.side_to_move() == BLACK);
56 HashProbeMove *root_move = response->add_move();
57 root_move->set_move(MOVE_NONE);
58 ProbeMove(pos.key(), invert, root_move);
60 MoveList<LEGAL> moves(pos);
61 for (const ExtMove* em = moves.begin(); em != moves.end(); ++em) {
62 HashProbeMove *move = response->add_move();
63 move->set_move(em->move);
64 ProbeMove(pos.key_after(em->move), !invert, move);
70 void ProbeMove(const int64_t key, bool invert, HashProbeMove* response) {
72 TTEntry *entry = TT.probe(key, found);
73 response->set_found(found);
75 Value value = entry->value();
76 Value eval = entry->eval();
77 Bound bound = entry->bound();
82 if (bound == BOUND_UPPER) {
84 } else if (bound == BOUND_LOWER) {
89 response->set_pv_move(entry->move());
90 response->set_depth(entry->depth());
91 response->set_eval(eval);
92 response->set_value(value);
93 response->set_bound(HashProbeMove::ValueBound(bound));
100 std::string server_address("0.0.0.0:50051");
101 HashProbeImpl service;
103 ServerBuilder builder;
104 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
105 builder.RegisterService(&service);
106 std::unique_ptr<Server> server(builder.BuildAndStart());
107 std::cout << "Server listening on " << server_address << std::endl;
115 int main(int argc, char* argv[]) {
117 std::cout << engine_info() << std::endl;
126 Threads.set(Options["Threads"]);
127 Search::clear(); // After threads are up
129 std::thread(&rpc_thread).detach();
131 UCI::loop(argc, argv);