aboutsummaryrefslogtreecommitdiff
path: root/src/DirectConnection.cpp
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-06-08 04:16:48 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-06-08 04:16:50 +0200
commite36509911c34a376c04498d47016002b1badbead (patch)
tree3c386074c82ce8cf6b7e2835ded40db12a5b013f /src/DirectConnection.cpp
parentfa911f5dd666eb01eabf898f071ca57933bc110f (diff)
Make connect asynchronous
Diffstat (limited to 'src/DirectConnection.cpp')
-rw-r--r--src/DirectConnection.cpp64
1 files changed, 39 insertions, 25 deletions
diff --git a/src/DirectConnection.cpp b/src/DirectConnection.cpp
index 748a936..d10c990 100644
--- a/src/DirectConnection.cpp
+++ b/src/DirectConnection.cpp
@@ -47,7 +47,7 @@ namespace sibs
{
std::string errMsg = "UDT: Failed to create socket, error: ";
errMsg += UDT::getlasterror_desc();
- throw ConnectionException(errMsg);
+ throw SocketCreateException(errMsg);
}
UDT::setsockopt(socket, 0, UDT_RENDEZVOUS, &rendezvous, sizeof(bool));
@@ -64,43 +64,57 @@ namespace sibs
{
std::string errMsg = "UDT: Failed to bind, error: ";
errMsg += UDT::getlasterror_desc();
- throw ConnectionException(errMsg);
+ throw SocketCreateException(errMsg);
}
return socket;
}
- std::shared_ptr<DirectConnectionPeer> DirectConnections::connectServer(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc)
+ void DirectConnections::connectServer(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc)
{
- return connect(address, false, true, receiveDataCallbackFunc);
+ connect(address, false, true, connectCallbackFunc, receiveDataCallbackFunc);
}
- std::shared_ptr<DirectConnectionPeer> DirectConnections::connect(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc)
+ void DirectConnections::connect(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc)
{
- return connect(address, true, true, receiveDataCallbackFunc);
+ connect(address, true, true, connectCallbackFunc, receiveDataCallbackFunc);
}
- std::shared_ptr<DirectConnectionPeer> DirectConnections::connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubReceiveDataCallback receiveDataCallbackFunc)
+ void DirectConnections::connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc)
{
- UDTSOCKET socket = createSocket(Ipv4(nullptr, port), rendezvous, reuseAddr);
-
- if(UDT::connect(socket, (sockaddr*)&address.address, sizeof(address.address)) == UDT::ERROR)
+ std::thread([this, address, rendezvous, reuseAddr, connectCallbackFunc, receiveDataCallbackFunc]()
{
- UDT::close(socket);
- std::string errMsg = "UDT: Failed to connect, error: ";
- errMsg += UDT::getlasterror_desc();
- throw ConnectionException(errMsg);
- }
-
- UDT::epoll_add_usock(eid, socket);
- std::shared_ptr<DirectConnectionPeer> peer = std::make_shared<DirectConnectionPeer>();
- peer->socket = socket;
- peer->address = address;
- peer->receiveDataCallbackFunc = receiveDataCallbackFunc;
- peersMutex.lock();
- peers[socket] = peer;
- peersMutex.unlock();
- return peer;
+ std::shared_ptr<DirectConnectionPeer> peer = std::make_shared<DirectConnectionPeer>();
+ UDTSOCKET socket;
+ try
+ {
+ socket = createSocket(Ipv4(nullptr, port), rendezvous, reuseAddr);
+ }
+ catch(SocketCreateException &e)
+ {
+ if(connectCallbackFunc)
+ connectCallbackFunc(peer, PubSubResult::ERROR, e.what());
+ return;
+ }
+
+ if(UDT::connect(socket, (sockaddr*)&address.address, sizeof(address.address)) == UDT::ERROR)
+ {
+ if(connectCallbackFunc)
+ connectCallbackFunc(peer, PubSubResult::ERROR, UDT::getlasterror_desc());
+ return;
+ }
+
+ UDT::epoll_add_usock(eid, socket);
+ peer->socket = socket;
+ peer->address = address;
+ peer->receiveDataCallbackFunc = receiveDataCallbackFunc;
+ peersMutex.lock();
+ peers[socket] = peer;
+ peersMutex.unlock();
+
+ if(connectCallbackFunc)
+ connectCallbackFunc(peer, PubSubResult::OK, "");
+ }).detach();
}
void DirectConnections::send(const std::shared_ptr<DirectConnectionPeer> &peer, std::shared_ptr<std::vector<u8>> data, PubSubSendDataCallback sendDataCallbackFunc)