summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordknn <yoann.blein@free.fr>2012-07-08 20:23:27 (GMT)
committerdknn <yoann.blein@free.fr>2012-09-22 08:55:56 (GMT)
commitdc259819a687fa9a6f9007ed15b443f737c5d473 (patch)
treeb3322e7902705923cfe85d5a67e4bfda9697f3c3 /Swiften/Network/BoostUDPSocket.h
parent9928de47c73430b4ed37db8a0cfb44adecc3e296 (diff)
downloadswift-contrib-dc259819a687fa9a6f9007ed15b443f737c5d473.zip
swift-contrib-dc259819a687fa9a6f9007ed15b443f737c5d473.tar.bz2
Udp stuff
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_;
+
+ };
+}