File: socket.hpp

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (47 lines) | stat: -rw-r--r-- 1,101 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once

#include <poll.h>

#include "common.hpp"

POLYBAR_NS

namespace socket_util {
  class unix_connection {
   public:
    explicit unix_connection(string&& path);

    ~unix_connection() noexcept;

    int disconnect();

    ssize_t send(const void* data, size_t len, int flags = 0);
    ssize_t send(const string& data, int flags = 0);

    string receive(const ssize_t receive_bytes, int flags = 0);
    string receive(const ssize_t receive_bytes, ssize_t* bytes_received, int flags = 0);

    bool peek(const size_t peek_bytes);
    bool poll(short int events = POLLIN, int timeout_ms = -1);

   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
   */
  inline unique_ptr<unix_connection> make_unix_connection(string&& path) {
    return std::make_unique<unix_connection>(forward<string>(path));
  }
}  // namespace socket_util

POLYBAR_NS_END