ManagedSocket.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #ifndef ADCHPP_MANAGEDSOCKET_H
00020 #define ADCHPP_MANAGEDSOCKET_H
00021 
00022 #include "common.h"
00023 
00024 #include "forward.h"
00025 #include "Signal.h"
00026 #include "Util.h"
00027 #include "Buffer.h"
00028 #include "AsyncStream.h"
00029 
00030 namespace adchpp {
00031 
00035 class ManagedSocket : private boost::noncopyable, public enable_shared_from_this<ManagedSocket> {
00036 public:
00037     ManagedSocket(const AsyncStreamPtr& sock_) : sock(sock_), overflow(0), disc(0), maxBufferSize(getDefaultMaxBufferSize()), lastWrite(0) { }
00038 
00040     ADCHPP_DLL void write(const BufferPtr& buf, bool lowPrio = false) throw();
00041 
00043     ADCHPP_DLL size_t getQueuedBytes() const;
00044 
00046     ADCHPP_DLL void disconnect(size_t timeout, Util::Reason reason) throw();
00047 
00048     const std::string& getIp() const { return ip; }
00049     void setIp(const std::string& ip_) { ip = ip_; }
00050 
00051     typedef std::function<void()> ConnectedHandler;
00052     void setConnectedHandler(const ConnectedHandler& handler) { connectedHandler = handler; }
00053     typedef std::function<void(const BufferPtr&)> DataHandler;
00054     void setDataHandler(const DataHandler& handler) { dataHandler = handler; }
00055     typedef std::function<void(const boost::system::error_code&)> FailedHandler;
00056     void setFailedHandler(const FailedHandler& handler) { failedHandler = handler; }
00057 
00058     void setMaxBufferSize(size_t newSize) { maxBufferSize = newSize; }
00059     size_t getMaxBufferSize() { return maxBufferSize; }
00060 
00061     time_t getOverflow() { return overflow; }
00062 
00063     time_t getLastWrite() { return lastWrite; }
00064 
00065     static void setDefaultMaxBufferSize(size_t newSize) { defaultMaxBufferSize = newSize; }
00066     static size_t getDefaultMaxBufferSize() { return defaultMaxBufferSize; }
00067 
00068     static time_t getOverflowTimeout() { return overflowTimeout; }
00069     ~ManagedSocket() throw();
00070 
00071 private:
00072     static size_t defaultMaxBufferSize;
00073     static time_t overflowTimeout;
00074 
00075     friend class SocketManager;
00076     friend class SocketFactory;
00077 
00078     void completeAccept(const boost::system::error_code&) throw();
00079     void prepareWrite() throw();
00080     void completeWrite(const boost::system::error_code& ec, size_t bytes) throw();
00081     void prepareRead() throw();
00082     void prepareRead2(const boost::system::error_code& ec, size_t bytes) throw();
00083     void completeRead(const boost::system::error_code& ec, size_t bytes) throw();
00084 
00085     void failSocket(const boost::system::error_code& error) throw();
00086 
00087     AsyncStreamPtr sock;
00088 
00090     BufferList outBuf;
00091 
00093     BufferPtr inBuf;
00094 
00096     time_t overflow;
00098     uint32_t disc;
00099 
00101     size_t maxBufferSize;
00102 
00104     time_t lastWrite;
00105 
00106     std::string ip;
00107 
00108     ConnectedHandler connectedHandler;
00109     DataHandler dataHandler;
00110     FailedHandler failedHandler;
00111 };
00112 
00113 }
00114 
00115 #endif // MANAGEDSOCKET_H
Generated on Sat Nov 27 23:37:53 2010 for adchpp by  doxygen 1.6.3