[complement-svn] SF.net SVN: complement: [1718] trunk/complement/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2007-09-01 05:45:14
|
Revision: 1718 http://complement.svn.sourceforge.net/complement/?rev=1718&view=rev Author: complement Date: 2007-08-31 22:45:11 -0700 (Fri, 31 Aug 2007) Log Message: ----------- add inet_sockaddr() method, that return sockaddr_in in whole; replace sleep by yield in recvfrom, but function not really in use now Modified Paths: -------------- trunk/complement/explore/include/sockios/sockstream trunk/complement/explore/include/sockios/sockstream.cc trunk/complement/explore/lib/sockios/ChangeLog Modified: trunk/complement/explore/include/sockios/sockstream =================================================================== --- trunk/complement/explore/include/sockios/sockstream 2007-09-01 05:33:23 UTC (rev 1717) +++ trunk/complement/explore/include/sockios/sockstream 2007-09-01 05:45:11 UTC (rev 1718) @@ -1,22 +1,14 @@ -// -*- C++ -*- Time-stamp: <06/07/11 12:28:37 ptr> +// -*- C++ -*- Time-stamp: <07/08/31 09:49:58 ptr> /* - * Copyright (c) 1997-1999, 2002, 2003, 2005, 2006 + * Copyright (c) 1997-1999, 2002, 2003, 2005-2007 * Petr Ovtchenkov * * Portion Copyright (c) 1999-2001 * Parallel Graphics Ltd. * - * Licensed under the Academic Free License Version 2.1 + * Licensed under the Academic Free License version 3.0 * - * This material is provided "as is", with absolutely no warranty expressed - * or implied. Any use is at your own risk. - * - * Permission to use, copy, modify, distribute and sell this software - * and its documentation for any purpose is hereby granted without fee, - * provided that the above copyright notice appear in all copies and - * that both that copyright notice and this permission notice appear - * in supporting documentation. */ #ifndef __SOCKSTREAM__ @@ -494,6 +486,14 @@ { return /* is_open() && */ _address.any.sa_family == AF_INET ? _address.inet.sin_addr.s_addr : 0; } + const sockaddr_in& inet_sockaddr() const throw( std::domain_error ) + { + if ( _address.any.sa_family != AF_INET ) { + throw domain_error( "socket not belongs to inet type" ); + } + return /* is_open() && */ _address.inet; + } + sock_base::stype stype() const { return _type; } @@ -613,7 +613,6 @@ private: typedef basic_sockbuf<charT,traits,_Alloc> _Self_type; - // int __rdsync(); int (basic_sockbuf<charT,traits,_Alloc>::*_xwrite)( const void *, size_t ); int (basic_sockbuf<charT,traits,_Alloc>::*_xread)( void *, size_t ); int write( const void *buf, size_t n ) @@ -651,7 +650,7 @@ void __hostname(); sock_base::socket_type _fd; - union { + union sockaddr_t { sockaddr_in inet; sockaddr any; } _address; @@ -660,9 +659,6 @@ int _errno; sock_base::stype _type; // bool _doclose; - // if open, and connected, other side hostname, - // otherwise undefined: - // string _hostname; }; template <class charT, class traits, class _Alloc> Modified: trunk/complement/explore/include/sockios/sockstream.cc =================================================================== --- trunk/complement/explore/include/sockios/sockstream.cc 2007-09-01 05:33:23 UTC (rev 1717) +++ trunk/complement/explore/include/sockios/sockstream.cc 2007-09-01 05:45:11 UTC (rev 1718) @@ -1,22 +1,14 @@ // -*- C++ -*- Time-stamp: <06/08/21 23:58:48 ptr> /* - * Copyright (c) 1997-1999, 2002, 2003, 2005, 2006 + * Copyright (c) 1997-1999, 2002, 2003, 2005-2007 * Petr Ovtchenkov * * Portion Copyright (c) 1999-2001 * Parallel Graphics Ltd. * - * Licensed under the Academic Free License Version 2.1 + * Licensed under the Academic Free License version 3.0 * - * This material is provided "as is", with absolutely no warranty expressed - * or implied. Any use is at your own risk. - * - * Permission to use, copy, modify, distribute and sell this software - * and its documentation for any purpose is hereby granted without fee, - * provided that the above copyright notice appear in all copies and - * that both that copyright notice and this permission notice appear - * in supporting documentation. */ #ifdef __unix @@ -106,7 +98,7 @@ throw std::invalid_argument( "protocol not implemented" ); } if ( _bbuf == 0 ) - _M_allocate_block( 0xb00 ); // max 1460 (dec) [0x5b4] --- single segment + _M_allocate_block( type == sock_base::sock_stream ? 0xb00 : 0xffff ); // max 1460 (dec) [0x5b4] --- single segment if ( _bbuf == 0 ) { throw std::length_error( "can't allocate block" ); } @@ -114,21 +106,7 @@ setp( _bbuf, _bbuf + ((_ebuf - _bbuf)>>1) ); setg( this->epptr(), this->epptr(), this->epptr() ); - // _STLP_ASSERT( this->pbase() != 0 ); - // _STLP_ASSERT( this->pptr() != 0 ); - // _STLP_ASSERT( this->epptr() != 0 ); - // _STLP_ASSERT( this->eback() != 0 ); - // _STLP_ASSERT( this->gptr() != 0 ); - // _STLP_ASSERT( this->egptr() != 0 ); - // _STLP_ASSERT( _bbuf != 0 ); - // _STLP_ASSERT( _ebuf != 0 ); - _errno = 0; // if any - // __hostname(); - -// in_port_t ppp = 0x5000; -// cerr << hex << _address.inet.sin_port << " " << ppp << endl; -// cerr << hex << _address.inet.sin_addr.s_addr << endl; } catch ( std::domain_error& ) { #ifdef WIN32 @@ -234,7 +212,7 @@ throw std::invalid_argument( "protocol not implemented" ); } if ( _bbuf == 0 ) - _M_allocate_block( 0xb00 ); // max 1460 (dec) [0x5b4] --- single segment + _M_allocate_block( type == sock_base::sock_stream ? 0xb00 : 0xffff ); // max 1460 (dec) [0x5b4] --- single segment if ( _bbuf == 0 ) { throw std::length_error( "can't allocate block" ); } @@ -242,21 +220,7 @@ setp( _bbuf, _bbuf + ((_ebuf - _bbuf)>>1) ); setg( this->epptr(), this->epptr(), this->epptr() ); - // _STLP_ASSERT( this->pbase() != 0 ); - // _STLP_ASSERT( this->pptr() != 0 ); - // _STLP_ASSERT( this->epptr() != 0 ); - // _STLP_ASSERT( this->eback() != 0 ); - // _STLP_ASSERT( this->gptr() != 0 ); - // _STLP_ASSERT( this->egptr() != 0 ); - // _STLP_ASSERT( _bbuf != 0 ); - // _STLP_ASSERT( _ebuf != 0 ); - _errno = 0; // if any - // __hostname(); - -// in_port_t ppp = 0x5000; -// cerr << hex << _address.inet.sin_port << " " << ppp << endl; -// cerr << hex << _address.inet.sin_addr.s_addr << endl; } catch ( std::domain_error& ) { #ifdef WIN32 @@ -357,7 +321,7 @@ } if ( _bbuf == 0 ) { - _M_allocate_block( 0xb00 ); + _M_allocate_block( t == sock_base::sock_stream ? 0xb00 : 0xffff ); } if ( _bbuf == 0 ) { @@ -373,28 +337,8 @@ setp( _bbuf, _bbuf + ((_ebuf - _bbuf)>>1) ); setg( this->epptr(), this->epptr(), this->epptr() ); - // _STLP_ASSERT( this->pbase() != 0 ); - // _STLP_ASSERT( this->pptr() != 0 ); - // _STLP_ASSERT( this->epptr() != 0 ); - // _STLP_ASSERT( this->eback() != 0 ); - // _STLP_ASSERT( this->gptr() != 0 ); - // _STLP_ASSERT( this->egptr() != 0 ); - // _STLP_ASSERT( _bbuf != 0 ); - // _STLP_ASSERT( _ebuf != 0 ); - _errno = 0; // if any - // __hostname(); -// in_port_t ppp = 0x5000; -// cerr << hex << _address.inet.sin_port << " " << ppp << endl; -// cerr << hex << _address.inet.sin_addr.s_addr << endl; -// sockaddr_in xxx; -// int len = sizeof( xxx ); - -// getpeername( fd(), (sockaddr *)&xxx, &len ); -// cerr << hex << xxx.sin_port << endl; -// cerr << hex << xxx.sin_addr.s_addr << endl; - return this; } @@ -546,16 +490,6 @@ long count = this->pptr() - this->pbase(); if ( count ) { - // _STLP_ASSERT( this->pbase() != 0 ); - - // Never do this: read and and write in basic_sockbuf are independent, - // so reading here lead to lost message if reading and writing occur - // simultaneously from different threads - -// if ( __rdsync() != 0 ) { // I should read something, if other side write -// return traits::eof(); // otherwise I can't write without pipe broken -// } - if ( (this->*_xwrite)( this->pbase(), sizeof(charT) * count ) != count * sizeof(charT) ) return traits::eof(); } @@ -576,12 +510,6 @@ return 0; } - // _STLP_ASSERT( this->pbase() != 0 ); - // _STLP_ASSERT( this->pptr() != 0 ); - // _STLP_ASSERT( this->epptr() != 0 ); - // _STLP_ASSERT( _bbuf != 0 ); - // _STLP_ASSERT( _ebuf != 0 ); - if ( this->epptr() - this->pptr() > n ) { traits::copy( this->pptr(), s, n ); this->pbump( n ); @@ -605,52 +533,7 @@ return n; } -#if 0 // No needs: the sockets are essential bi-direction entities template<class charT, class traits, class _Alloc> -int basic_sockbuf<charT, traits, _Alloc>::__rdsync() -{ -#ifdef WIN32 - unsigned long nlen = 0; - int nmsg = ioctlsocket( fd(), FIONREAD, &nlen ); -#else - long nlen = 0; - int nmsg = ioctl( fd(), I_NREAD, &nlen ); // shouldn't work, as I understand... -#endif - if ( nmsg > 0 && nlen > 0 ) { - // _STLP_ASSERT( _bbuf != 0 ); - // _STLP_ASSERT( _ebuf != 0 ); - // _STLP_ASSERT( this->gptr() != 0 ); - // _STLP_ASSERT( this->egptr() != 0 ); - - bool shift_req = this->gptr() == this->eback() ? false : (_ebuf - this->gptr()) > nlen ? false : true; - if ( shift_req ) { - // _STLP_ASSERT( this->gptr() > this->eback() ); - // _STLP_ASSERT( this->gptr() <= this->egptr() ); - traits::move( this->eback(), this->gptr(), this->egptr() - this->gptr() ); - setg( this->eback(), this->eback(), this->eback() + (this->egptr() - this->gptr()) ); - } - if ( this->gptr() == _ebuf ) { // I should read something, if other side write - return -1; // otherwise I can't write without pipe broken - } - long offset = (this->*_xread)( this->egptr(), sizeof(char_type) * (_ebuf - this->egptr()) ); - if ( offset < 0 ) // allow message of zero length - return -1; - offset /= sizeof(charT); - setg( this->eback(), this->gptr(), this->egptr() + offset ); - } - - return 0; -} -#endif // 0 - -#if defined(__HP_aCC) && (__HP_aCC == 1) - union basic_sockbuf_sockaddr { - sockaddr_in inet; - sockaddr any; - }; -#endif - -template<class charT, class traits, class _Alloc> int basic_sockbuf<charT, traits, _Alloc>::recvfrom( void *buf, size_t n ) { #if defined(_WIN32) || (defined(__hpux) && !defined(_INCLUDE_POSIX1C_SOURCE)) @@ -659,20 +542,9 @@ socklen_t sz = sizeof( sockaddr_in ); #endif -#if defined(__HP_aCC) && (__HP_aCC == 1) - basic_sockbuf_sockaddr addr; -#else - union { - sockaddr_in inet; - sockaddr any; - } addr; -#endif -#ifdef __FIT_POLL - timespec t; + sockaddr_t addr; - t.tv_sec = 0; - t.tv_nsec = 10000; - +#ifdef __FIT_POLL pollfd pfd; pfd.fd = _fd; pfd.events = POLLIN; @@ -704,23 +576,14 @@ return 0; // poll wait infinite, so it can't return 0 (timeout), so it return -1. } #endif // __FIT_POLL - if ( port() == addr.inet.sin_port && inet_addr() == addr.inet.sin_addr.s_addr ) { -// if ( memcmp( (void *)&_address.any, (const void *)&addr, sizeof(sockaddr) ) == 0 ) { + if ( memcmp( &_address.inet, &addr.inet, sizeof(sockaddr_in) ) == 0 ) { #ifdef WIN32 return ::recvfrom( _fd, (char *)buf, n, 0, &_address.any, &sz ); #else return ::recvfrom( _fd, buf, n, 0, &_address.any, &sz ); #endif } -#ifdef __unix -// cerr << "Sleeping in sockstream: " -// << port() << "/" << addr.inet.sin_port << ", " -// << inet_addr() << "/" << addr.inet.sin_addr.s_addr << endl; - nanosleep( &t, 0 ); -#endif -#ifdef WIN32 - Sleep( t ); -#endif + xmt::Thread::yield(); } while ( true ); return 0; // never Modified: trunk/complement/explore/lib/sockios/ChangeLog =================================================================== --- trunk/complement/explore/lib/sockios/ChangeLog 2007-09-01 05:33:23 UTC (rev 1717) +++ trunk/complement/explore/lib/sockios/ChangeLog 2007-09-01 05:45:11 UTC (rev 1718) @@ -1,3 +1,11 @@ +2007-09-01 Petr Ovtchenkov <pt...@is...> + + * sockstream: clean; add inet_sockaddr() method, that return + sockaddr_in in whole; + + * sockstream.cc: clean; replace sleep by yield in recvfrom, + but function not really in use now; + 2007-08-23 Petr Ovtchenkov <pt...@is...> * sockmgr.h: explicitly inhibit copy of basic_sockmgr and This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |