summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/sys/posix/AsynchIO.cpp
diff options
context:
space:
mode:
authorAndrew Stitcher <astitcher@apache.org>2008-05-09 02:00:04 +0000
committerAndrew Stitcher <astitcher@apache.org>2008-05-09 02:00:04 +0000
commit2d4d4a1425d3f4e189868d36a0cc9fbd4bec4756 (patch)
tree7413a08c12494e5c5551b3f09ad35f29804d66a3 /cpp/src/qpid/sys/posix/AsynchIO.cpp
parent266fbd3880a49ea4f6a221231027408a32033687 (diff)
downloadqpid-python-2d4d4a1425d3f4e189868d36a0cc9fbd4bec4756.tar.gz
QPID-1040: Patch from Ted Ross: Asynchronous Connector
Code to allow non-blocking connection of new sockets git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@654666 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/sys/posix/AsynchIO.cpp')
-rw-r--r--cpp/src/qpid/sys/posix/AsynchIO.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/cpp/src/qpid/sys/posix/AsynchIO.cpp b/cpp/src/qpid/sys/posix/AsynchIO.cpp
index cedad5c011..9dcb841992 100644
--- a/cpp/src/qpid/sys/posix/AsynchIO.cpp
+++ b/cpp/src/qpid/sys/posix/AsynchIO.cpp
@@ -97,6 +97,57 @@ void AsynchAcceptor::readable(DispatchHandle& h) {
}
/*
+ * Asynch Connector
+ */
+
+AsynchConnector::AsynchConnector(const Socket& s,
+ Poller::shared_ptr poller,
+ std::string hostname,
+ uint16_t port,
+ ConnectedCallback connCb,
+ FailedCallback failCb) :
+ DispatchHandle(s,
+ 0,
+ boost::bind(&AsynchConnector::connComplete, this, _1),
+ boost::bind(&AsynchConnector::connComplete, this, _1)),
+ connCallback(connCb),
+ failCallback(failCb),
+ socket(s)
+{
+ socket.setNonblocking();
+ try {
+ socket.connect(hostname, port);
+ startWatch(poller);
+ } catch(std::exception& e) {
+ failure(-1, std::string(e.what()));
+ }
+}
+
+void AsynchConnector::connComplete(DispatchHandle& h)
+{
+ int errCode = socket.getError();
+
+ h.stopWatch();
+ if (errCode == 0) {
+ connCallback(socket);
+ DispatchHandle::doDelete();
+ } else {
+ failure(errCode, std::string(strerror(errCode)));
+ }
+}
+
+void AsynchConnector::failure(int errCode, std::string message)
+{
+ if (failCallback)
+ failCallback(errCode, message);
+
+ socket.close();
+ delete &socket;
+
+ DispatchHandle::doDelete();
+}
+
+/*
* Asynch reader/writer
*/
AsynchIO::AsynchIO(const Socket& s,