receiver socket

This commit is contained in:
Erik Frojdh
2019-05-15 17:51:48 +02:00
parent 40c2d9f760
commit e252b8e0e9
9 changed files with 440 additions and 342 deletions

View File

@ -6,6 +6,7 @@ set(SOURCES
src/DataSocket.cpp
src/ServerSocket.cpp
src/ServerInterface.cpp
src/ServerInterface2.cpp
src/network_utils.cpp
)

View File

@ -0,0 +1,28 @@
#pragma once
#include "DataSocket.h"
namespace sls {
class ServerInterface2;
}
#include "ServerSocket.h"
#include "sls_detector_defs.h"
namespace sls {
class ServerInterface2 : public DataSocket {
using defs = slsDetectorDefs;
public:
ServerInterface2(int socketId) : DataSocket(socketId){}
int sendResult(bool update, int ret, void *retval, int retvalSize,
char *mess = nullptr);
int receiveArg(int &ret, char *mess, void *arg, int sizeofArg);
private:
};
} // namespace sls

View File

@ -1,7 +1,8 @@
#pragma once
#include "DataSocket.h"
#include "ServerInterface2.h"
#include "network_utils.h"
#include <cstdint>
#include <netdb.h>
#include <string>
@ -13,14 +14,18 @@ namespace sls {
class ServerSocket : public DataSocket {
public:
ServerSocket(int port);
DataSocket accept();
const std::string &getLastClient();
ServerInterface2 accept();
constexpr IpAddr getLastClient() noexcept { return lastClient; }
constexpr IpAddr getThisClient() noexcept { return thisClient; }
constexpr IpAddr getLockedBy() noexcept { return lockedBy; }
void setLockedBy(IpAddr addr){ lockedBy = addr; }
int getPort() const;
void SendResult(int &ret, void *retval, int retvalSize, char* mess);
void SendResult(int &ret, void *retval, int retvalSize, char *mess);
private:
std::string lastClient_ = std::string(INET_ADDRSTRLEN, '\0');
std::string thisClient_ = std::string(INET_ADDRSTRLEN, '\0');
IpAddr thisClient;
IpAddr lastClient;
IpAddr lockedBy;
int serverPort;
// char lastClient_[INET_ADDRSTRLEN]{};
};

View File

@ -11,6 +11,7 @@ class IpAddr {
uint32_t addr_{0};
public:
constexpr IpAddr() noexcept{}
constexpr IpAddr(uint32_t address) noexcept : addr_{address} {}
IpAddr(const std::string &address);
IpAddr(const char *address);

View File

@ -0,0 +1,32 @@
#include "ServerInterface2.h"
namespace sls {
int ServerInterface2::sendResult(bool update, int ret, void *retval,
int retvalSize, char *mess) {
// if (update && ret == defs::OK && server_->DifferentClients()) {
// ret = defs::FORCE_UPDATE;
// }
sendData(&ret, sizeof(ret));
if (ret == defs::FAIL) {
// send error message
if (mess)
sendData(mess, MAX_STR_LENGTH);
// debugging feature. should not happen.
else
FILE_LOG(logERROR) << "No error message provided for this "
"failure. Will mess up TCP\n";
}
sendData(retval, retvalSize);
return ret;
}
int ServerInterface2::receiveArg(int &ret, char *mess, void *arg,
int sizeofArg) {
if (sizeofArg && receiveData(arg, sizeofArg) < 0)
return defs::FAIL;
return defs::OK;
}
} // namespace sls

View File

@ -1,4 +1,6 @@
#include "ServerInterface2.h"
#include "ServerSocket.h"
#include "DataSocket.h"
#include "logger.h"
#include "sls_detector_defs.h"
@ -35,7 +37,8 @@ ServerSocket::ServerSocket(int port)
}
}
DataSocket ServerSocket::accept() {
ServerInterface2 ServerSocket::accept() {
lastClient = thisClient; //update from previous connection
struct sockaddr_in clientAddr;
socklen_t addr_size = sizeof clientAddr;
int newSocket =
@ -43,17 +46,15 @@ DataSocket ServerSocket::accept() {
if (newSocket == -1) {
throw sls::SocketError("Server ERROR: socket accept failed\n");
}
inet_ntop(AF_INET, &(clientAddr.sin_addr), &thisClient_.front(),
INET_ADDRSTRLEN);
std::cout << "lastClient: " << lastClient_ << " thisClient: " << thisClient_
char tc[INET_ADDRSTRLEN]{};
inet_ntop(AF_INET, &(clientAddr.sin_addr), tc, INET_ADDRSTRLEN);
thisClient = tc;
std::cout << "lastClient: " << lastClient << " thisClient: " << thisClient
<< '\n';
// Here goes any check for locks etc
lastClient_ = thisClient_;
return DataSocket(newSocket);
return ServerInterface2(newSocket);
}
const std::string &ServerSocket::getLastClient() { return lastClient_; }
int ServerSocket::getPort() const { return serverPort; }

View File

@ -16,6 +16,7 @@
namespace sls {
IpAddr::IpAddr(const std::string &address) {
inet_pton(AF_INET, address.c_str(), &addr_);
}