-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply clang-format to networking namespace. (#173)
- Loading branch information
1 parent
6571d8d
commit ecb5ab1
Showing
5 changed files
with
79 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |