src/Socket.h
branchv_0
changeset 3 2b57c8683ffe
parent 2 2665ab0bcf44
--- a/src/Socket.h	Sun Aug 07 10:45:05 2022 +0200
+++ b/src/Socket.h	Sun Aug 21 00:16:50 2022 +0200
@@ -17,6 +17,7 @@
 #pragma once
 
 #include <string>
+#include <vector>
 #include <cstring>
 #include <unistd.h>
 #include <stdexcept>
@@ -29,55 +30,92 @@
 namespace in {
 namespace socket {
 
-class Socket {
-private:
-	const static size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
-	int s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+namespace options {
+static const char OPTION_PROTOCOL[] = "protocol";
+static const char OPTION_ROLE[] = "role";
+static const char OPTION_MODE[] = "mode";
+static const char OPTION_HOST[] = "host";
+static const char OPTION_PORT[] = "port";
+static const char OPTION_PATH[] = "path";
+static const char OPTION_DELAY[] = "delay";
+
+static const char PROTOCOL_TCP[] = "tcp";
+static const char PROTOCOL_UDP[] = "udp";
+static const char PROTOCOL_UDS[] = "uds";
+static const char PROTOCOL_SCTP[] = "sctp";
+
+static const char ROLE_CLIENT[] = "client";
+static const char ROLE_SERVER[] = "server";
 
+static const char MODE_STREAM[] = "stream";
+static const char MODE_DATAGRAM[] = "datagram";
+}
+
+class Message {
+public:
+	std::string data;
+
+	Message() {
+	}
+
+	Message(const std::string& data) : data(data) {
+	}
+
+	virtual ~Message() = default;
+
+
+};
+
+class IncomingMessage : public Message {
 public:
 
-	Socket() {
-		s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
-
-		struct sockaddr_in a;
-		memset((char *) &a, 0, sizeof (a));
-		a.sin_family = AF_INET;
-		a.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
-		a.sin_port = htons(1234);
-		::bind(s, (sockaddr*) & a, sizeof (a));
-
-		// int soBufferSize = 1024 * 1024;
-		// setsockopt(s, SOL_SOCKET, SO_RCVBUF, &soBufferSize, sizeof (soBufferSize));
-		// soBufferSize = 0;
-		// socklen_t soBufferSizeLength = sizeof (soBufferSize);
-
-		// std::cerr << "soBufferSize=" << soBufferSize << std::endl;
-		// getsockopt(s, SOL_SOCKET, SO_RCVBUF, &soBufferSize, &soBufferSizeLength);
-		// std::cerr << "soBufferSize=" << soBufferSize << " length=" << soBufferSizeLength << std::endl;
+	IncomingMessage(const std::string& data) : Message(data) {
 	}
 
-	virtual ~Socket() {
-		close(s);
-	}
-
-	std::string receive() {
-		char buffer[MSG_SIZE + 1];
-		memset(buffer, 0, MSG_SIZE + 1);
+	std::string remoteHost;
+	in_port_t remotePort = 0;
+	pid_t remotePID = 0;
+	uid_t remoteUID = 0;
+	gid_t remoteGID = 0;
+};
 
-		struct sockaddr_in remoteAddress;
-		memset((char *) &remoteAddress, 0, sizeof (remoteAddress));
-		socklen_t remoteAddressSize = sizeof (remoteAddress);
+class OutgoingMessage : public Message {
+public:
 
-		ssize_t msgSize = recvfrom(s, buffer, sizeof (buffer), 0, (sockaddr*) & remoteAddress, &remoteAddressSize);
-
-
-		if (msgSize > sizeof (buffer))throw std::logic_error("Invalid Socket message size.");
-		else if (msgSize >= 0) return std::string(buffer, msgSize);
-		else throw std::logic_error("Unable to receive Socket message the socket; error: " + std::string(strerror(errno)));
+	OutgoingMessage(const std::string& data) : Message(data) {
 	}
 
 };
 
+class Socket {
+public:
+	virtual ~Socket() = default;
+	virtual void send(const OutgoingMessage& message) = 0;
+	virtual const IncomingMessage receive() = 0;
+};
+
+class SocketOption {
+public:
+	const std::string name;
+	const std::string value;
+
+	SocketOption(const std::string name, const std::string value) : name(name), value(value) {
+	}
+
+	virtual ~SocketOption() = default;
+};
+
+using SocketOptions = std::vector<SocketOption>;
+
+class SocketFactory {
+public:
+	virtual ~SocketFactory() = default;
+	virtual bool canHandle(const SocketOptions& options) = 0;
+	virtual std::shared_ptr<Socket> open(const SocketOptions& options) = 0;
+	static std::shared_ptr<SocketFactory> find(const SocketOptions& options);
+};
+
+
 }
 }
 }