src/Socket.h
branchv_0
changeset 2 fb399fd4f053
parent 1 e3265afd1111
child 3 e701e06ff561
equal deleted inserted replaced
1:e3265afd1111 2:fb399fd4f053
    28 namespace relpipe {
    28 namespace relpipe {
    29 namespace out {
    29 namespace out {
    30 namespace socket {
    30 namespace socket {
    31 
    31 
    32 class Socket {
    32 class Socket {
    33 private:
       
    34 public:
    33 public:
       
    34 	virtual ~Socket() = default;
       
    35 	// virtual void setOption(const std::string& uri, const std::string& value) = 0;
       
    36 	virtual void send(const std::string& message) = 0;
       
    37 	virtual const std::string receive() = 0;
       
    38 };
    35 
    39 
    36 	virtual ~Socket() {
    40 class SocketFactory {
    37 	}
    41 public:
       
    42 	virtual ~SocketFactory() = default;
       
    43 	virtual bool canHandle(const std::string& connectionString) = 0;
       
    44 	// virtual void setOption(const std::string& uri, const std::string& value) = 0;
       
    45 	virtual Socket* open(const std::string& connectionString) = 0;
       
    46 	static SocketFactory* find(const std::string& connectionString);
       
    47 };
    38 
    48 
    39 	void send(const std::string& message) {
       
    40 
       
    41 		struct sockaddr_in a;
       
    42 		memset((char *) &a, 0, sizeof (a));
       
    43 		a.sin_family = AF_INET;
       
    44 		a.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
       
    45 		a.sin_port = htons(1234);
       
    46 
       
    47 		int s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
       
    48 		sendto(s, message.c_str(), message.size(), 0, (sockaddr*) & a, sizeof (a));
       
    49 		
       
    50 		close(s);
       
    51 
       
    52 		// TODO: send message
       
    53 	}
       
    54 	
       
    55 	// virtual const std::string receive();
       
    56 
       
    57 };
       
    58 
    49 
    59 }
    50 }
    60 }
    51 }
    61 }
    52 }