📄 unixsocket.hpp
字号:
//// This file is part of the "More for C++" library//// Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)//// The "More for C++" library is free software; you can redistribute it and/or// modify it under the terms of the license that comes with this package.//// Read "license.txt" for more details.//// THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES// OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#ifndef UNIXSOCKET_HPP#define UNIXSOCKET_HPP////////////////////////////////////////////////////////////////////////////////#include <more/core/finalizable.hpp>#include <more/os/socket.hpp>#include <sys/socket.h>#include <netinet/in.h>////////////////////////////////////////////////////////////////////////////////namespace more{ namespace os { namespace unix_ { class UnixSocketAddress { public: UnixSocketAddress( struct sockaddr_in& rSockAddrIn ); virtual void resolve( const String& sNameOfHost, int nNoOfPort ) throw( more::io::IOException ); private: struct sockaddr_in& m_rSockAddrIn; }; //////////////////////////////////////////////////////////////////////// class UnixAcceptorSocket: public AcceptorSocket, public more::core::Finalizable { public: UnixAcceptorSocket( ); virtual void finalize( ); virtual bool isValid( ) const; virtual void bind( const String& sMask, size_t nNoOfPort ) throw( more::io::IOException ); virtual void bind( size_t nNoOfPort ) throw( more::io::IOException ); virtual String getNameOfHost( ) const; virtual size_t getNoOfPort( ) const; virtual p<Socket> accept( ) throw( more::io::IOException ); virtual void cancel( ) throw( more::io::IOException ); virtual void close( ) throw( more::io::IOException ); protected: virtual void throwIfNotValid( ) const throw( more::io::IOException ); private: int m_socket; int m_nNoOfPort; struct sockaddr_in m_sockAddrIn; bool m_bHasBeenCancelled; }; //////////////////////////////////////////////////////////////////////// class UnixSocket: public Socket, public more::core::Finalizable { public: UnixSocket( ); UnixSocket( int socket, sockaddr_in& rSockAddrIn ); virtual void finalize( ); virtual bool isValid( ) const; virtual void connect( const String& sNameOfHost, size_t nNoOfPort ) throw( more::io::IOException ); virtual String getNameOfHost( ) const; virtual size_t getNoOfPort( ) const; virtual p<more::io::InputStream> getInputStream ( ) throw( more::io::IOException ); virtual p<more::io::OutputStream> getOutputStream ( ) throw( more::io::IOException ); virtual void close( ) throw( more::io::IOException ); protected: void throwIfNotValid( ) const throw( more::io::IOException ); void throwIfNotConnected( ) const throw( more::io::IOException ); private: int m_socket; struct sockaddr_in m_sockAddrIn; bool m_bHasBeenConnected; // For UnixSocketInputStream/UnixSocketOutputStream: public: virtual size_t available( ) throw( more::io::IOException ); virtual bool hasBeenClosed( ) throw( more::io::IOException ); virtual size_t skip( size_t nNoOfBytes ) throw( more::io::IOException ); virtual size_t read( void* pBuffer, size_t nMaxNoOfBytes ) throw( more::io::IOException ); virtual void write( const void* pBuffer, size_t nNoOfBytes ) throw( more::io::IOException ); virtual size_t rewind( size_t nNoOfBytes ) throw( more::io::IOException ); virtual void flush( ) throw( more::io::IOException ); }; class UnixSocketInputStream: public more::io::InputStream { public: UnixSocketInputStream( const p<UnixSocket>& ); virtual size_t available( ) throw( more::io::IOException ); virtual bool hasBeenClosed( ) throw( more::io::IOException ); virtual size_t skip( size_t nNoOfBytes ) throw( more::io::IOException ); virtual size_t read( void* pBuffer, size_t nMaxNoOfBytes ) throw( more::io::IOException ); private: p<UnixSocket> m_pUnixSocket; }; class UnixSocketOutputStream: public more::io::OutputStream { public: UnixSocketOutputStream ( const p<UnixSocket>& ); virtual void write( const void* pBuffer, size_t nNoOfBytes ) throw( more::io::IOException ); virtual size_t rewind( size_t nNoOfBytes ) throw( more::io::IOException ); virtual bool hasBeenClosed( ) throw( more::io::IOException ); virtual void flush( ) throw( more::io::IOException ); private: p<UnixSocket> m_pUnixSocket; }; } }}////////////////////////////////////////////////////////////////////////////////inline void more::os::unix_::UnixAcceptorSocket::throwIfNotValid( ) const throw( more::io::IOException ){ if( !isValid( ) ) { throw more::io::IOException( -1, "Invalid socket" ); }}////////////////////////////////////////////////////////////////////////////////inline void more::os::unix_::UnixSocket::throwIfNotValid( ) const throw( more::io::IOException ){ if( !isValid( ) ) { throw more::io::IOException( -1, "Invalid socket" ); }}////////////////////////////////////////////////////////////////////////////////inline void more::os::unix_::UnixSocket::throwIfNotConnected( ) const throw( more::io::IOException ){ if( !m_bHasBeenConnected ) { throw more::io::IOException( -1, "Socket has not been connected" ); }}////////////////////////////////////////////////////////////////////////////////#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -