2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
2016-11-12 09:36:22 -05:00
|
|
|
#include <poll.h>
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
#include "common.hpp"
|
2016-12-09 03:02:47 -05:00
|
|
|
#include "utils/factory.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
namespace socket_util {
|
|
|
|
class unix_connection {
|
|
|
|
public:
|
2016-11-02 15:22:45 -04:00
|
|
|
explicit unix_connection(string&& path);
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
~unix_connection() noexcept;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
int disconnect();
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
ssize_t send(const void* data, size_t len, int flags = 0);
|
2016-11-25 07:55:15 -05:00
|
|
|
ssize_t send(const string& data, int flags = 0);
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-14 01:45:29 -05:00
|
|
|
string receive(const ssize_t receive_bytes, int flags = 0);
|
2016-12-14 01:13:07 -05:00
|
|
|
string receive(const ssize_t receive_bytes, ssize_t* bytes_received, int flags = 0);
|
|
|
|
|
|
|
|
bool peek(const size_t peek_bytes);
|
2016-11-02 15:22:45 -04:00
|
|
|
bool poll(short int events = POLLIN, int timeout_ms = -1);
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
int m_fd = -1;
|
|
|
|
string m_socketpath;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a wrapper for a unix socket connection
|
|
|
|
*
|
|
|
|
* Example usage:
|
2018-11-04 10:08:54 -05:00
|
|
|
* \code cpp
|
2016-06-14 23:32:35 -04:00
|
|
|
* auto conn = socket_util::make_unix_connection("/tmp/socket");
|
|
|
|
* conn->send(...);
|
|
|
|
* conn->receive(...);
|
2018-11-04 10:08:54 -05:00
|
|
|
* \endcode
|
2016-06-14 23:32:35 -04:00
|
|
|
*/
|
2020-05-14 16:13:43 -04:00
|
|
|
inline unique_ptr<unix_connection> make_unix_connection(string&& path) {
|
2016-12-09 03:02:47 -05:00
|
|
|
return factory_util::unique<unix_connection>(forward<string>(path));
|
2020-05-14 16:13:43 -04:00
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|