00001
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifndef RCSC_NET_BASIC_SOCKET_H
00033 #define RCSC_NET_BASIC_SOCKET_H
00034
00035 #include <boost/scoped_ptr.hpp>
00036 #include <cstddef>
00037
00038 namespace rcsc {
00039
00040 struct AddrImpl;
00041
00046 class BasicSocket {
00047 private:
00049 int M_fd;
00050
00051 protected:
00053 boost::scoped_ptr< AddrImpl > M_dest;
00054
00055 public:
00060 enum SocketType {
00061 DATAGRAM_TYPE,
00062 STREAM_TYPE,
00063 };
00064
00065 public:
00069 virtual ~BasicSocket();
00070
00074 int fd() const
00075 {
00076 return M_fd;
00077 }
00078
00079 protected:
00084 BasicSocket();
00085
00090 void setDestPort( const void * addr );
00091
00092
00097 bool open( const SocketType type );
00098
00104 bool bind( const int port = 0 );
00105
00112 bool setAddr( const char * hostname,
00113 const int port );
00114
00119 int setNonBlocking();
00120
00125 int connectToPresetAddr();
00126
00133 int writeToStream( const char * data,
00134 const std::size_t len );
00135
00144 int readFromStream( char * buf,
00145 const std::size_t len );
00146
00153 int sendDatagramPacket( const char * data,
00154 const std::size_t len );
00155
00166 int receiveDatagramPacket( char * buf,
00167 const std::size_t len,
00168 const bool overwrite_dist_addr = false );
00169
00170 public:
00171
00176 bool isOpen() const
00177 {
00178 return fd() != -1;
00179 }
00180
00185 int close();
00186
00187
00194 virtual
00195 int send( const char * data,
00196 const std::size_t len ) = 0;
00197
00206 virtual
00207 int receive( char * buf,
00208 const std::size_t len ) = 0;
00209 };
00210
00211 }
00212
00213 #endif