aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
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, ...);
+ };
+}