polybar-dwm/include/utils/socket.hpp

44 lines
978 B
C++
Raw Normal View History

2016-06-15 03:32:35 +00:00
#pragma once
#include <poll.h>
2016-06-15 03:32:35 +00:00
#include "common.hpp"
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-06-15 03:32:35 +00:00
namespace socket_util {
class unix_connection {
public:
2016-11-02 19:22:45 +00:00
explicit unix_connection(string&& path);
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
~unix_connection() noexcept;
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
int disconnect();
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
ssize_t send(const void* data, size_t len, int flags = 0);
2016-11-25 12:55:15 +00:00
ssize_t send(const string& data, int flags = 0);
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
string receive(const ssize_t receive_bytes, ssize_t& bytes_received_addr, int flags = 0);
bool poll(short int events = POLLIN, int timeout_ms = -1);
2016-06-15 03:32:35 +00:00
protected:
int m_fd = -1;
string m_socketpath;
};
/**
* Creates a wrapper for a unix socket connection
*
* Example usage:
* @code cpp
* auto conn = socket_util::make_unix_connection("/tmp/socket");
* conn->send(...);
* conn->receive(...);
* @endcode
*/
2016-11-25 07:42:31 +00:00
auto make_unix_connection = [](
string&& path) -> unique_ptr<unix_connection> { return make_unique<unix_connection>(forward<string>(path)); };
2016-06-15 03:32:35 +00:00
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END