aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-06-02 01:39:25 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-06-02 01:39:29 +0200
commitad7cc4dbe4dacaa5cccfd1c9adeccc0ece2299f4 (patch)
treeb045f3bc6068fb797203953d661bad5fcc9c509f /include
parent595b1b7d2fc7aa084296978b8a38a26ed960a0f5 (diff)
Receive data with epoll_wait
Diffstat (limited to 'include')
-rw-r--r--include/DirectConnection.hpp44
-rw-r--r--include/Log.hpp12
2 files changed, 51 insertions, 5 deletions
diff --git a/include/DirectConnection.hpp b/include/DirectConnection.hpp
index c11871f..4075d12 100644
--- a/include/DirectConnection.hpp
+++ b/include/DirectConnection.hpp
@@ -1,6 +1,12 @@
#pragma once
#include <stdexcept>
+#include <unordered_map>
+#include <functional>
+#include <string>
+#include <memory>
+#include <thread>
+#include <mutex>
#include "types.hpp"
#include "utils.hpp"
@@ -20,6 +26,12 @@ namespace sibs
ConnectionException(const std::string &errMsg) : std::runtime_error(errMsg) {}
};
+ class SendException : public std::runtime_error
+ {
+ public:
+ SendException(const std::string &errMsg) : std::runtime_error(errMsg) {}
+ };
+
class Ipv4
{
DISABLE_COPY(Ipv4)
@@ -31,19 +43,41 @@ namespace sibs
struct addrinfo *address;
};
+ enum class PubSubConnectResult
+ {
+ OK,
+ ERROR
+ };
+
+ using PubSubConnectCallback = std::function<void(PubSubConnectResult result, const std::string &resultStr)>;
+ using PubSubReceiveDataCallback = std::function<void(const void *data, const usize size)>;
+
+ struct DirectConnectionPeer
+ {
+ int socket;
+ PubSubReceiveDataCallback receiveDataCallbackFunc;
+ };
+
class DirectConnections
{
DISABLE_COPY(DirectConnections)
public:
- DirectConnections();
+ DirectConnections(u16 port = 27137);
~DirectConnections();
- void connect(const Ipv4 &address);
+ // Throws ConnectionException on error
+ std::shared_ptr<DirectConnectionPeer> connect(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc);
+ // Throws SendException on error
+ void send(const std::shared_ptr<DirectConnectionPeer> &peer, const void *data, const usize size);
private:
- void init();
- void cleanup();
+ void receiveData();
+ bool receiveDataFromPeer(const int socket, char *output);
private:
+ u16 port;
int eid;
- int mySocket;
+ std::unordered_map<int, std::shared_ptr<DirectConnectionPeer>> peers;
+ std::thread receiveDataThread;
+ std::mutex peersMutex;
+ bool alive;
};
}
diff --git a/include/Log.hpp b/include/Log.hpp
new file mode 100644
index 0000000..1301c5b
--- /dev/null
+++ b/include/Log.hpp
@@ -0,0 +1,12 @@
+#pragma once
+
+namespace sibs
+{
+ class Log
+ {
+ public:
+ static void debug(const char *fmt, ...);
+ static void warn(const char *fmt, ...);
+ static void error(const char *fmt, ...);
+ };
+}