00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _psocket_h_
00021 #define _psocket_h_
00022
00023 #ifdef WIN32
00024 #include <winsock2.h>
00025 #else
00026 #include <sys/types.h>
00027 #include <sys/socket.h>
00028 #endif
00029
00030 #include <pclasses/pexport.h>
00031 #include <pclasses/ptypes.h>
00032 #include <pclasses/piodevice.h>
00033 #include <pclasses/pnetaddr.h>
00034 #include <string>
00035
00040 namespace P {
00041
00042 typedef uint16_t port_t;
00043
00045
00049 class PNET_EXPORT Socket: public IODevice {
00050 public:
00052
00062 Socket(int domain, int type, int proto) throw(IOError);
00063
00065 Socket(io_handle_t handle, int domain, int type, int proto) throw(IOError);
00066
00067 ~Socket() throw();
00068
00070 void close() throw(IOError);
00071
00072 size_t write(const char* buffer, size_t count) throw(IOError);
00073
00074 size_t peek(char* buffer, size_t count) throw(IOError);
00075
00076 size_t read(char* buffer, size_t count) throw(IOError);
00077
00078 inline bool isSeekable() const throw()
00079 { return false; }
00080
00082 inline int domain() const throw()
00083 { return m_domain; }
00084
00086 inline int type() const throw()
00087 { return m_type; }
00088
00090 inline int protocol() const throw()
00091 { return m_protocol; }
00092
00094 enum wait_t {
00095 pendingInput = 0x01,
00096 pendingOutput = 0x02
00097 };
00098
00100
00106 int wait(int wait, unsigned int timeout) throw(IOError);
00107
00108 void setBroadcast(bool enable) throw(IOError);
00109
00110 void setRouting(bool enable) throw(IOError);
00111
00113 void bind(const NetworkAddress& addr, port_t port) throw(IOError);
00114
00116 void getName(NetworkAddress& addr, port_t& port) const throw(LogicError,IOError);
00117
00119 void getPeer(NetworkAddress& addr, port_t& port) const throw(LogicError,IOError);
00120
00121 private:
00122 int m_domain;
00123 int m_type;
00124 int m_protocol;
00125 };
00126
00128
00132 class PNET_EXPORT StreamSocketServer: protected Socket {
00133 public:
00134 friend class StreamSocket;
00135
00137
00145 StreamSocketServer(int domain) throw(IOError);
00146
00148
00155 StreamSocketServer(const NetworkAddress& addr, port_t port) throw(IOError);
00156
00158 ~StreamSocketServer() throw();
00159
00161
00167 void listen() throw(IOError);
00168
00170
00175 bool waitClient(unsigned int timeout) throw(IOError);
00176
00178 inline void getName(NetworkAddress& addr, port_t& port) const throw(LogicError,IOError)
00179 { Socket::getName(addr, port); }
00180
00181 protected:
00182
00184 io_handle_t accept() throw(IOError);
00185
00186 private:
00187 StreamSocketServer(const StreamSocketServer&);
00188 StreamSocketServer& operator=(const StreamSocketServer&);
00189
00190 io_handle_t m_lastAcceptHandle;
00191 };
00192
00194
00198 class PNET_EXPORT StreamSocket: public Socket {
00199 public:
00201 enum sdmode_t {
00202 disableRecv = 0,
00203 disableSend,
00204 disableBoth
00205 };
00206
00208 StreamSocket(const NetworkAddress& addr, port_t port) throw(IOError);
00209
00211 StreamSocket(int domain) throw(IOError);
00212
00214 StreamSocket(StreamSocketServer& srv) throw(IOError);
00215
00216 ~StreamSocket() throw();
00217
00218 void close() throw(IOError);
00219
00221 void shutdown(sdmode_t mode);
00222
00223 void connect(const NetworkAddress& addr, port_t port) throw(IOError);
00224
00225 std::string readLine(unsigned int timeout) throw(IOError);
00226
00227 };
00228
00230
00234 class PNET_EXPORT DatagramSocket: public Socket {
00235 public:
00237 DatagramSocket(const NetworkAddress& addr, port_t port) throw(IOError);
00238
00240 DatagramSocket(int domain) throw(IOError);
00241
00242 ~DatagramSocket() throw();
00243
00244 size_t write(const char* buffer, size_t count) throw(IOError);
00245
00246 size_t read(char* buffer, size_t count) throw(IOError);
00247
00248 size_t peek(char* buffer, size_t count) throw(IOError);
00249
00250 void setPeer(const NetworkAddress& addr, port_t port) throw();
00251
00252 const NetworkAddress& getSender(port_t& port) const throw();
00253
00254 private:
00255 void retrSenderAddr(sockaddr* sa, size_t salen);
00256
00257 NetworkAddress* m_peer;
00258 port_t m_peerPort;
00259 NetworkAddress* m_sender;
00260 port_t m_senderPort;
00261 };
00262
00263 }
00264
00265 #endif