summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Network/BoostUDPSocket.h')
-rw-r--r--Swiften/Network/BoostUDPSocket.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/Swiften/Network/BoostUDPSocket.h b/Swiften/Network/BoostUDPSocket.h
new file mode 100644
index 0000000..5cc8b1d
--- /dev/null
+++ b/Swiften/Network/BoostUDPSocket.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2012 Yoann Blein
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Network/UDPSocket.h>
+#include <Swiften/EventLoop/EventOwner.h>
+//#include <Swiften/Base/SafeByteArray.h>
+
+#include <boost/asio/io_service.hpp>
+#include <boost/asio/ip/udp.hpp>
+#include <boost/enable_shared_from_this.hpp>
+#include <boost/thread/mutex.hpp>
+
+namespace boost {
+ class thread;
+ namespace system {
+ class error_code;
+ }
+}
+
+namespace Swift {
+ class EventLoop;
+ class HostAddressPort;
+
+ class BoostUDPSocket : public UDPSocket, public EventOwner, public boost::enable_shared_from_this<BoostUDPSocket> {
+ public:
+ typedef boost::shared_ptr<BoostUDPSocket> ref;
+
+ virtual ~BoostUDPSocket();
+
+ static ref create(boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop) {
+ return ref(new BoostUDPSocket(ioService, eventLoop));
+ }
+
+ virtual void bind(int port);
+ virtual void listen();
+ virtual void connect(const HostAddressPort& address);
+ virtual void connectToFirstIncoming();
+ virtual void send(const SafeByteArray& data);
+ virtual void close();
+
+ boost::asio::ip::udp::socket& getSocket() {
+ return socket_;
+ }
+
+ private:
+ BoostUDPSocket(boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop);
+
+ void handleSocketRead(const boost::system::error_code& error, size_t bytesTransferred);
+ void handleFirstRead(const boost::system::error_code& error);
+ void handleDataWritten(const boost::system::error_code& error);
+ void doRead();
+ void doSend(const SafeByteArray& data);
+
+ private:
+ EventLoop* eventLoop;
+ boost::shared_ptr<boost::asio::io_service> ioService;
+ boost::asio::ip::udp::socket socket_;
+ boost::shared_ptr<SafeByteArray> readBuffer_;
+ boost::mutex sendMutex_;
+ bool sending_;
+ SafeByteArray sendQueue_;
+ boost::asio::ip::udp::endpoint remoteEndpoint_;
+
+ };
+}