Skip to content

Commit

Permalink
add readExact
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed May 2, 2024
1 parent 3bd38aa commit 285c649
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 66 deletions.
66 changes: 66 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated from CLion C/C++ Code Style settings
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 0
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Left
ReflowComments: false
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: false
SpaceBeforeInheritanceColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never
40 changes: 24 additions & 16 deletions include/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,38 @@

class Connection {
public:
virtual bool read(std::vector<unsigned char> &buffer, size_t *bytesRead = nullptr) = 0;
virtual bool read(std::vector<unsigned char>& buffer,
size_t* bytesRead = nullptr) = 0;

virtual bool read(unsigned char *buffer, size_t size, size_t *bytesRead = nullptr) = 0;
virtual bool read(unsigned char* buffer, size_t size,
size_t* bytesRead = nullptr) = 0;

virtual bool write(const std::string &message) = 0;
virtual bool readExact(std::vector<unsigned char>& buffer) = 0;

virtual bool write(const std::vector<unsigned char> &data) = 0;
virtual bool readExact(unsigned char* buffer, size_t size) = 0;

virtual bool write(const std::string& message) = 0;

virtual bool write(const std::vector<unsigned char>& data) = 0;

virtual ~Connection() = default;
};

class Socket : public Connection {
class Socket: public Connection {
public:
Socket();

bool read(std::vector<unsigned char> &buffer, size_t *bytesRead) override;
bool read(std::vector<unsigned char>& buffer, size_t* bytesRead) override;

bool read(unsigned char* buffer, size_t size, size_t* bytesRead) override;

bool read(unsigned char *buffer, size_t size, size_t *bytesRead) override;
bool readExact(std::vector<unsigned char>& buffer) override;

bool write(const std::string &message) override;
bool readExact(unsigned char* buffer, size_t size) override;

bool write(const std::vector<unsigned char> &data) override;
bool write(const std::string& message) override;

bool write(const std::vector<unsigned char>& data) override;

void close();

Expand All @@ -44,20 +54,19 @@ class Socket : public Connection {

std::unique_ptr<Connection> accept();

bool connect(const std::string &ip, int port);
bool connect(const std::string& ip, int port);

private:
struct Impl;
std::unique_ptr<Impl> pimpl_;
};


class ClientSocket : public Socket {
class ClientSocket: public Socket {
public:
bool connect(const std::string &ip, int port);
bool connect(const std::string& ip, int port);
};

class ServerSocket : public Socket {
class ServerSocket: public Socket {
public:
void bind(int port);

Expand All @@ -66,5 +75,4 @@ class ServerSocket : public Socket {
std::unique_ptr<Connection> accept();
};


#endif//SPHEROSIM_SOCKET_HPP
#endif// SPHEROSIM_SOCKET_HPP
103 changes: 59 additions & 44 deletions src/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ struct Socket::Impl {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
}

bool connect(const std::string &ip, int port) {
bool connect(const std::string& ip, int port) {
sockaddr_in serv_addr{};
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if (inet_pton(AF_INET, ip.c_str(), &serv_addr.sin_addr) <= 0) {

return false;
}
if (::connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
if (::connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) <
0) {

return false;
}
Expand All @@ -49,7 +50,7 @@ struct Socket::Impl {
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);

if (::bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
if (::bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
std::cerr << "Bind failed" << std::endl;
exit(1);
}
Expand All @@ -66,7 +67,8 @@ struct Socket::Impl {

sockaddr_in client_addr{};
socklen_t addrlen = sizeof(client_addr);
SOCKET new_sock = ::accept(sockfd, (struct sockaddr *) &client_addr, &addrlen);
SOCKET new_sock =
::accept(sockfd, (struct sockaddr*) &client_addr, &addrlen);

#ifdef _WIN32
if (new_sock == INVALID_SOCKET) {
Expand All @@ -84,20 +86,44 @@ struct Socket::Impl {
return conn;
}

bool read(unsigned char *buffer, size_t size, size_t *bytesRead) {
bool read(unsigned char* buffer, size_t size, size_t* bytesRead) {

#ifdef _WIN32
auto read = recv(sockfd, reinterpret_cast<char *>(buffer), size, 0);
if (bytesRead) *bytesRead = read;
auto read = recv(sockfd, reinterpret_cast<char*>(buffer), size, 0);
if (bytesRead)
*bytesRead = read;
return read != SOCKET_ERROR && read != 0;
#else
auto read = ::read(sockfd, reinterpret_cast<char *>(buffer), size);
if (bytesRead) *bytesRead = read;
auto read = ::read(sockfd, reinterpret_cast<char*>(buffer), size);
if (bytesRead)
*bytesRead = read;
return read != -1;
#endif
}

bool write(const std::string &buffer) {
bool readExact(unsigned char* buffer, size_t size) {

int totalBytesReceived = 0;
while (totalBytesReceived < size) {
#ifdef _WIN32
auto read = recv(sockfd, reinterpret_cast<char*>(buffer), size, 0);
if (read == SOCKET_ERROR || read == 0) {
return false;
}
totalBytesReceived += read;
#else
auto read = ::read(sockfd, reinterpret_cast<char*>(buffer), size);
if (read == -1 || read == 0) {
return false;
}
totalBytesReceived += read;
#endif
}

return true;
}

bool write(const std::string& buffer) {

#ifdef _WIN32
return send(sockfd, buffer.data(), buffer.size(), 0) != SOCKET_ERROR;
Expand All @@ -106,10 +132,11 @@ struct Socket::Impl {
#endif
}

bool write(const std::vector<unsigned char> &buffer) {
bool write(const std::vector<unsigned char>& buffer) {

#ifdef _WIN32
return send(sockfd, reinterpret_cast<const char *>(buffer.data()), buffer.size(), 0) != SOCKET_ERROR;
return send(sockfd, reinterpret_cast<const char*>(buffer.data()),
buffer.size(), 0) != SOCKET_ERROR;
#else
return ::write(sockfd, buffer.data(), buffer.size()) != -1;
#endif
Expand Down Expand Up @@ -148,69 +175,57 @@ struct Socket::Impl {
bool closed{false};
};

Socket::Socket() : pimpl_(std::make_unique<Impl>()) {}
Socket::Socket(): pimpl_(std::make_unique<Impl>()) {}


bool Socket::read(std::vector<unsigned char> &buffer, size_t *bytesRead) {
bool Socket::read(std::vector<unsigned char>& buffer, size_t* bytesRead) {

return pimpl_->read(buffer.data(), buffer.size(), bytesRead);
}

bool Socket::read(unsigned char *buffer, size_t size, size_t *bytesRead) {
bool Socket::read(unsigned char* buffer, size_t size, size_t* bytesRead) {

return pimpl_->read(buffer, size, bytesRead);
}

bool Socket::write(const std::string &buffer) {
bool Socket::readExact(std::vector<unsigned char>& buffer) {

return pimpl_->write(buffer);
return pimpl_->readExact(buffer.data(), buffer.size());
}

bool Socket::write(const std::vector<unsigned char> &buffer) {
bool Socket::readExact(unsigned char* buffer, size_t size) {

return pimpl_->write(buffer);
return pimpl_->readExact(buffer, size);
}

bool Socket::connect(const std::string &ip, int port) {

return pimpl_->connect(ip, port);
}
bool Socket::write(const std::string& buffer) { return pimpl_->write(buffer); }

void Socket::bind(int port) {
bool Socket::write(const std::vector<unsigned char>& buffer) {

pimpl_->bind(port);
return pimpl_->write(buffer);
}

void Socket::listen(int backlog) {
bool Socket::connect(const std::string& ip, int port) {

pimpl_->listen(backlog);
return pimpl_->connect(ip, port);
}

std::unique_ptr<Connection> Socket::accept() {
void Socket::bind(int port) { pimpl_->bind(port); }

return pimpl_->accept();
}
void Socket::listen(int backlog) { pimpl_->listen(backlog); }

void Socket::close() {
std::unique_ptr<Connection> Socket::accept() { return pimpl_->accept(); }

pimpl_->close();
}
void Socket::close() { pimpl_->close(); }

Socket::~Socket() = default;

bool ClientSocket::connect(const std::string& ip, int port) {

bool ClientSocket::connect(const std::string &ip, int port) {
return Socket::connect(ip, port);
}

void ServerSocket::bind(int port) {
Socket::bind(port);
}
void ServerSocket::bind(int port) { Socket::bind(port); }

void ServerSocket::listen(int backlog) {
Socket::listen(backlog);
}
void ServerSocket::listen(int backlog) { Socket::listen(backlog); }

std::unique_ptr<Connection> ServerSocket::accept() {
return Socket::accept();
}
std::unique_ptr<Connection> ServerSocket::accept() { return Socket::accept(); }
4 changes: 2 additions & 2 deletions tests/integration/run_server.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "Socket.hpp"

#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
#include <atomic>

void socketHandler(std::unique_ptr<Connection> conn) {
size_t n;
Expand All @@ -27,7 +27,7 @@ int main() {
std::unique_ptr<Connection> conn;
try {
conn = server.accept();
} catch (const std::exception &e) {
} catch (const std::exception& e) {
continue;
}

Expand Down
Loading

0 comments on commit 285c649

Please sign in to comment.