-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Network - Add Socket Class * Network - Socket Fixes * App - Use Socket in Linux IPC Implementation * Network - Fix Socket and IPC * Network - Fix Socket Shutdown on Linux * Version Bump * Network - Fix `Socket` Build on Windows * Network - Fix Windows Socket Shutdown * Network - Add Pipe Support to Socket on Windows * Check Spell
- Loading branch information
Showing
16 changed files
with
578 additions
and
186 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef ADDRESSFAMILY_H | ||
#define ADDRESSFAMILY_H | ||
|
||
#ifdef _WIN32 | ||
#include <Winsock2.h> | ||
#else | ||
#include <sys/socket.h> | ||
#endif | ||
|
||
namespace Nickvision::Network | ||
{ | ||
/** | ||
* @brief Type of address that can be used by a socket. | ||
*/ | ||
enum class AddressFamily | ||
{ | ||
#ifdef _WIN32 | ||
Pipe, | ||
#else | ||
Unix = AF_UNIX, | ||
#endif | ||
IPv4 = AF_INET | ||
}; | ||
} | ||
|
||
#endif //ADDRESSFAMILY_H |
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 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#ifndef SOCKET_H | ||
#define SOCKET_H | ||
|
||
#include <memory> | ||
#include <string> | ||
#ifdef _WIN32 | ||
#include <Winsock2.h> | ||
#endif | ||
#include "addressfamily.h" | ||
#include "socketpurpose.h" | ||
#include "sockettype.h" | ||
|
||
namespace Nickvision::Network | ||
{ | ||
/** | ||
* @brief A network socket (an endpoint for communication). | ||
*/ | ||
class Socket | ||
{ | ||
public: | ||
/** | ||
* @brief Constructs a Socket. | ||
* @param purpose The purpose of the socket | ||
* @param type The SocketType of the socket (Ignored when AddressFamily::Pipe is used) | ||
* @param family The AddressFamily of the socket | ||
* @param address The address to bind/connect the socket to | ||
* @param port The port to bind/connect the socket to (Ignored when AddressFamily::Unix or AddressFamily::Pipe is used) | ||
* @throw std::invalid_argument Thrown if the address is invalid | ||
* @throw std::logic_error Thrown if the socket cannot be binded to (i.e. A server socket already exists) | ||
* @throw std::runtime_error Thrown on Windows if winsock cannot be initialized | ||
* @throw std::runtime_error Thrown if the socket cannot be created or listened | ||
*/ | ||
Socket(SocketPurpose purpose, SocketType type, AddressFamily family, const std::string& address, int port); | ||
/** | ||
* @brief Destructs a Socket. | ||
* @brief This will disconnect from a child socket if disconnect was not already called. | ||
*/ | ||
~Socket(); | ||
/** | ||
* @brief Establishes a connection. | ||
* @brief If the socket's purpose is SocketPurpose::Server, this method will block until a client is connected. | ||
* @brief Is the socket's purpose is SocketPurpose::Client, this method will connect to the server. | ||
* @return True if connected, else false | ||
*/ | ||
bool connect(); | ||
/** | ||
* @brief Closes a connection. | ||
* @brief If the socket's purpose is SocketPurpose::Server, this method will drop the connection with the client. | ||
* @brief If the socket's purpose is SocketPurpose::Client, this method will have no effect. | ||
* @return True if disconnected, else false | ||
*/ | ||
bool disconnect(); | ||
/** | ||
* @brief Receives a message. | ||
* @brief connect() must have been called first and have returned true. | ||
* @brief If the socket's purpose is SocketPurpose::Server, this method will receive a message from the client. | ||
* @brief If the socket's purpose is SocketPurpose::Client, this method will receive a message from the server. | ||
* @return The received message | ||
*/ | ||
std::string receiveMessage() const; | ||
/** | ||
* @brief Sends a message. | ||
* @brief connect() must have been called first and have returned true. | ||
* @brief If the socket's purpose is SocketPurpose::Server, this method will send a message to the client. | ||
* @brief If the socket's purpose is SocketPurpose::Client, this method will send a message to the server. | ||
* @param message The message to send | ||
* @return True if message sent successfully, else false | ||
*/ | ||
bool sendMessage(const std::string& message) const; | ||
|
||
private: | ||
SocketPurpose m_purpose; | ||
SocketType m_type; | ||
AddressFamily m_family; | ||
std::string m_address; | ||
int m_port; | ||
#ifdef _WIN32 | ||
SOCKET m_socket; | ||
SOCKET m_child; | ||
HANDLE m_pipe; | ||
#else | ||
int m_socket; | ||
int m_child; | ||
#endif | ||
}; | ||
} | ||
|
||
#endif //SOCKET_H |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef SOCKETPURPOSE_H | ||
#define SOCKETPURPOSE_H | ||
|
||
namespace Nickvision::Network | ||
{ | ||
/** | ||
* @brief Purposes of sockets. | ||
*/ | ||
enum class SocketPurpose | ||
{ | ||
Server, | ||
Client | ||
}; | ||
} | ||
|
||
#endif // SOCKETPURPOSE_H |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef SOCKETTYPE_H | ||
#define SOCKETTYPE_H | ||
|
||
#ifdef _WIN32 | ||
#include <Winsock2.h> | ||
#else | ||
#include <sys/socket.h> | ||
#endif | ||
|
||
namespace Nickvision::Network | ||
{ | ||
/** | ||
* @brief Type of sockets. | ||
*/ | ||
enum class SocketType | ||
{ | ||
Stream = SOCK_STREAM, | ||
Datagram = SOCK_DGRAM, | ||
#ifdef __linux__ | ||
SequencedPacket = SOCK_SEQPACKET | ||
#endif | ||
}; | ||
} | ||
|
||
#endif // SOCKETTYPE_H |
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 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
Oops, something went wrong.