From: Christian P. <cp...@us...> - 2005-06-06 12:09:42
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses/Net In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28363/include/pclasses/Net Modified Files: InetSocket.h ServerSocketManager.h Socket.h Log Message: - Fixed Socket::setBlocking() - Added AsyncSocket, AsyncStreamSocket, AsyncInetSocket, AsyncTCPSocket Index: ServerSocketManager.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Net/ServerSocketManager.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ServerSocketManager.h 10 Feb 2005 19:10:36 -0000 1.1 +++ ServerSocketManager.h 6 Jun 2005 12:09:30 -0000 1.2 @@ -46,7 +46,7 @@ virtual void onConnect(StreamSocketServer& s); private: - void slotConnected(SocketListener&); + void slotConnected(); std::list< StreamSocketServer* Index: Socket.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Net/Socket.h,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- Socket.h 10 Feb 2005 19:13:46 -0000 1.8 +++ Socket.h 6 Jun 2005 12:09:30 -0000 1.9 @@ -20,11 +20,12 @@ #ifndef P_Net_Socket_h #define P_Net_Socket_h - + #include <pclasses/Export.h> #include <pclasses/BasicTypes.h> -#include <pclasses/Signal.h> +#include <pclasses/Buffer.h> #include <pclasses/IO/IODevice.h> +#include <pclasses/IO/IOListener.h> #include <pclasses/System/EventQueue.h> #include <pclasses/Net/NetworkAddress.h> @@ -71,7 +72,7 @@ //! Bind socket to given address and port void bind(const NetworkAddress& addr, port_t port) throw(IO::IOError); - void connect(const NetworkAddress& addr, port_t port) throw(IO::IOError); + virtual void connect(const NetworkAddress& addr, port_t port) throw(IO::IOError); void setSendTimeout(unsigned int timeout) throw(IO::IOError); unsigned int sendTimeout() const throw(IO::IOError); @@ -101,10 +102,10 @@ protected: Socket(Domain domain, Type type, int proto = 0) throw(IO::IOError); - void open(Domain domain, Type type, int proto) + virtual void open(Domain domain, Type type, int proto) throw(IO::IOError); - void open(int handle, Domain domain, Type type, int proto) + virtual void open(int handle, Domain domain, Type type, int proto) throw(IO::IOError); int handle() const throw(); @@ -124,37 +125,83 @@ //! Socket Event-listener -class PNET_EXPORT SocketListener: public System::EventListener { +class PNET_EXPORT SocketListener: public IO::IOListener, + public System::EventListener +{ public: - enum EventMask { - Read = 0x01, - Write = 0x02, - Error = 0x04 - }; - SocketListener(Socket& s); SocketListener(System::EventQueue& evq, Socket& s); ~SocketListener(); - Socket& socket() const throw(); - void setEventMask(int mask); int eventMask() const throw(); - Signal1<void, SocketListener&> sigRead; - Signal1<void, SocketListener&> sigWrite; - Signal1<void, SocketListener&> sigError; + private: + void signaled(const System::Event& ev); + void* _handle; +}; + + +//! Asynchronous socket +class PNET_EXPORT AsyncSocket: public virtual Socket { + public: + enum ConnectState { + NotConnected, + Connecting, + Connected + }; + + AsyncSocket() throw(); + AsyncSocket(System::EventQueue& evq) throw(); + ~AsyncSocket() throw(); + + void connect(const NetworkAddress& addr, port_t port) throw(IO::IOError); + + ConnectState connectState() const throw(); protected: - virtual void onRead(); + AsyncSocket(Domain domain, Type type, int proto = 0) throw(IO::IOError); + + void open(Domain domain, Type type, int proto) + throw(IO::IOError); + + void open(int handle, Domain domain, Type type, int proto) + throw(IO::IOError); + + //! Called after connection has been established + virtual void onConnect(); + + //! Called when a connection-attempt has been failed + virtual void onConnectFailed(const IO::IOError& err); + + //! Called when data has been received + virtual void onRead(const char* buffer, size_t count); + + //! Called when socket is ready for more data virtual void onWrite(); - virtual void onError(); + + //! Called on asynchronous socket error + virtual void onError(const IO::IOError& err); + + //! Called when connection has been closed + virtual void onClose(); + + // IODevice related methods + void _close() throw(IO::IOError); + size_t _read(char* buffer, size_t count) throw(IO::IOError); + size_t _peek(char* buffer, size_t count) throw(IO::IOError); + size_t _write(const char* buffer, size_t count) throw(IO::IOError); private: - void signaled(const System::Event& ev); - unsigned long _handle; -}; + void writeNotify(); + void readNotify(); + void errorNotify(); + System::EventQueue& _eventQueue; + SocketListener* _listener; + Buffer<char> _writeBuffer; + ConnectState _connState; +}; //! Datagram Socket @@ -168,6 +215,7 @@ }; + //! Streaming Socket Server class PNET_EXPORT StreamSocketServer: public Socket { public: @@ -191,6 +239,7 @@ int accept() throw(IO::IOError); }; + //! Streaming Socket class PNET_EXPORT StreamSocket: public virtual Socket { public: @@ -199,6 +248,21 @@ StreamSocket(StreamSocketServer& srv) throw(IO::IOError); ~StreamSocket() throw(); + virtual void open(Domain d, int proto) throw(IO::IOError); + virtual void open(StreamSocketServer& srv) throw(IO::IOError); +}; + + +//! Asynchronous streaming socket +class PNET_EXPORT AsyncStreamSocket: + public virtual AsyncSocket, public virtual StreamSocket +{ + public: + AsyncStreamSocket() throw(); + AsyncStreamSocket(Domain d, int proto) throw(IO::IOError); + AsyncStreamSocket(StreamSocketServer& srv) throw(IO::IOError); + ~AsyncStreamSocket() throw(); + void open(Domain d, int proto) throw(IO::IOError); void open(StreamSocketServer& srv) throw(IO::IOError); }; Index: InetSocket.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Net/InetSocket.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- InetSocket.h 26 Jan 2005 10:25:40 -0000 1.3 +++ InetSocket.h 6 Jun 2005 12:09:30 -0000 1.4 @@ -43,11 +43,26 @@ int timeToLive() const throw(IO::IOError); protected: + virtual void open(Type type, int proto) throw(IO::IOError); +}; + +//! Asynchronous IPv4 Socket +class PNET_EXPORT AsyncInetSocket: + public virtual AsyncSocket, public virtual InetSocket +{ + public: + AsyncInetSocket() throw(); + AsyncInetSocket(Type t, int proto) throw(IO::IOError); + ~AsyncInetSocket() throw(); + + protected: void open(Type type, int proto) throw(IO::IOError); }; //! UDP Datagram Socket -class PNET_EXPORT UDPSocket: public InetSocket, public DatagramSocket { +class PNET_EXPORT UDPSocket: + public virtual InetSocket, public virtual DatagramSocket +{ public: UDPSocket() throw(IO::IOError); ~UDPSocket() throw(); @@ -66,15 +81,28 @@ }; //! TCP Streaming Socket -class PNET_EXPORT TCPSocket: public InetSocket, public StreamSocket { +class PNET_EXPORT TCPSocket: + public virtual InetSocket, public virtual StreamSocket +{ public: TCPSocket() throw(IO::IOError); TCPSocket(StreamSocketServer& srv) throw(IO::IOError); ~TCPSocket() throw(); + virtual void open() throw(IO::IOError); + virtual void open(StreamSocketServer& srv) throw(IO::IOError); +}; + +class PNET_EXPORT AsyncTCPSocket: + public AsyncInetSocket, public AsyncStreamSocket, public TCPSocket +{ + public: + AsyncTCPSocket() throw(IO::IOError); + AsyncTCPSocket(StreamSocketServer& srv) throw(IO::IOError); + ~AsyncTCPSocket() throw(); + void open() throw(IO::IOError); void open(StreamSocketServer& srv) throw(IO::IOError); - }; } // !namespace Net |