### Object files
OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
- search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
+ search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o \
+ hashprobe.grpc.pb.o hashprobe.pb.o
+CLIOBJS = client.o hashprobe.grpc.pb.o hashprobe.pb.o
### Establish the operating system name
KERNEL = $(shell uname -s)
### Section 5. Private targets
### ==========================================================================
-all: $(EXE) .depend
+all: $(EXE) client .depend
config-sanity:
@echo ""
EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
all
+### GRPC
+
+PROTOS_PATH = .
+PROTOC = protoc
+GRPC_CPP_PLUGIN = grpc_cpp_plugin
+GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
+
+%.grpc.pb.h %.grpc.pb.cc: %.proto
+ $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
+
+# oh my
+%.cpp: %.cc
+ cp $< $@
+
+%.pb.h %.pb.cc: %.proto
+ $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
+
+LDFLAGS += -lprotobuf -lgrpc++_unsecure -lgrpc_unsecure -lgrpc
+
+client: $(CLIOBJS)
+ $(CXX) -o $@ $(CLIOBJS) $(LDFLAGS)
+
+# Other stuff
+
.depend:
-@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
--- /dev/null
+#include <iostream>
+#include <memory>
+#include <string>
+
+#include <grpc++/grpc++.h>
+
+#include "hashprobe.grpc.pb.h"
+
+using grpc::Channel;
+using grpc::ClientContext;
+using grpc::Status;
+
+int main(int argc, char** argv) {
+ std::shared_ptr<Channel> channel(grpc::CreateChannel(
+ "localhost:50051", grpc::InsecureChannelCredentials()));
+ std::unique_ptr<HashProbe::Stub> stub(HashProbe::NewStub(channel));
+
+ HashProbeRequest request;
+// request.set_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
+ request.set_fen(argv[1]);
+
+ HashProbeResponse response;
+ ClientContext context;
+ Status status = stub->Probe(&context, request, &response);
+
+ if (status.ok()) {
+ std::cout << response.DebugString();
+ } else {
+ std::cout << "RPC failed";
+ }
+
+ return 0;
+}
--- /dev/null
+syntax = "proto3";
+
+message HashProbeRequest {
+ string fen = 1;
+}
+message HashProbeResponse {
+ bool found = 1;
+ int32 move = 2;
+ int32 value = 3;
+ int32 eval = 4;
+ int32 depth = 5;
+ int32 bound = 6;
+}
+
+service HashProbe {
+ rpc Probe(HashProbeRequest) returns (HashProbeResponse) {}
+}
*/
#include <iostream>
+#include <thread>
#include "bitboard.h"
#include "position.h"
#include "uci.h"
#include "syzygy/tbprobe.h"
+#include <grpc/grpc.h>
+#include <grpc++/server.h>
+#include <grpc++/server_builder.h>
+#include "hashprobe.grpc.pb.h"
+
+using grpc::Server;
+using grpc::ServerBuilder;
+using grpc::ServerContext;
+using grpc::Status;
+using grpc::StatusCode;
+
+class HashProbeImpl final : public HashProbe::Service {
+public:
+ Status Probe(ServerContext* context,
+ const HashProbeRequest* request,
+ HashProbeResponse *response) {
+ std::cout << "fen=" << request->fen() << std::endl;
+ Position pos(request->fen(), /*isChess960=*/false, Threads.main());
+ if (!pos.pos_is_ok()) {
+ return Status(StatusCode::INVALID_ARGUMENT, "Invalid FEN");
+ }
+ std::cout << "parsed=" << pos.fen() << std::endl;
+ bool found;
+ TTEntry *entry = TT.probe(pos.key(), found);
+ response->set_found(found);
+ if (found) {
+ response->set_move(entry->move());
+ response->set_value(entry->value());
+ response->set_eval(entry->eval());
+ response->set_depth(entry->depth());
+ response->set_bound(entry->bound());
+ }
+ return Status::OK;
+ }
+};
+
+void rpc_thread()
+{
+ std::string server_address("0.0.0.0:50051");
+ HashProbeImpl service;
+
+ ServerBuilder builder;
+ builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
+ builder.RegisterService(&service);
+ std::unique_ptr<Server> server(builder.BuildAndStart());
+ std::cout << "Server listening on " << server_address << std::endl;
+ server->Wait();
+}
+
namespace PSQT {
void init();
}
Threads.set(Options["Threads"]);
Search::clear(); // After threads are up
+ std::thread(&rpc_thread).detach();
+
UCI::loop(argc, argv);
Threads.set(0);
thisThread = th;
set_state(st);
- assert(pos_is_ok());
-
return *this;
}