Skip to content

Commit

Permalink
Apply clang-format to networking namespace. (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkrodrigues authored Dec 6, 2023
1 parent 6571d8d commit ecb5ab1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 90 deletions.
1 change: 0 additions & 1 deletion components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ set(SOURCE_FILES_clo
src/LogTypeDictionaryReader.hpp
src/networking/socket_utils.cpp
src/networking/socket_utils.hpp
src/networking/SocketOperationFailed.cpp
src/networking/SocketOperationFailed.hpp
src/PageAllocatedVector.cpp
src/PageAllocatedVector.hpp
Expand Down
1 change: 0 additions & 1 deletion components/core/src/networking/SocketOperationFailed.cpp

This file was deleted.

23 changes: 10 additions & 13 deletions components/core/src/networking/SocketOperationFailed.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#ifndef NETWORKING_SOCKETOPERATIONFAILED_HPP
#define NETWORKING_SOCKETOPERATIONFAILED_HPP

// Project headers
#include "../ErrorCode.hpp"
#include "../TraceableException.hpp"

namespace networking {
class SocketOperationFailed : public TraceableException {
public:
// Constructors
SocketOperationFailed (ErrorCode error_code, const char* const filename, int line_number) :
TraceableException(error_code, filename, line_number) {}
class SocketOperationFailed : public TraceableException {
public:
// Constructors
SocketOperationFailed(ErrorCode error_code, char const* const filename, int line_number)
: TraceableException(error_code, filename, line_number) {}

// Methods
const char* what () const noexcept override {
return "Socket operation failed";
}
};
}
// Methods
[[nodiscard]] char const* what() const noexcept override { return "Socket operation failed"; }
};
} // namespace networking

#endif //NETWORKING_SOCKETOPERATIONFAILED_HPP
#endif // NETWORKING_SOCKETOPERATIONFAILED_HPP
70 changes: 33 additions & 37 deletions components/core/src/networking/socket_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,54 @@
#include "socket_utils.hpp"

// C standard libraries
#include <sys/socket.h>

// C++ standard libraries
#include <cstdio>

// Project headers
#include "../Defs.h"
#include "SocketOperationFailed.hpp"

namespace networking {
ErrorCode try_send (int fd, const char* buf, size_t buf_len) {
if (fd < 0 || nullptr == buf) {
return ErrorCode_BadParam;
}

ssize_t num_bytes_sent = ::send(fd, buf, buf_len, 0);
if (-1 == num_bytes_sent) {
return ErrorCode_errno;
}

return ErrorCode_Success;
ErrorCode try_send(int fd, char const* buf, size_t buf_len) {
if (fd < 0 || nullptr == buf) {
return ErrorCode_BadParam;
}

void send (int fd, const char* buf, size_t buf_len) {
auto error_code = try_send(fd, buf, buf_len);
if (ErrorCode_Success != error_code) {
throw SocketOperationFailed(error_code, __FILENAME__, __LINE__);
}
ssize_t num_bytes_sent = ::send(fd, buf, buf_len, 0);
if (-1 == num_bytes_sent) {
return ErrorCode_errno;
}

return ErrorCode_Success;
}

ErrorCode try_receive (int fd, char* buf, size_t buf_len, size_t& num_bytes_received) {
if (fd < 0 || nullptr == buf) {
return ErrorCode_BadParam;
}
void send(int fd, char const* buf, size_t buf_len) {
auto error_code = try_send(fd, buf, buf_len);
if (ErrorCode_Success != error_code) {
throw SocketOperationFailed(error_code, __FILENAME__, __LINE__);
}
}

ssize_t result = recv(fd, buf, buf_len, 0);
if (result < 0) {
return ErrorCode_errno;
}
if (0 == result) {
return ErrorCode_EndOfFile;
}
num_bytes_received = result;
ErrorCode try_receive(int fd, char* buf, size_t buf_len, size_t& num_bytes_received) {
if (fd < 0 || nullptr == buf) {
return ErrorCode_BadParam;
}

return ErrorCode_Success;
ssize_t result = recv(fd, buf, buf_len, 0);
if (result < 0) {
return ErrorCode_errno;
}
if (0 == result) {
return ErrorCode_EndOfFile;
}
num_bytes_received = result;

return ErrorCode_Success;
}

void receive (int fd, char* buf, size_t buf_len, size_t& num_bytes_received) {
auto error_code = try_receive(fd, buf, buf_len, num_bytes_received);
if (ErrorCode_Success != error_code) {
throw SocketOperationFailed(error_code, __FILENAME__, __LINE__);
}
void receive(int fd, char* buf, size_t buf_len, size_t& num_bytes_received) {
auto error_code = try_receive(fd, buf, buf_len, num_bytes_received);
if (ErrorCode_Success != error_code) {
throw SocketOperationFailed(error_code, __FILENAME__, __LINE__);
}
}
} // namespace networking
74 changes: 36 additions & 38 deletions components/core/src/networking/socket_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
#ifndef NETWORKING_SOCKET_UTILS_HPP
#define NETWORKING_SOCKET_UTILS_HPP

// C++ standard libraries
#include <cstddef>

// Project headers
#include "../ErrorCode.hpp"

namespace networking {
// Methods
/**
* Tries to send a buffer of data over the socket
* @param fd
* @param buf
* @param buf_len
* @return ErrorCode_BadParam if the file descriptor or buffer pointer is invalid
* @return ErrorCode_errno if sending failed
* @return ErrorCode_Success otherwise
*/
ErrorCode try_send (int fd, const char* buf, size_t buf_len);
/**
* Sends a buffer of data over the socket
* @param fd
* @param buf
* @param buf_len
*/
void send (int fd, const char* buf, size_t buf_len);
// Methods
/**
* Tries to send a buffer of data over the socket
* @param fd
* @param buf
* @param buf_len
* @return ErrorCode_BadParam if the file descriptor or buffer pointer is invalid
* @return ErrorCode_errno if sending failed
* @return ErrorCode_Success otherwise
*/
ErrorCode try_send(int fd, char const* buf, size_t buf_len);
/**
* Sends a buffer of data over the socket
* @param fd
* @param buf
* @param buf_len
*/
void send(int fd, char const* buf, size_t buf_len);

/**
* Tries to receive up to a given number of bytes over a socket
* @param buf Buffer to store received bytes
* @param buf_len Number of bytes to receive
* @return ErrorCode_BadParam if file descriptor or buffer pointer are invalid
* @return ErrorCode_EndOfFile on EOF
* @return ErrorCode_errno if receiving failed
* @return ErrorCode_Success otherwise
*/
ErrorCode try_receive (int fd, char* buf, size_t buf_len, size_t& num_bytes_received);
/**
* Receives up to the give number of bytes over a socket
* @param buf Buffer to store received bytes
* @param buf_len Number of bytes to receive
*/
void receive (int fd, char* buf, size_t buf_len, size_t& num_bytes_received);
}
/**
* Tries to receive up to a given number of bytes over a socket
* @param buf Buffer to store received bytes
* @param buf_len Number of bytes to receive
* @return ErrorCode_BadParam if file descriptor or buffer pointer are invalid
* @return ErrorCode_EndOfFile on EOF
* @return ErrorCode_errno if receiving failed
* @return ErrorCode_Success otherwise
*/
ErrorCode try_receive(int fd, char* buf, size_t buf_len, size_t& num_bytes_received);
/**
* Receives up to the give number of bytes over a socket
* @param buf Buffer to store received bytes
* @param buf_len Number of bytes to receive
*/
void receive(int fd, char* buf, size_t buf_len, size_t& num_bytes_received);
} // namespace networking

#endif //NETWORKING_SOCKET_UTILS_HPP
#endif // NETWORKING_SOCKET_UTILS_HPP

0 comments on commit ecb5ab1

Please sign in to comment.