diff --git a/cpp-console-game/main.cpp b/cpp-console-game/main.cpp index 7d13850..86f039c 100644 --- a/cpp-console-game/main.cpp +++ b/cpp-console-game/main.cpp @@ -51,6 +51,30 @@ void renderLoop() } } +std::pair movePlayer(std::vector args) +{ + int playerId = std::stoi(args[0]); + int x = std::stoi(args[1]); + int y = std::stoi(args[2]); + + //gameState.movePlayer(playerId, {x, y}); + std::cout << "Player " << playerId << " moved to (" << x << ", " << y << ")" << std::endl; + + return {true, "Move Successful"}; +} + +std::pair echo(std::vector args) +{ + for (auto arg : args) + { + std::cout << arg << " "; + } + + std::cout << std::endl; + + return {true, "Echo Successful"}; +} + int main(int argc, char **argv) { if (argc < 4) @@ -66,14 +90,20 @@ int main(int argc, char **argv) if (mode == "server") { eRPC::Server server(port); + server.bindMethod("movePlayer", movePlayer); + server.bindMethod("echo", echo); server.start(); } else if (mode == "client") { eRPC::Client client(host, port); - client.openConnection(); - client.call("Hello from client"); - client.closeConnection(); + + client.call("echo", {"Hello", "World"}); + client.call("movePlayer", {"1", "5", "5"}); + client.call("movePlayer", {"1", "6", "6"}); + auto ret = client.call("echol", {"Goodbye", "World"}); + + std::cout << "Result: " << ret << std::endl; } else { diff --git a/eRPC/Client.cpp b/eRPC/Client.cpp index 294e22a..542b929 100644 --- a/eRPC/Client.cpp +++ b/eRPC/Client.cpp @@ -8,25 +8,22 @@ #include #include +int msgIdCounter = 0; + namespace eRPC { - Client::Client(std::string host, int port) : sockfd(-1), serv_addr() + Client::Client(std::string host, int port) : sockfd(-1), serverAddress() { - if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - { - throw std::runtime_error("Failed to create socket"); - } + sockaddr_in serverAddress; + serverAddress.sin_family = AF_INET; + serverAddress.sin_port = htons(port); - sockaddr_in serv_addr; - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(port); - - if (inet_pton(AF_INET, host.c_str(), &serv_addr.sin_addr) <= 0) + if (inet_pton(AF_INET, host.c_str(), &serverAddress.sin_addr) <= 0) { throw std::runtime_error("Invalid address"); } - this->serv_addr = serv_addr; + this->serverAddress = serverAddress; } Client::~Client() @@ -36,39 +33,43 @@ namespace eRPC void Client::openConnection() { - if (sockfd < 0) + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - throw std::runtime_error("Socket not created"); + throw std::runtime_error("Failed to create socket"); } - if (connect(sockfd, (sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + if (connect(sockfd, (sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) { throw std::runtime_error("Failed to connect to server"); } - - std::cout << "Connected to server" << std::endl; } void Client::closeConnection() { close(sockfd); + sockfd = -1; } - void Client::call(std::string method) + std::string Client::call(std::string method, std::vector params) { - if (sockfd < 0) - { - throw std::runtime_error("Socket not created"); - } + Client::openConnection(); - Request request(0, method, {"First argument", "Another argument"}); + Request request(msgIdCounter++, method, params); auto serialized = request.serialize(); write(sockfd, serialized.c_str(), serialized.size() + 1); char buffer[1024] = {0}; read(sockfd, buffer, 1024); + Client::closeConnection(); + Response response(buffer); - std::cout << "Received response:\n" << response.serialize() << std::endl; + + if (!response.isOk()) + { + throw std::runtime_error(response.getResult()); + } + + return response.getResult(); } } \ No newline at end of file diff --git a/eRPC/Client.hpp b/eRPC/Client.hpp index 6b9bd7c..9b4d2ff 100644 --- a/eRPC/Client.hpp +++ b/eRPC/Client.hpp @@ -2,6 +2,7 @@ #define ERPC_CLIENT_HPP #include +#include #include namespace eRPC @@ -11,13 +12,14 @@ namespace eRPC public: Client(std::string host, int port); ~Client(); - void openConnection(); - void closeConnection(); - void call(std::string method); + std::string call(std::string method, std::vector params); private: int sockfd; - sockaddr_in serv_addr; + sockaddr_in serverAddress; + + void openConnection(); + void closeConnection(); }; } diff --git a/eRPC/Request.cpp b/eRPC/Request.cpp index 75fdd76..0cbaa40 100644 --- a/eRPC/Request.cpp +++ b/eRPC/Request.cpp @@ -18,7 +18,7 @@ namespace eRPC std::getline(iss, line); if (line != "eRPC 1.0") { - throw std::runtime_error("Invalid message"); + throw std::runtime_error("Invalid Request"); } std::getline(iss, line); diff --git a/eRPC/Response.cpp b/eRPC/Response.cpp index fb60b35..2298d53 100644 --- a/eRPC/Response.cpp +++ b/eRPC/Response.cpp @@ -17,7 +17,7 @@ namespace eRPC std::getline(iss, line); if (line != "eRPC 1.0") { - throw std::runtime_error("Invalid message"); + throw std::runtime_error("Invalid Response"); } std::getline(iss, line); diff --git a/eRPC/Server.cpp b/eRPC/Server.cpp index 5bb99fa..32cd4e6 100644 --- a/eRPC/Server.cpp +++ b/eRPC/Server.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include namespace eRPC @@ -32,8 +31,6 @@ namespace eRPC { throw std::runtime_error("Failed to listen on socket"); } - - std::cout << "Server listening on port " << port << std::endl; } Server::~Server() @@ -52,36 +49,32 @@ namespace eRPC while (running) { - std::cout << "Waiting for connection" << std::endl; - - int connfd = accept(sockfd, (sockaddr *)NULL, NULL); + auto connfd = accept(sockfd, (sockaddr *)NULL, NULL); char buffer[1024] = {0}; read(connfd, buffer, 1024); Request request(buffer); - std::cout << "Received request:\n" - << request.serialize() << std::endl; std::unique_ptr response; if (methods.find(request.getMethod()) != methods.end()) { - std::function method = methods[request.getMethod()]; - method(); + auto method = methods[request.getMethod()]; + auto ret = method(request.getParams()); response = std::make_unique(Response( request.getMsgid(), - true, - "RESULTS HERE")); + ret.first, + ret.second)); } else { response = std::make_unique(Response( request.getMsgid(), false, - "Method not found")); + "Method \"" + request.getMethod() + "\" not found")); } - std::string serialized = response->serialize(); + auto serialized = response->serialize(); write(connfd, serialized.c_str(), serialized.size() + 1); close(connfd); @@ -95,7 +88,7 @@ namespace eRPC running = false; } - void Server::bindMethod(std::string method, std::function callback) + void Server::bindMethod(std::string method, std::function(std::vector)> callback) { methods[method] = callback; } diff --git a/eRPC/Server.hpp b/eRPC/Server.hpp index 10d82ca..d3a6858 100644 --- a/eRPC/Server.hpp +++ b/eRPC/Server.hpp @@ -33,12 +33,12 @@ namespace eRPC /** * Bind a method to a callback. */ - void bindMethod(std::string method, std::function callback); - + void bindMethod(std::string method, std::function(std::vector)> callback); + private: int sockfd; bool running; - std::unordered_map> methods; + std::unordered_map(std::vector)>> methods; }; }