/* * Copyright (c) 2012 Yoann Blein * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #pragma once #include #include //#include #include #include #include #include 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 { public: typedef boost::shared_ptr ref; virtual ~BoostUDPSocket(); static ref create(boost::shared_ptr 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 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 ioService; boost::asio::ip::udp::socket socket_; boost::shared_ptr readBuffer_; boost::mutex sendMutex_; bool sending_; SafeByteArray sendQueue_; boost::asio::ip::udp::endpoint remoteEndpoint_; }; }