00001 #ifndef __TCPCONNECTION_H 00002 #define __TCPCONNECTION_H 00003 00004 #include <vector> 00005 #include <iostream> 00006 00007 #ifdef LINUX 00008 #include <netinet/in.h> 00009 #include <sys/time.h> 00010 #endif 00011 00012 #ifdef WIN32 00013 #include <winsock2.h> 00014 #include <ws2tcpip.h> 00015 #endif 00016 00017 #include "os_spec.h" 00018 #include "BasicObject.h" 00019 #include "ListenerInterface.h" 00020 #include "ObservableInterface.h" 00021 #include "ConnectionInterface.h" 00022 #include "SocketAddress.h" 00023 #include "IpAddress.h" 00024 00025 class TcpConnection : public BasicObject, public ConnectionInterface { 00026 public: 00027 TcpConnection(); 00028 TcpConnection(SocketAddress); 00029 TcpConnection(IpAddress ipAddr, unsigned short port); 00030 TcpConnection(unsigned short port); 00031 00032 // these functions fulfill the ConnectionInterface contract 00033 virtual ~TcpConnection(); 00034 virtual SocketAddress* getSocketAddress(); 00035 virtual void setSocketAddress(SocketAddress*); 00036 virtual int getStream(); 00037 virtual void setStream(int stream); 00038 virtual int connect(); 00039 virtual void close(); 00040 virtual int listen(); 00041 virtual ConnectionInterface* accept(); 00042 virtual bool isConnected(); 00043 virtual bool isConnectedTo(IpAddress); 00044 virtual int read(unsigned char* buffer, int bufferSize, int amountToRead = 0); 00045 virtual int write(unsigned char* buffer, int amountToWrite); 00046 virtual ConnectionInterface::ObjectType getConnectionType(); 00047 00048 // fulfills the BasicObject contract 00049 virtual void toStream(std::ostream& out); 00050 00051 private: 00052 static OS_SPEC_SOCKET_TYPE createListeningSocket(SocketAddress socketAddress); 00053 void setSocketOptions(); 00054 00056 SocketAddress m_socketAddress; 00057 00059 OS_SPEC_SOCKET_TYPE m_stream; 00060 00062 bool m_isActive; 00063 }; 00064 00065 #endif