Refine RPC library
Got multiple remote calls from one client working Improved error messages Got function name and arguments sent over as well as return values
This commit is contained in:
parent
ed64fb05b1
commit
c9a50d363e
@ -51,6 +51,30 @@ void renderLoop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::pair<bool, std::string> movePlayer(std::vector<std::string> 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<bool, std::string> echo(std::vector<std::string> args)
|
||||||
|
{
|
||||||
|
for (auto arg : args)
|
||||||
|
{
|
||||||
|
std::cout << arg << " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
return {true, "Echo Successful"};
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if (argc < 4)
|
if (argc < 4)
|
||||||
@ -66,14 +90,20 @@ int main(int argc, char **argv)
|
|||||||
if (mode == "server")
|
if (mode == "server")
|
||||||
{
|
{
|
||||||
eRPC::Server server(port);
|
eRPC::Server server(port);
|
||||||
|
server.bindMethod("movePlayer", movePlayer);
|
||||||
|
server.bindMethod("echo", echo);
|
||||||
server.start();
|
server.start();
|
||||||
}
|
}
|
||||||
else if (mode == "client")
|
else if (mode == "client")
|
||||||
{
|
{
|
||||||
eRPC::Client client(host, port);
|
eRPC::Client client(host, port);
|
||||||
client.openConnection();
|
|
||||||
client.call("Hello from client");
|
client.call("echo", {"Hello", "World"});
|
||||||
client.closeConnection();
|
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
|
else
|
||||||
{
|
{
|
||||||
|
@ -8,25 +8,22 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
int msgIdCounter = 0;
|
||||||
|
|
||||||
namespace eRPC
|
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)
|
sockaddr_in serverAddress;
|
||||||
{
|
serverAddress.sin_family = AF_INET;
|
||||||
throw std::runtime_error("Failed to create socket");
|
serverAddress.sin_port = htons(port);
|
||||||
}
|
|
||||||
|
|
||||||
sockaddr_in serv_addr;
|
if (inet_pton(AF_INET, host.c_str(), &serverAddress.sin_addr) <= 0)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Invalid address");
|
throw std::runtime_error("Invalid address");
|
||||||
}
|
}
|
||||||
|
|
||||||
this->serv_addr = serv_addr;
|
this->serverAddress = serverAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
Client::~Client()
|
Client::~Client()
|
||||||
@ -36,39 +33,43 @@ namespace eRPC
|
|||||||
|
|
||||||
void Client::openConnection()
|
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");
|
throw std::runtime_error("Failed to connect to server");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Connected to server" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::closeConnection()
|
void Client::closeConnection()
|
||||||
{
|
{
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
sockfd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::call(std::string method)
|
std::string Client::call(std::string method, std::vector<std::string> params)
|
||||||
{
|
{
|
||||||
if (sockfd < 0)
|
Client::openConnection();
|
||||||
{
|
|
||||||
throw std::runtime_error("Socket not created");
|
|
||||||
}
|
|
||||||
|
|
||||||
Request request(0, method, {"First argument", "Another argument"});
|
Request request(msgIdCounter++, method, params);
|
||||||
auto serialized = request.serialize();
|
auto serialized = request.serialize();
|
||||||
write(sockfd, serialized.c_str(), serialized.size() + 1);
|
write(sockfd, serialized.c_str(), serialized.size() + 1);
|
||||||
|
|
||||||
char buffer[1024] = {0};
|
char buffer[1024] = {0};
|
||||||
read(sockfd, buffer, 1024);
|
read(sockfd, buffer, 1024);
|
||||||
|
|
||||||
|
Client::closeConnection();
|
||||||
|
|
||||||
Response response(buffer);
|
Response response(buffer);
|
||||||
std::cout << "Received response:\n" << response.serialize() << std::endl;
|
|
||||||
|
if (!response.isOk())
|
||||||
|
{
|
||||||
|
throw std::runtime_error(response.getResult());
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.getResult();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
#define ERPC_CLIENT_HPP
|
#define ERPC_CLIENT_HPP
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
namespace eRPC
|
namespace eRPC
|
||||||
@ -11,13 +12,14 @@ namespace eRPC
|
|||||||
public:
|
public:
|
||||||
Client(std::string host, int port);
|
Client(std::string host, int port);
|
||||||
~Client();
|
~Client();
|
||||||
void openConnection();
|
std::string call(std::string method, std::vector<std::string> params);
|
||||||
void closeConnection();
|
|
||||||
void call(std::string method);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int sockfd;
|
int sockfd;
|
||||||
sockaddr_in serv_addr;
|
sockaddr_in serverAddress;
|
||||||
|
|
||||||
|
void openConnection();
|
||||||
|
void closeConnection();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ namespace eRPC
|
|||||||
std::getline(iss, line);
|
std::getline(iss, line);
|
||||||
if (line != "eRPC 1.0")
|
if (line != "eRPC 1.0")
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Invalid message");
|
throw std::runtime_error("Invalid Request");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::getline(iss, line);
|
std::getline(iss, line);
|
||||||
|
@ -17,7 +17,7 @@ namespace eRPC
|
|||||||
std::getline(iss, line);
|
std::getline(iss, line);
|
||||||
if (line != "eRPC 1.0")
|
if (line != "eRPC 1.0")
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Invalid message");
|
throw std::runtime_error("Invalid Response");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::getline(iss, line);
|
std::getline(iss, line);
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace eRPC
|
namespace eRPC
|
||||||
@ -32,8 +31,6 @@ namespace eRPC
|
|||||||
{
|
{
|
||||||
throw std::runtime_error("Failed to listen on socket");
|
throw std::runtime_error("Failed to listen on socket");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Server listening on port " << port << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Server::~Server()
|
Server::~Server()
|
||||||
@ -52,36 +49,32 @@ namespace eRPC
|
|||||||
|
|
||||||
while (running)
|
while (running)
|
||||||
{
|
{
|
||||||
std::cout << "Waiting for connection" << std::endl;
|
auto connfd = accept(sockfd, (sockaddr *)NULL, NULL);
|
||||||
|
|
||||||
int connfd = accept(sockfd, (sockaddr *)NULL, NULL);
|
|
||||||
|
|
||||||
char buffer[1024] = {0};
|
char buffer[1024] = {0};
|
||||||
read(connfd, buffer, 1024);
|
read(connfd, buffer, 1024);
|
||||||
Request request(buffer);
|
Request request(buffer);
|
||||||
std::cout << "Received request:\n"
|
|
||||||
<< request.serialize() << std::endl;
|
|
||||||
|
|
||||||
std::unique_ptr<Response> response;
|
std::unique_ptr<Response> response;
|
||||||
if (methods.find(request.getMethod()) != methods.end())
|
if (methods.find(request.getMethod()) != methods.end())
|
||||||
{
|
{
|
||||||
std::function<void *()> method = methods[request.getMethod()];
|
auto method = methods[request.getMethod()];
|
||||||
method();
|
auto ret = method(request.getParams());
|
||||||
|
|
||||||
response = std::make_unique<Response>(Response(
|
response = std::make_unique<Response>(Response(
|
||||||
request.getMsgid(),
|
request.getMsgid(),
|
||||||
true,
|
ret.first,
|
||||||
"RESULTS HERE"));
|
ret.second));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
response = std::make_unique<Response>(Response(
|
response = std::make_unique<Response>(Response(
|
||||||
request.getMsgid(),
|
request.getMsgid(),
|
||||||
false,
|
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);
|
write(connfd, serialized.c_str(), serialized.size() + 1);
|
||||||
|
|
||||||
close(connfd);
|
close(connfd);
|
||||||
@ -95,7 +88,7 @@ namespace eRPC
|
|||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::bindMethod(std::string method, std::function<void *()> callback)
|
void Server::bindMethod(std::string method, std::function<std::pair<bool, std::string>(std::vector<std::string>)> callback)
|
||||||
{
|
{
|
||||||
methods[method] = callback;
|
methods[method] = callback;
|
||||||
}
|
}
|
||||||
|
@ -33,12 +33,12 @@ namespace eRPC
|
|||||||
/**
|
/**
|
||||||
* Bind a method to a callback.
|
* Bind a method to a callback.
|
||||||
*/
|
*/
|
||||||
void bindMethod(std::string method, std::function<void *()> callback);
|
void bindMethod(std::string method, std::function<std::pair<bool, std::string>(std::vector<std::string>)> callback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int sockfd;
|
int sockfd;
|
||||||
bool running;
|
bool running;
|
||||||
std::unordered_map<std::string, std::function<void *()>> methods;
|
std::unordered_map<std::string, std::function<std::pair<bool, std::string>(std::vector<std::string>)>> methods;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user