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);
55 Search::StateStackPtr setup_states = Search::StateStackPtr(new std::stack<StateInfo>);
57 HashProbeLine *root_line = response->add_line();
58 ProbeMove(&pos, setup_states.get(), invert, root_line);
60 MoveList<LEGAL> moves(pos);
61 for (const ExtMove* em = moves.begin(); em != moves.end(); ++em) {
62 HashProbeLine *line = response->add_line();
63 FillMove(em->move, line->mutable_move());
64 setup_states->push(StateInfo());
65 pos.do_move(em->move, setup_states->top(), pos.gives_check(em->move, CheckInfo(pos)));
66 ProbeMove(&pos, setup_states.get(), !invert, line);
67 pos.undo_move(em->move);
73 void FillMove(Move move, HashProbeMove* decoded) {
74 if (!is_ok(move)) return;
76 Square from = from_sq(move);
77 Square to = to_sq(move);
79 if (type_of(move) == CASTLING) {
80 to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
83 decoded->set_from_sq(UCI::square(from));
84 decoded->set_to_sq(UCI::square(to));
86 if (type_of(move) == PROMOTION) {
87 decoded->set_promotion(std::string() + " PNBRQK"[promotion_type(move)]);
91 void ProbeMove(Position* pos, std::stack<StateInfo>* setup_states, bool invert, HashProbeLine* response) {
93 TTEntry *entry = TT.probe(pos->key(), found);
94 response->set_found(found);
96 Value value = entry->value();
97 Value eval = entry->eval();
98 Bound bound = entry->bound();
103 if (bound == BOUND_UPPER) {
105 } else if (bound == BOUND_LOWER) {
110 response->set_depth(entry->depth());
111 response->set_eval(eval);
112 response->set_value(value);
113 response->set_bound(HashProbeLine::ValueBound(bound));
115 // Follow the PV until we hit an illegal move.
118 while (found && is_ok(entry->move())) {
119 FillMove(entry->move(), response->add_pv());
120 if (seen.count(pos->key())) break;
121 pv.push(entry->move());
122 seen.insert(pos->key());
123 setup_states->push(StateInfo());
124 pos->do_move(entry->move(), setup_states->top(), pos->gives_check(entry->move(), CheckInfo(*pos)));
125 entry = TT.probe(pos->key(), found);
128 // Unroll the PV back again, so the Position object remains unchanged.
129 while (!pv.empty()) {
130 pos->undo_move(pv.top());
139 std::string server_address("0.0.0.0:50051");
140 HashProbeImpl service;
142 ServerBuilder builder;
143 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
144 builder.RegisterService(&service);
145 std::unique_ptr<Server> server(builder.BuildAndStart());
146 std::cout << "Server listening on " << server_address << std::endl;
154 int main(int argc, char* argv[]) {
156 std::cout << engine_info() << std::endl;
165 Threads.set(Options["Threads"]);
166 Search::clear(); // After threads are up
168 std::thread(&rpc_thread).detach();
170 UCI::loop(argc, argv);