00001 #ifndef TCPCONNECTION_H_
00002 #define TCPCONNECTION_H_
00003
00004 #include "data/Bundle.h"
00005 #include "core/ConvergenceLayer.h"
00006 #include "core/TCPConvergenceLayer.h"
00007 #include "utils/MutexLock.h"
00008 #include "utils/Mutex.h"
00009 #include "utils/Conditional.h"
00010
00011 using namespace dtn::data;
00012 using namespace std;
00013
00014
00015 namespace dtn
00016 {
00017 namespace core
00018 {
00019 enum TCPConnectionState
00020 {
00021 STATE_INITIAL = 0,
00022 STATE_HANDSHAKE = 1,
00023 STATE_IDLE = 2,
00024 STATE_MESSAGE = 3,
00025 STATE_TRANSMITTING = 4,
00026 STATE_TRANSFER_COMPLETE = 5,
00027 STATE_CLOSED = 6
00028 };
00029
00030 struct TCPConnectionData
00031 {
00032 unsigned int payloadsize;
00033 char* data;
00034 unsigned int length;
00035 char* position;
00036 unsigned int remain;
00037 Mutex mutex;
00038 bool last;
00039 unsigned int idletime;
00040 u_int16_t keepalive;
00041 unsigned int ackdata;
00042
00043 TCPConnectionState state;
00044 Conditional state_cond;
00045 Mutex mutex_state;
00046 bool outbound;
00047 };
00048
00052 class TCPConnection
00053 {
00054 friend class TCPConvergenceLayer;
00055
00056 public:
00063 TCPConnection(TCPConvergenceLayer &clayer, int socket, size_t chunksize = 128);
00064
00068 ~TCPConnection();
00069
00070 string getRemoteURI();
00071
00075 bool isClosed();
00076
00077 TCPConnectionState getState();
00078
00079 bool waitForState(TCPConnectionState state);
00080
00081 protected:
00085 unsigned int transmit(char *data, unsigned int size);
00086
00090 bool datawaiting();
00091
00095 void transmit();
00096
00100 void receive();
00101
00102 int getSocket();
00103
00109 void handshake(unsigned char* data, unsigned int size);
00110
00114 void idlechecking();
00115
00116 private:
00117 void disconnect();
00118 void setState(TCPConnectionState state);
00119
00120 void setState(TCPConnectionData &data, TCPConnectionState state);
00121 TCPConnectionState getState(TCPConnectionData &data);
00122 bool waitForState(TCPConnectionData &data, TCPConnectionState state);
00123
00124 int m_socket;
00125 TCPConvergenceLayer &m_clayer;
00126 string m_eid;
00127
00128 TCPConnectionData m_incoming;
00129 TCPConnectionData m_outgoing;
00130
00131 Mutex m_mutex_state;
00132 TCPConnectionState m_state;
00133 Conditional m_cond_state;
00134
00135 size_t m_chunksize;
00136 };
00137 }
00138 }
00139
00140 #endif