Advertisement
Guest User

TCPConnection.h

a guest
Dec 26th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. // Copyright (c) 2010 Martin Knafve / hMailServer.com.  
  2. // http://www.hmailserver.com
  3.  
  4. #pragma once
  5.  
  6. #include "../Util/Event.h"
  7.  
  8. #include "IOOperationQueue.h"
  9. using boost::asio::ip::tcp;
  10.  
  11. typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
  12.  
  13. namespace HM
  14. {
  15.    class ProtocolParser;
  16.    class ByteBuffer;
  17.    class SecurityRange;
  18.  
  19.    class TCPConnection :
  20.       public boost::enable_shared_from_this<TCPConnection>
  21.    {
  22.    public:
  23.       TCPConnection(bool useSSL,
  24.                     boost::asio::io_service& io_service,    
  25.                     boost::asio::ssl::context& context);
  26.       ~TCPConnection(void);
  27.  
  28.       enum ShutdownOption
  29.       {
  30.          ShutdownSend,
  31.          ShutdownReceive,
  32.       };
  33.  
  34.       enum Consts
  35.       {
  36.          BufferSize = 60000
  37.       };
  38.  
  39.       ssl_socket::lowest_layer_type& GetSocket();
  40.  
  41.       int GetBufferSize() {return BufferSize; }
  42.       bool Connect(const AnsiString &remoteServer, long remotePort, const IPAddress &localAddress);
  43.       void Start(shared_ptr<ProtocolParser> protocolParser);
  44.       void SetReceiveBinary(bool binary);
  45.  
  46.       void PostWrite(const AnsiString &sData);
  47.       void PostWrite(shared_ptr<ByteBuffer> pByteBuffer);
  48.       void PostRead(const AnsiString &delimitor);
  49.  
  50.       void PostShutdown(ShutdownOption what);
  51.       void PostDisconnect();
  52.       void PostTimeout();
  53.      
  54.       IPAddress GetRemoteEndpointAddress();
  55.       unsigned long GetRemoteEndpointPort();
  56.       unsigned long GetLocalEndpointPort();
  57.  
  58.       void UpdateLogoutTimer();
  59.       void CancelLogoutTimer();
  60.       void StartTLS();
  61.  
  62.       static bool PrepareSSLContext(boost::asio::ssl::context &ctx);
  63.  
  64.       void SetSecurityRange(shared_ptr<SecurityRange> securityRange);
  65.       shared_ptr<SecurityRange> GetSecurityRange();
  66.  
  67.       shared_ptr<TCPConnection> GetSharedFromThis();
  68.  
  69.       Event GetConnectionTerminationEvent() {return _connectionTermination;}
  70.  
  71.       int GetSessionID();
  72.  
  73.       bool ReportReadErrors(bool newValue);
  74.       bool startTLS;
  75.    private:
  76.      
  77.       void _StartAsyncConnect(tcp::resolver::iterator endpoint_iterator);
  78.  
  79.       static void OnTimeout(boost::weak_ptr<TCPConnection> connection, boost::system::error_code const& err);
  80.  
  81.       String SafeGetIPAddress();
  82.  
  83.       bool IsClient();
  84.  
  85.       void _ProcessOperationQueue();
  86.  
  87.       void Disconnect();
  88.       void Shutdown(boost::asio::socket_base::shutdown_type, bool removeFromQueue);
  89.       void Write(shared_ptr<ByteBuffer> buffer);
  90.       void Read(const AnsiString &delimitor);
  91.  
  92.       void HandleResolve(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator);
  93.       void HandleConnect(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator);
  94.       void HandleHandshake(const boost::system::error_code& error);
  95.       void HandleRead(const boost::system::error_code& /*error*/,  size_t bytes_transferred);
  96.       void HandleWrite(const boost::system::error_code& /*error*/,  size_t bytes_transferred);
  97.  
  98.       void ReportDebugMessage(const String &message, const boost::system::error_code &error);
  99.       void ReportError(ErrorManager::eSeverity sev, int code, const String &context, const String &message, const boost::system::system_error &error);
  100.       void ReportError(ErrorManager::eSeverity sev, int code, const String &context, const String &message);
  101.       static void ReportInitError(ErrorManager::eSeverity sev, int code, const String &context, const String &message, const boost::system::system_error &error);
  102.  
  103.       boost::asio::ip::tcp::socket _socket;
  104.       ssl_socket _sslSocket;
  105.  
  106.       boost::asio::ip::tcp::resolver _resolver;
  107.       boost::asio::deadline_timer _timer;
  108.       boost::asio::streambuf _receiveBuffer;
  109.       shared_ptr<ProtocolParser> _protocolParser;
  110.      
  111.       IOOperationQueue _operationQueue;
  112.  
  113.       bool _receiveBinary;
  114.       bool _useSSL;
  115.       long _remotePort;
  116.       bool _hasTimeout;
  117.       String _remoteServer;
  118.       Event _connectionTermination;
  119.  
  120.       shared_ptr<SecurityRange> _securityRange;
  121.  
  122.       bool _handshakeDone;
  123.    };
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement