abstract common tcp operations to a base class
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
|
||||
#include "TcpServer.hpp"
|
||||
|
||||
TcpServer::TcpServer(ConfigService* config, LoggerService* logger) : config_(config), logger_(logger) {
|
||||
TcpServer::TcpServer(ConfigService* config, LoggerService* logger) : TcpBase(logger) {
|
||||
|
||||
if(!(config->getConfig<ServerConfig>("TcpServer", "main", &configuration_))) {
|
||||
logger_->log("TcpServer", LogFlag::Error, "Failed to get configuration");
|
||||
@@ -79,6 +79,7 @@ ErrorCode TcpServer::init() {
|
||||
// TODO: more robust errorchecking with errnos
|
||||
return ErrorCode::Error;
|
||||
}
|
||||
// TODO: because accept blocks, the server can't handle parallel clients.
|
||||
logger_->log("TcpServer", LogFlag::Info
|
||||
, "Client connected from {}:{}",
|
||||
inet_ntoa(clientAddress.sin_addr), ntohs(clientAddress.sin_port));
|
||||
@@ -86,17 +87,19 @@ ErrorCode TcpServer::init() {
|
||||
// receive loop
|
||||
while(1) {
|
||||
// receive messages from the client. blocks until there's a message to receive
|
||||
receiveBytes = recv(clientSocket, buffer, sizeof(buffer), 0);
|
||||
// last arg is flags, 0=default
|
||||
// MSG_PEEK: read without consuming the message from the queue
|
||||
// MSG_WAITALL: block until all of the specified size is received
|
||||
// MSG_DONTWAIT: non-blocking receive, returns -1 immediately if queue is empty
|
||||
if(receiveBytes <= 0) break; // exit when there's nothing left to receive
|
||||
receiveBytes = tcpRead(clientSocket, buffer, sizeof(buffer));
|
||||
std::string message(buffer, receiveBytes);
|
||||
|
||||
logger_->log("TcpClient", LogFlag::Debug, "Received {} bytes from client: 0x{:x}", receiveBytes, buffer[0]);
|
||||
// everything below is a simulated "processMessage()"
|
||||
logger_->log("TcpClient", LogFlag::Debug, "Received {} bytes from client {}:{}: {}",
|
||||
receiveBytes, inet_ntoa(clientAddress.sin_addr), ntohs(clientAddress.sin_port), message);
|
||||
|
||||
// echo back received data back to the client
|
||||
sendBytes = send(clientSocket, buffer, receiveBytes, 0); // TODO: send size is not guarenteed
|
||||
sendBytes = tcpSend(clientSocket, buffer, receiveBytes);
|
||||
// TODO: send size is not guarenteed
|
||||
// the solution might be a standard header that contains message type, checksum, and message length
|
||||
// i.e. first four bytes are the total length of the message
|
||||
|
||||
if(sendBytes < 0) {
|
||||
logger_->log("TcpServer", LogFlag::Error, "Unable to send to client.");
|
||||
return ErrorCode::Error;
|
||||
|
||||
@@ -12,9 +12,10 @@
|
||||
#include "common/config/ConfigService.hpp"
|
||||
#include "common/LoggerService.hpp"
|
||||
#include "common/ErrorCodes.hpp"
|
||||
#include "common/TcpBase.hpp"
|
||||
#include "config/ServerConfig.hpp"
|
||||
|
||||
class TcpServer {
|
||||
class TcpServer : TcpBase {
|
||||
|
||||
public:
|
||||
|
||||
@@ -25,8 +26,6 @@ private:
|
||||
|
||||
ErrorCode init();
|
||||
|
||||
LoggerService* logger_;
|
||||
ConfigService* config_;
|
||||
ServerParams configuration_;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user