00001 // BTCPConnection.h (this is -*- C++ -*-) 00002 // 00003 // \author: Bjoern Giesler <bjoern@giesler.de> 00004 // 00005 // 00006 // 00007 // $Author: giesler $ 00008 // $Locker$ 00009 // $Revision$ 00010 // $Date: 2002-08-19 10:41:28 +0200 (Mon, 19 Aug 2002) $ 00011 00012 #ifndef BTCPCONNECTION_H 00013 #define BTCPCONNECTION_H 00014 00021 /* system includes */ 00022 #include <string> 00023 #include <deque> 00024 #include <netinet/in.h> 00025 00026 /* my includes */ 00027 /* (none) */ 00028 00034 class BTCPConnection { 00035 public: 00037 BTCPConnection(long addr, short port); 00038 00040 BTCPConnection(const std::string& host, short port); 00041 00043 BTCPConnection(short port); 00044 00046 BTCPConnection(); 00047 00048 virtual ~BTCPConnection(); 00049 00051 void bind(short port); 00053 short portNumber() { return portnum; } 00054 00056 virtual void handle(); 00058 virtual void close(); 00059 00061 bool connected() { return conn != -1; } 00062 00064 int writeRaw(const unsigned char* buf, int buflen); 00066 bool write(const unsigned char* buf, int buflen = -1); 00068 bool write(const char* buf, int buflen = -1) 00069 { 00070 return write((const unsigned char*)buf, buflen); 00071 } 00073 int read(char* buf, int buflen); 00074 00076 bool makeBlocking(); 00078 bool makeNonblocking(); 00079 00080 protected: 00082 virtual void handleClose(); 00084 virtual bool handleConnection(); 00086 virtual bool handleInput(const unsigned char* buf, int bufleno); 00088 virtual bool handleAccept(); 00089 00091 unsigned int readBlockSize(); 00093 void setReadBlockSize(unsigned int rbs); 00094 00096 short portnum; 00098 int sock; 00100 int conn; 00102 bool doNonblocking; 00104 bool isServer; 00105 00107 unsigned int rbs; 00109 unsigned char* buf; 00110 00112 struct sockaddr_in lastaddr; 00113 00114 private: 00115 typedef struct { 00116 int socket; 00117 unsigned int timeout; 00118 } TimedSocketCloser; 00119 static std::deque<TimedSocketCloser> socketsToClose; 00120 static void reapSockets(); 00121 }; 00122 00123 #endif /* BTCPCONNECTION_H */