From: Markus R. <rol...@us...> - 2007-02-09 20:09:37
|
Update of /cvsroot/simspark/simspark/spark/utility/rcssnet In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv9359 Modified Files: Tag: WIN32 README addr.cpp addr.hpp exception.cpp socket.cpp socket.hpp socketstreambuf.hpp tcpsocket.cpp tcpsocket.hpp udpsocket.cpp udpsocket.hpp Log Message: - updated intree rcssnet library to 10.011 to take advantage of windows socket implementation Index: udpsocket.hpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/udpsocket.hpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** udpsocket.hpp 19 Dec 2005 19:13:30 -0000 1.1 --- udpsocket.hpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 2,9 **** /*************************************************************************** ! updsocket.hpp - A simple upd socket class ------------------- begin : 08-JAN-2003 ! copyright : (C) 2003 by The RoboCup Soccer Server Maintenance Group. email : sse...@li... --- 2,9 ---- /*************************************************************************** ! udpsocket.hpp - A simple udp socket class ------------------- begin : 08-JAN-2003 ! copyright : (C) 2003 by The RoboCup Soccer Server Maintenance Group. email : sse...@li... *************** *** 34,42 **** public: UDPSocket(); UDPSocket( const Addr& addr ); UDPSocket( const Addr& addr, const Addr& dest ); ! protected: ! virtual void doOpen( int& fd ); }; } --- 34,44 ---- public: UDPSocket(); + UDPSocket( SocketDesc& s ); UDPSocket( const Addr& addr ); UDPSocket( const Addr& addr, const Addr& dest ); ! private: ! bool ! doOpen( SocketDesc& fd ); }; } Index: socketstreambuf.hpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/socketstreambuf.hpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** socketstreambuf.hpp 19 Dec 2005 19:13:30 -0000 1.1 --- socketstreambuf.hpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 5,9 **** ------------------- begin : 08-JAN-2003 ! copyright : (C) 2003 by The RoboCup Soccer Server Maintenance Group. email : sse...@li... --- 5,9 ---- ------------------- begin : 08-JAN-2003 ! copyright : (C) 2003 by The RoboCup Soccer Server Maintenance Group. email : sse...@li... *************** *** 44,48 **** SocketStreamBuf( Socket& socket, const Addr& dest, ! ConnType conn = CONN_ON_READ, std::streamsize bufsize = 8192 ) : m_socket( socket ), --- 44,48 ---- SocketStreamBuf( Socket& socket, const Addr& dest, ! ConnType conn = CONN_ON_READ, std::streamsize bufsize = 8192 ) : m_socket( socket ), *************** *** 55,59 **** {} ! SocketStreamBuf( Socket& socket, ConnType conn = NO_CONN, --- 55,59 ---- {} ! SocketStreamBuf( Socket& socket, ConnType conn = NO_CONN, *************** *** 67,71 **** {} ! virtual ~SocketStreamBuf() { --- 67,71 ---- {} ! virtual ~SocketStreamBuf() { *************** *** 74,93 **** delete [] m_outbuf; } ! void setEndPoint( const Addr& addr ) { m_end_point = addr; } ! void setConnectType( ConnType conn ) { m_connect = conn; } ! protected: ! virtual bool writeData() { ! int size = (pptr () - m_outbuf) * sizeof(char_type); if( size == 0 ) return true; --- 74,93 ---- delete [] m_outbuf; } ! void setEndPoint( const Addr& addr ) { m_end_point = addr; } ! void setConnectType( ConnType conn ) { m_connect = conn; } ! protected: ! virtual bool writeData() { ! unsigned long size = (unsigned long)((pptr () - m_outbuf) * sizeof(char_type)); if( size == 0 ) return true; *************** *** 98,103 **** return m_socket.send( m_outbuf, size, m_end_point ) > 0; } ! ! virtual int_type overflow( int_type c = EOF ) --- 98,103 ---- return m_socket.send( m_outbuf, size, m_end_point ) > 0; } ! ! virtual int_type overflow( int_type c = EOF ) *************** *** 105,109 **** // this method is supposed to flush the put area of the buffer // to the I/O device ! // if the buffer was not already allocated nor set by user, // do it just now --- 105,109 ---- // this method is supposed to flush the put area of the buffer // to the I/O device ! // if the buffer was not already allocated nor set by user, // do it just now *************** *** 124,130 **** return 0; } ! ! virtual ! int sync() { --- 124,130 ---- return 0; } ! ! virtual ! int sync() { *************** *** 140,151 **** return 0; } ! ! virtual ! int_type ! underflow() { // this method is supposed to read some bytes from the I/O device ! // if the buffer was not already allocated nor set by user, // do it just now --- 140,151 ---- return 0; } ! ! virtual ! int_type ! underflow() { // this method is supposed to read some bytes from the I/O device ! // if the buffer was not already allocated nor set by user, // do it just now *************** *** 154,162 **** m_inbuf = new char_type[m_bufsize]; } ! if( m_remained != 0 ) m_inbuf[0] = m_remained_char; ! ! int readn = m_bufsize * sizeof( char_type ) - m_remained; if( m_socket.isConnected() ) readn = m_socket.recv( m_inbuf + m_remained, --- 154,162 ---- m_inbuf = new char_type[m_bufsize]; } ! if( m_remained != 0 ) m_inbuf[0] = m_remained_char; ! ! int readn = m_bufsize * sizeof( char_type ) - m_remained; if( m_socket.isConnected() ) readn = m_socket.recv( m_inbuf + m_remained, *************** *** 171,176 **** m_socket.connect( addr ); } ! ! if( readn < 0 || readn == 1 && (m_inbuf + m_remained)[ 0 ] == -1 ) { (m_inbuf + m_remained)[ 0 ] = -1; --- 171,176 ---- m_socket.connect( addr ); } ! ! if( readn < 0 || ( readn == 1 && (m_inbuf + m_remained)[ 0 ] == -1 ) ) { (m_inbuf + m_remained)[ 0 ] = -1; *************** *** 180,200 **** setg( m_inbuf, m_inbuf, m_inbuf + totalbytes / sizeof(char_type) ); ! m_remained = totalbytes % sizeof( char_type ); if( m_remained != 0 ) m_remained_char = m_inbuf[totalbytes / sizeof(char_type)]; ! return sgetc(); } ! private: ! // not used SocketStreamBuf( const SocketStreamBuf& ); ! // not used ! SocketStreamBuf& operator=( const SocketStreamBuf& ); ! Socket& m_socket; Addr m_end_point; --- 180,200 ---- setg( m_inbuf, m_inbuf, m_inbuf + totalbytes / sizeof(char_type) ); ! m_remained = totalbytes % sizeof( char_type ); if( m_remained != 0 ) m_remained_char = m_inbuf[totalbytes / sizeof(char_type)]; ! return sgetc(); } ! private: ! // not used SocketStreamBuf( const SocketStreamBuf& ); ! // not used ! SocketStreamBuf& operator=( const SocketStreamBuf& ); ! Socket& m_socket; Addr m_end_point; Index: tcpsocket.cpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/tcpsocket.cpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** tcpsocket.cpp 19 Dec 2005 19:13:30 -0000 1.1 --- tcpsocket.cpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 1,28 **** ! /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- ! ! this file is part of rcssserver3D ! Fri May 9 2003 ! Copyright (C) 2002,2003 Koblenz University ! Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group ! $Id$ ! This program is free software; you can redistribute it and/or modify ! it under the terms of the GNU General Public License as published by ! the Free Software Foundation; version 2 of the License. ! This program is distributed in the hope that it will be useful, ! but WITHOUT ANY WARRANTY; without even the implied warranty of ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ #include "tcpsocket.hpp" ! #include "exception.hpp" #include <sys/types.h> #include <sys/socket.h> #include <cerrno> namespace rcss --- 1,42 ---- ! // -*-c++-*- ! /*************************************************************************** ! tcpsocket.cpp - A simple tcp socket class ! ------------------- ! begin : 08-JAN-2003 ! copyright : (C) 2003 by The RoboCup Soccer Server ! Maintenance Group. ! email : sse...@li... ! ***************************************************************************/ ! /*************************************************************************** ! * * ! * This program is free software; you can redistribute it and/or modify * ! * it under the terms of the GNU LGPL as published by the Free Software * ! * Foundation; either version 2 of the License, or (at your option) any * ! * later version. * ! * * ! ***************************************************************************/ #include "tcpsocket.hpp" ! ! #ifdef HAVE_CONFIG_H ! #include <config.h> ! #endif ! #include <sys/types.h> + #ifdef HAVE_SYS_SOCKET_H #include <sys/socket.h> + #endif #include <cerrno> + #ifdef HAVE_NETINET_IN_H + #include <netinet/in.h> + #endif + + #ifdef HAVE_WINSOCK2_H + #include "Winsock2.h" + #endif + + #include <iostream> namespace rcss *************** *** 35,38 **** --- 49,56 ---- } + TCPSocket::TCPSocket( SocketDesc& s ) + : Socket( s ) + {} + TCPSocket::TCPSocket( const Addr& addr ) { *************** *** 44,85 **** { open(); ! bind( addr ); connect( dest ); } ! TCPSocket::TCPSocket( int socket ) ! { ! m_open = true; ! m_connected = true; ! m_socket = socket; ! } ! ! void ! TCPSocket::doOpen( int& fd ) { close(); ! fd = ::socket(AF_INET, SOCK_STREAM, 0 ); ! if( fd < 0 ) ! throw OpenErr( errno ); ! } ! ! ! Socket* TCPSocket::accept(Addr& addr) ! { ! socklen_t len = sizeof(struct sockaddr); ! int fd = ::accept( ! m_socket, ! (struct sockaddr *)&( addr.getAddr() ), ! &len ! ); ! ! if (fd < 0) ! { ! throw AcceptErr( errno ); ! } ! ! return new TCPSocket(fd); } - } } --- 62,76 ---- { open(); ! bind( addr ); connect( dest ); } ! bool ! TCPSocket::doOpen( SocketDesc& fd ) { close(); ! fd = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP ); ! return fd >= 0; } } } Index: addr.cpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/addr.cpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** addr.cpp 19 Dec 2005 19:13:30 -0000 1.1 --- addr.cpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 5,9 **** ------------------- begin : 07-JAN-2003 ! copyright : (C) 2003 by The RoboCup Soccer Server Maintenance Group. email : sse...@li... --- 5,9 ---- ------------------- begin : 07-JAN-2003 ! copyright : (C) 2003 by The RoboCup Soccer Server Maintenance Group. email : sse...@li... *************** *** 20,29 **** #include "addr.hpp" ! #include "exception.hpp" #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> namespace rcss --- 20,52 ---- #include "addr.hpp" ! ! #ifdef HAVE_CONFIG_H ! #include "config.h" ! #endif ! ! #ifdef HAVE_NETDB_H #include <netdb.h> + #endif + #include <sys/types.h> + + #ifdef HAVE_SYS_SOCKET_H #include <sys/socket.h> + #endif + #ifdef HAVE_NETINET_IN_H #include <netinet/in.h> + #endif + #ifdef HAVE_ARPA_INET_H #include <arpa/inet.h> + #endif + + #ifdef HAVE_WINSOCK2_H + #include "Winsock2.h" + #endif + + #include "handler.hpp" + #include "exception.hpp" + #include <iostream> + #include <errno.h> namespace rcss *************** *** 36,65 **** class AddrImpl { ! public: ! AddrImpl() ! : m_addr() ! { memset( (char *)&m_addr, 0, sizeof( m_addr ) ); } ! ! AddrImpl( const Addr::AddrType& addr ) ! : m_addr( addr ) ! {} ! AddrImpl( Addr::PortType port ) { ! memset( (char *)&m_addr, 0, sizeof( m_addr ) ) ; ! m_addr.sin_family = AF_INET ; ! m_addr.sin_addr.s_addr = htonl( INADDR_ANY ); ! m_addr.sin_port = htons( port ); } ! AddrImpl( Addr::PortType port, Addr::HostType host ) { ! memset( (char *)&m_addr, 0, sizeof( m_addr ) ) ; ! m_addr.sin_family = AF_INET ; ! m_addr.sin_addr.s_addr = htonl( host ); ! m_addr.sin_port = htons( port ); } AddrImpl( Addr::PortType port, const std::string& host ) ! : m_host_name( host ) { struct hostent* host_ent --- 59,100 ---- class AddrImpl { ! private: ! void ! setAddr( Addr::PortType port, Addr::HostType host ) ! { ! memset( (char *)&m_addr, 0, sizeof( m_addr ) ) ; ! m_addr.sin_family = AF_INET ; ! m_addr.sin_addr.s_addr = host; ! m_addr.sin_port = port; ! } ! ! bool ! setPortRaw( Addr::PortType port ) { ! m_addr.sin_port = port; ! return true; } ! ! bool ! setHostRaw( Addr::HostType host ) { ! m_addr.sin_addr.s_addr = host; ! return true; } + public: + AddrImpl( const Addr::AddrType& addr ) + : m_handler( Handler::instance() ), + m_addr( addr ) + {} + AddrImpl( Addr::PortType port, Addr::HostType host ) + : m_handler( Handler::instance() ) + { + setAddr( htons( port ), htonl( host ) ); + } + AddrImpl( Addr::PortType port, const std::string& host ) ! : m_handler( Handler::instance() ), m_host_name( host ) { struct hostent* host_ent *************** *** 75,78 **** --- 110,181 ---- } + + bool + setPort( Addr::PortType port ) + { + m_port_name = std::string(); + return setPortRaw( htons( port ) ); + } + + bool + setPort( const std::string& port, + const std::string& proto = "" ) + { + if( port.empty() ) + { + #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(CYGWIN) + errno = WSAHOST_NOT_FOUND; + #else + errno = HOST_NOT_FOUND; + #endif + return false; + } + struct servent* serv_ent + = (struct servent*)getservbyname( port.c_str(), + ( proto.empty() + ? NULL + : proto.c_str() ) ); + if( serv_ent == NULL ) + { + return false; + } + m_port_name = port; + return setPortRaw( serv_ent->s_port ); + } + + bool + setHost( Addr::HostType host ) + { + m_host_name = std::string(); + return setHostRaw( htonl( host ) ); + } + + bool + setHost( const std::string& host ) + { + if( host.empty() ) + { + #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(CYGWIN) + errno = WSAHOST_NOT_FOUND; + #else + errno = HOST_NOT_FOUND; + #endif + return false; + } + struct hostent* host_ent + = (struct hostent*)gethostbyname( host.c_str() ); + if( host_ent == NULL ) + { + #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(CYGWIN) + return false; + #else + return false; + #endif + } + m_host_name = host; + return setHostRaw( ((struct in_addr *)host_ent->h_addr_list[0])->s_addr ); + } + + const Addr::AddrType& getAddr() const *************** *** 82,90 **** getPort() const { return ntohs( m_addr.sin_port ); } ! ! void ! setPort(Addr::PortType port) ! { m_addr.sin_port = htons(port); } ! Addr::HostType getHost() const --- 185,189 ---- getPort() const { return ntohs( m_addr.sin_port ); } ! Addr::HostType getHost() const *************** *** 100,123 **** return m_host_name; } ! private: ! Addr::AddrType m_addr; mutable std::string m_host_name; }; ! Addr::Addr() ! : m_impl( new AddrImpl() ) {} ! Addr::Addr( const AddrType& addr ) : m_impl( new AddrImpl( addr ) ) {} ! Addr::Addr( PortType port ) ! : m_impl( new AddrImpl( port ) ) ! {} ! Addr::Addr( PortType port, HostType host ) ! : m_impl( new AddrImpl( port, host ) ) ! {} Addr::Addr( PortType port, const std::string& host ) --- 199,261 ---- return m_host_name; } ! ! std::string ! getPortStr( const std::string& proto = "" ) const ! { ! if( m_port_name.empty() ) ! { ! struct servent* serv_ent = getservbyport( m_addr.sin_port, ! ( proto.empty() ! ? NULL ! : proto.c_str() ) ); ! if( serv_ent == NULL ) ! { ! return m_port_name; ! } ! m_port_name = serv_ent->s_name; ! } ! return m_port_name; ! } ! private: ! Handler& m_handler; ! Addr::AddrType m_addr; mutable std::string m_host_name; + mutable std::string m_port_name; + Addr::Error m_error; + int m_errno; }; ! Addr::Addr( PortType port, HostType host ) ! : m_impl( new AddrImpl( port, host ) ) {} ! Addr::Addr( const AddrType& addr ) : m_impl( new AddrImpl( addr ) ) {} ! bool ! Addr::setPort( PortType port ) ! { ! return m_impl->setPort( port ); ! } ! bool ! Addr::setPort( const std::string& port, ! const std::string& proto ) ! { ! return m_impl->setPort( port, proto ); ! } ! ! bool ! Addr::setHost( HostType host ) ! { ! return m_impl->setHost( host ); ! } ! ! bool ! Addr::setHost( const std::string& host ) ! { ! return m_impl->setHost( host ); ! } Addr::Addr( PortType port, const std::string& host ) *************** *** 125,128 **** --- 263,267 ---- {} + const Addr::AddrType& Addr::getAddr() const *************** *** 133,140 **** { return m_impl->getPort(); } - void - Addr::setPort(PortType port) - { m_impl->setPort(port); } - Addr::HostType Addr::getHost() const --- 272,275 ---- *************** *** 145,161 **** { return m_impl->getHostStr(); } ! bool ! Addr::operator==( const Addr& addr ) const { ! return ( addr.getAddr().sin_port == getAddr().sin_port ! && ( addr.getAddr().sin_addr.s_addr ! == getAddr().sin_addr.s_addr ) ); } bool ! Addr::operator < ( const Addr& addr ) const { ! const Addr::HostType host_a = getHost(); ! const Addr::HostType host_b = addr.getHost(); if (host_a != host_b) --- 280,309 ---- { return m_impl->getHostStr(); } ! std::string ! Addr::getPortStr( const std::string& proto ) const ! { return m_impl->getPortStr( proto ); } ! ! bool ! operator==( const Addr& a, ! const Addr& b ) { ! return ( a.getAddr().sin_port == b.getAddr().sin_port ! && ( a.getAddr().sin_addr.s_addr ! == b.getAddr().sin_addr.s_addr ) ); } + bool + operator!=( const Addr& a, + const Addr& b ) + { + return !(a == b); + } + bool ! operator < ( const Addr& a, ! const Addr& b ) { ! const Addr::HostType host_a = a.getHost(); ! const Addr::HostType host_b = b.getHost(); if (host_a != host_b) *************** *** 164,168 **** } ! return getPort() < addr.getPort(); } --- 312,316 ---- } ! return a.getPort() < b.getPort(); } *************** *** 170,173 **** --- 318,322 ---- operator<<( std::ostream& o, const rcss::net::Addr& addr ) { return o << '(' << addr.getPort() << ':' << addr.getHostStr() << ')'; } + } } Index: addr.hpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/addr.hpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** addr.hpp 19 Dec 2005 19:13:30 -0000 1.1 --- addr.hpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 5,9 **** ------------------- begin : 07-JAN-2003 ! copyright : (C) 2003 by The RoboCup Soccer Server Maintenance Group. email : sse...@li... --- 5,9 ---- ------------------- begin : 07-JAN-2003 ! copyright : (C) 2003 by The RoboCup Soccer Server Maintenance Group. email : sse...@li... *************** *** 33,37 **** { class AddrImpl; ! class Addr { --- 33,37 ---- { class AddrImpl; ! class Addr { *************** *** 41,78 **** typedef struct sockaddr_in AddrType; static const HostType BROADCAST; static const HostType ANY; ! ! Addr(); ! Addr( const AddrType& addr ); ! Addr( PortType port ); ! Addr( PortType port, HostType host ); Addr( PortType port, const std::string& host ); const AddrType& getAddr() const; ! PortType getPort() const; ! ! void ! setPort(PortType port); ! HostType getHost() const; ! std::string getHostStr() const; ! bool operator == ( const Addr& addr ) const; ! bool operator < (const Addr& addr ) const; ! ! private: boost::shared_ptr< AddrImpl > m_impl; }; ! std::ostream& operator<<( std::ostream& o, const Addr& addr ); - } } --- 41,93 ---- typedef struct sockaddr_in AddrType; + enum Error { eADDR_OK, eSERV_NOT_FOUND, eHOST_NOT_FOUND }; + static const HostType BROADCAST; static const HostType ANY; ! ! Addr( PortType port = 0, HostType host = Addr::ANY ); Addr( PortType port, const std::string& host ); + Addr( const AddrType& addr ); + + bool setPort( PortType port = 0 ); + bool setPort( const std::string& port, + const std::string& proto = "" ); + bool setHost( HostType host = Addr::ANY ); + bool setHost( const std::string& host ); + const AddrType& getAddr() const; ! PortType getPort() const; ! HostType getHost() const; ! std::string getHostStr() const; ! std::string ! getPortStr( const std::string& proto = "" ) const; ! ! private: boost::shared_ptr< AddrImpl > m_impl; }; + + bool + operator ==( const Addr& a, + const Addr& b ); + + bool + operator !=( const Addr& a, + const Addr& b ); ! bool ! operator < ( const Addr& a, ! const Addr& b ); ! ! std::ostream& operator<<( std::ostream& o, const Addr& addr ); } } Index: socket.cpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/socket.cpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** socket.cpp 19 Dec 2005 19:13:30 -0000 1.1 --- socket.cpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 24,31 **** --- 24,40 ---- #include <sys/types.h> + + #ifdef HAVE_SYS_SOCKET_H #include <sys/socket.h> + #endif + #ifdef HAVE_NETINET_IN_H #include <netinet/in.h> + #endif + #ifdef HAVE_ARPA_INET_H #include <arpa/inet.h> + #endif + #ifdef HAVE_UNISTD_H #include <unistd.h> + #endif #include <fcntl.h> #include <cerrno> *************** *** 33,37 **** #include <poll.h> #endif - #include "exception.hpp" #ifndef HAVE_SOCKLEN_T --- 42,45 ---- *************** *** 39,42 **** --- 47,58 ---- #endif + #ifdef HAVE_WINSOCK2_H + #include "Winsock2.h" + #endif + + #include "handler.hpp" + #include "tcpsocket.hpp" + + #include <iostream> namespace rcss *************** *** 44,61 **** namespace net { Socket::Socket() ! : m_socket( 0 ), ! m_open( false ), ! m_connected( false ) {} Socket::~Socket() { close(); } ! void Socket::open() { ! doOpen( m_socket ); ! m_open = true; int err = setCloseOnExec(); if( err < 0 ) --- 60,108 ---- namespace net { + + + #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(CYGWIN) + const Socket::SocketDesc Socket::INVALIDSOCKET = INVALID_SOCKET; + #else + const Socket::SocketDesc Socket::INVALIDSOCKET = -1; + #endif + + + void + Socket::closeFD( SocketDesc* s ) + { + #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) + ::closesocket( *s ); + #else + ::close( *s ); + #endif + delete s; + } + Socket::Socket() ! : m_handler( &Handler::instance() ) {} + Socket::Socket( SocketDesc s ) + : m_handler( &Handler::instance() ) + { + m_handle = boost::shared_ptr< SocketDesc >( new SocketDesc( s ), + Socket::closeFD ); + } + + Socket::~Socket() { close(); } ! bool Socket::open() { ! SocketDesc s; ! if( !doOpen( s ) ) ! return false; ! ! m_handle = boost::shared_ptr< SocketDesc >( new SocketDesc( s ), ! Socket::closeFD ); ! #if !defined(_WIN32) && !defined(__WIN32__) && !defined(WIN32) int err = setCloseOnExec(); if( err < 0 ) *************** *** 63,92 **** err = errno; close(); ! throw OpenErr( err ); } } ! void Socket::bind( const Addr& addr ) { ! int err = ::bind( m_socket, ! (struct sockaddr *)&( addr.getAddr() ), ! sizeof( addr.getAddr() ) ); ! if( err < 0 ) ! throw BindErr( errno ); ! } ! ! void ! Socket::listen( int backlog ) ! { ! int err = ::listen( m_socket, backlog ); ! if (err < 0 ) ! throw ListenErr( errno ); ! } ! ! Socket* Socket::accept(Addr& addr) ! { ! throw AcceptErr( EOPNOTSUPP ); ! return 0; } --- 110,137 ---- err = errno; close(); ! return false; } + #endif + return true; } ! bool Socket::bind( const Addr& addr ) { ! if( isOpen() ) ! { ! int err = ::bind( getFD(), ! (struct sockaddr *)&( addr.getAddr() ), ! sizeof( addr.getAddr() ) ); ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! if( err == SOCKET_ERROR ) ! return false; ! #else ! if( err < 0 ) ! return false; ! #endif ! return true; ! } ! return false; } *************** *** 94,118 **** Socket::getName() const { ! Addr::AddrType name; ! socklen_t from_len = sizeof( name ); ! int err = ::getsockname( m_socket, ! (struct sockaddr *)&name, ! &from_len ); ! if( err < 0 ) ! throw GetNameErr( errno ); ! ! return Addr( name ); } ! void Socket::connect( const Addr& addr ) { ! int err = ::connect( m_socket, ! (const struct sockaddr *)&( addr.getAddr() ), ! sizeof( addr.getAddr() ) ); ! if ( err < 0 ) ! throw ConnectErr( errno ); ! m_connected = true; } --- 139,185 ---- Socket::getName() const { ! if( isOpen() ) ! { ! Addr::AddrType name; ! socklen_t from_len = sizeof( name ); ! int err = ::getsockname( getFD(), ! (struct sockaddr *)&name, ! &from_len ); ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(CYGWIN) ! if( err == SOCKET_ERROR ) ! { ! err = WSAGetLastError(); ! return Addr(); ! } ! #else ! if( err < 0 ) ! return Addr(); ! #endif ! return Addr( name ); ! } ! else ! return Addr(); } ! bool Socket::connect( const Addr& addr ) { ! if( isOpen() ) ! { ! int err = ::connect( getFD(), ! (const struct sockaddr *)&( addr.getAddr() ), ! sizeof( addr.getAddr() ) ); ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(CYGWIN) ! if( err == SOCKET_ERROR ) ! return false; ! #else ! if( err < 0 ) ! return false; ! #endif ! return true; ! } ! else ! return false; } *************** *** 120,132 **** Socket::getPeer() const { ! Addr::AddrType name; ! socklen_t from_len = sizeof( name ); ! int err = ::getpeername( m_socket, ! (struct sockaddr *)&name, ! &from_len ); ! if( err < 0 ) ! throw GetNameErr( errno ); ! ! return Addr( name ); } --- 187,210 ---- Socket::getPeer() const { ! if( isOpen() ) ! { ! Addr::AddrType name; ! socklen_t from_len = sizeof( name ); ! int err = ::getpeername( getFD(), ! (struct sockaddr *)&name, ! &from_len ); ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(CYGWIN) ! if( err == SOCKET_ERROR ) ! { ! return Addr(); ! } ! #else ! if( err < 0 ) ! return Addr(); ! #endif ! return Addr( name ); ! } ! else ! return Addr(); } *************** *** 134,144 **** Socket::close() { ! if( m_open ) ! { ! m_open = false; ! ::close( m_socket ); ! m_socket = 0; ! } ! m_connected = false; } --- 212,216 ---- Socket::close() { ! m_handle.reset(); } *************** *** 146,151 **** Socket::setCloseOnExec( bool on ) { ! return fcntl( m_socket, F_SETFD, ! ( on ? FD_CLOEXEC : ~FD_CLOEXEC ) ); } --- 218,236 ---- Socket::setCloseOnExec( bool on ) { ! if( isOpen() ) ! { ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! errno = EPERM; ! return -1; ! #else ! return fcntl( getFD(), F_SETFD, ! ( on ? FD_CLOEXEC : ~FD_CLOEXEC ) ); ! #endif ! } ! else ! { ! errno = EPERM; ! return -1; ! } } *************** *** 153,166 **** Socket::setNonBlocking( bool on ) { ! int flags = fcntl( m_socket, F_GETFL, 0 ); ! if( flags == -1 ) ! return flags; ! if( on ) ! return fcntl( m_socket, F_SETFL, ! O_NONBLOCK | flags ); else ! return fcntl( m_socket, F_SETFL, ! ~O_NONBLOCK & flags ); } --- 238,264 ---- Socket::setNonBlocking( bool on ) { ! if( isOpen() ) ! { ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! u_long tmp = on; ! return ioctlsocket( getFD(), FIONBIO, &tmp ); ! #else ! int flags = fcntl( getFD(), F_GETFL, 0 ); ! if( flags == -1 ) ! return flags; ! if( on ) ! return fcntl( getFD(), F_SETFL, ! O_NONBLOCK | flags ); ! else ! return fcntl( getFD(), F_SETFL, ! ~O_NONBLOCK & flags ); ! #endif ! } else ! { ! errno = EPERM; ! return -1; ! } } *************** *** 169,184 **** { #ifdef O_ASYNC ! int flags = fcntl( m_socket, F_GETFL, 0 ); ! if ( on ) ! return fcntl ( m_socket, F_SETFL, O_ASYNC | flags ); ! else ! return fcntl ( m_socket, F_SETFL, ! ~O_ASYNC & flags ); ! #else errno = EPERM; return -1; ! #endif } --- 267,285 ---- { #ifdef O_ASYNC ! if( isOpen() ) ! { ! int flags = fcntl( getFD(), F_GETFL, 0 ); ! if ( on ) ! return fcntl ( getFD(), F_SETFL, O_ASYNC | flags ); ! else ! return fcntl ( getFD(), F_SETFL, ! ~O_ASYNC & flags ); ! } ! #endif errno = EPERM; return -1; ! } *************** *** 187,211 **** { #ifdef SO_BROADCAST ! int ison = on; ! return setsockopt( m_socket, SOL_SOCKET, ! SO_BROADCAST, (void*)&ison, sizeof( int ) ); #else errno = EPERM; return -1; - #endif } ! int Socket::getFD() const ! { return m_socket; } bool Socket::isOpen() const ! { return m_open; } bool Socket::isConnected() const { ! return m_connected; } --- 288,320 ---- { #ifdef SO_BROADCAST ! if( isOpen() ) ! { ! int ison = on; ! return setsockopt( getFD(), SOL_SOCKET, ! SO_BROADCAST, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (const char*)&ison, #else + (void*)&ison, + #endif + sizeof( int ) ); + } + #endif errno = EPERM; return -1; } ! Socket::SocketDesc Socket::getFD() const ! { return ( isOpen() ? *(m_handle.get()) : Socket::INVALIDSOCKET ); } bool Socket::isOpen() const ! { return m_handle.get() != NULL; } bool Socket::isConnected() const { ! return getPeer() != Addr(); } *************** *** 223,227 **** if( check == DONT_CHECK ) { ! return ::sendto( m_socket, msg, len, flags, (struct sockaddr *)&( dest.getAddr() ), sizeof( dest.getAddr() ) ); --- 332,342 ---- if( check == DONT_CHECK ) { ! return ::sendto( getFD(), msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! flags, (struct sockaddr *)&( dest.getAddr() ), sizeof( dest.getAddr() ) ); *************** *** 231,240 **** for(;;) { ! int sent = ::sendto( m_socket, msg, len, flags, (struct sockaddr *)&( dest.getAddr() ), sizeof( dest.getAddr() ) ); if( sent != -1 || ( errno != EINTR ! && errno != EWOULDBLOCK ) ) return sent; } --- 346,366 ---- for(;;) { ! int sent = ::sendto( getFD(), msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! flags, (struct sockaddr *)&( dest.getAddr() ), sizeof( dest.getAddr() ) ); if( sent != -1 || ( errno != EINTR ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! && errno != WSAEWOULDBLOCK ! #else ! && errno != EWOULDBLOCK ! #endif ! ) ) return sent; } *************** *** 250,254 **** if( check == DONT_CHECK ) { ! return ::send( m_socket, msg, len, flags ); } else --- 376,386 ---- if( check == DONT_CHECK ) { ! return ::send( getFD(), msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! flags ); } else *************** *** 256,263 **** for(;;) { ! int sent = ::send( m_socket, msg, len, flags ); if( sent != -1 || ( errno != EINTR ! && errno != EWOULDBLOCK ) ) return sent; } --- 388,406 ---- for(;;) { ! int sent = ::send( getFD(), msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! flags ); if( sent != -1 || ( errno != EINTR ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! && errno != WSAEWOULDBLOCK ! #else ! && errno != EWOULDBLOCK ! #endif ! ) ) return sent; } *************** *** 276,280 **** Addr::AddrType addr; socklen_t from_len = sizeof( addr ); ! int rval = ::recvfrom( m_socket, msg, len, flags, (struct sockaddr *)&addr, &from_len ); from = Addr( addr ); --- 419,429 ---- Addr::AddrType addr; socklen_t from_len = sizeof( addr ); ! int rval = ::recvfrom( getFD(), msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! flags, (struct sockaddr *)&addr, &from_len ); from = Addr( addr ); *************** *** 287,291 **** Addr::AddrType addr; socklen_t from_len = sizeof( addr ); ! int received = ::recvfrom( m_socket, msg, len, flags, (struct sockaddr *)&addr, &from_len ); --- 436,446 ---- Addr::AddrType addr; socklen_t from_len = sizeof( addr ); ! int received = ::recvfrom( getFD(), msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! flags, (struct sockaddr *)&addr, &from_len ); *************** *** 305,314 **** { if( check == DONT_CHECK ) ! return ::recv( m_socket, msg, len, flags ); else { for(;;) { ! int received = ::recv( m_socket, msg, len, flags ); if( received != -1 || errno != EINTR ) --- 460,481 ---- { if( check == DONT_CHECK ) ! return ::recv( getFD(), msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! flags ); else { for(;;) { ! int received = ::recv( getFD(), msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! flags ); if( received != -1 || errno != EINTR ) *************** *** 318,321 **** --- 485,520 ---- } + bool + Socket::listen( int backlog ) + { + return ::listen( getFD(), backlog ) == 0; + } + + bool + Socket::accept( Socket& sock ) + { + SocketDesc fd = ::accept( getFD(), NULL, 0 ); + if( fd == INVALIDSOCKET ) + return false; + sock = TCPSocket( fd ); + return true; + } + + Socket* Socket::accept(Addr& addr) + { + socklen_t len = sizeof(struct sockaddr); + SocketDesc fd = ::accept( getFD(), + (struct sockaddr *)&( addr.getAddr() ), + &len + ); + + if (fd == INVALIDSOCKET ) + { + return 0; + } + + return new TCPSocket(fd); + } + int Socket::recv( int timeout, *************** *** 326,330 **** { #if defined (HAVE_POLL_H) ! pollfd fd = { m_socket, POLLIN | POLLPRI, 0 }; int res = poll( &fd, 1, timeout ); if( res == 0 ) --- 525,529 ---- { #if defined (HAVE_POLL_H) ! pollfd fd = { getFD(), POLLIN | POLLPRI, 0 }; int res = poll( &fd, 1, timeout ); if( res == 0 ) *************** *** 335,339 **** else if( res == 1 ) { ! return recv( msg, len, from, flags ); } else --- 534,544 ---- else if( res == 1 ) { ! return recv( msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! from, flags ); } else *************** *** 354,358 **** { #if defined (HAVE_POLL_H) ! pollfd fd = { m_socket, POLLIN | POLLPRI, 0 }; int res = poll( &fd, 1, timeout ); if( res == 0 ) --- 559,563 ---- { #if defined (HAVE_POLL_H) ! pollfd fd = { getFD(), POLLIN | POLLPRI, 0 }; int res = poll( &fd, 1, timeout ); if( res == 0 ) *************** *** 363,367 **** else if( res == 1 ) { ! return recv( msg, len, flags ); } else --- 568,578 ---- else if( res == 1 ) { ! return recv( msg, ! #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) ! (int)len, ! #else ! len, ! #endif ! flags ); } else Index: socket.hpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/socket.hpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** socket.hpp 19 Dec 2005 19:13:30 -0000 1.1 --- socket.hpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 22,57 **** #define RCSS_NET_SOCKET_HPP #include <boost/cstdint.hpp> #include "addr.hpp" namespace rcss { namespace net { class Socket { public: enum CheckingType { CHECK, DONT_CHECK }; Socket(); virtual ~Socket(); ! void open(); ! void bind( const Addr& addr ); - void - listen( int backlog ); - - virtual Socket* accept(Addr& addr); - Addr getName() const; ! void connect( const Addr& addr ); --- 22,78 ---- #define RCSS_NET_SOCKET_HPP + #ifdef HAVE_CONFIG_H + #include <config.h> + #endif + #include <boost/cstdint.hpp> + #include <boost/shared_ptr.hpp> #include "addr.hpp" + #ifdef HAVE_WINSOCK2_H + #include "Winsock2.h" + #endif + namespace rcss { namespace net { + class Handler; + class Socket { public: + #ifdef HAVE_SOCKET + typedef SOCKET SocketDesc; + #else + typedef int SocketDesc; + #endif + + static const SocketDesc INVALIDSOCKET; + enum CheckingType { CHECK, DONT_CHECK }; + public: + static + void + closeFD( SocketDesc* s ); + Socket(); + Socket( SocketDesc s ); + virtual ~Socket(); ! bool open(); ! bool bind( const Addr& addr ); Addr getName() const; ! bool connect( const Addr& addr ); *************** *** 74,78 **** setBroadcast( bool on = true ); ! int getFD() const; --- 95,99 ---- setBroadcast( bool on = true ); ! SocketDesc getFD() const; *************** *** 112,115 **** --- 133,144 ---- CheckingType check = CHECK ); + bool + accept( Socket& sock ); + + Socket* accept(Addr& addr); + + bool + listen( int backlog ); + // The following two methods allow a timeout to be specified. // Overall, it's slower than the untimed varients so if you do *************** *** 131,147 **** int flags = 0 ); ! protected: virtual ! void ! doOpen( int& fd ) = 0; ! ! // not used ! Socket( const Socket& ); ! Socket& operator=( const Socket& ); ! protected: ! int m_socket; ! bool m_open; ! bool m_connected; }; } --- 160,171 ---- int flags = 0 ); ! private: virtual ! bool ! doOpen( SocketDesc& fd ) = 0; ! private: ! Handler* m_handler; ! boost::shared_ptr< SocketDesc > m_handle; }; } Index: udpsocket.cpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/udpsocket.cpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** udpsocket.cpp 19 Dec 2005 19:13:30 -0000 1.1 --- udpsocket.cpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 2,6 **** /*************************************************************************** ! updsocket.cpp - A simple upd socket class ------------------- begin : 08-JAN-2003 --- 2,6 ---- /*************************************************************************** ! udpsocket.cpp - A simple udp socket class ------------------- begin : 08-JAN-2003 *************** *** 20,29 **** #include "udpsocket.hpp" #include <sys/types.h> #include <sys/socket.h> #include <cerrno> ! #include "exception.hpp" #include <netinet/in.h> namespace rcss --- 20,42 ---- #include "udpsocket.hpp" + + #ifdef HAVE_CONFIG_H + #include "config.h" + #endif + #include <sys/types.h> + #ifdef HAVE_SYS_SOCKET_H #include <sys/socket.h> + #endif #include <cerrno> ! #ifdef HAVE_NETINET_IN_H #include <netinet/in.h> + #endif + + #ifdef HAVE_WINSOCK2_H + #include "Winsock2.h" + #endif + #include <iostream> namespace rcss *************** *** 36,39 **** --- 49,56 ---- } + UDPSocket::UDPSocket( SocketDesc& s ) + : Socket( s ) + {} + UDPSocket::UDPSocket( const Addr& addr ) { *************** *** 45,59 **** { open(); ! bind( addr ); connect( dest ); } ! void ! UDPSocket::doOpen( int& fd ) { close(); fd = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP ); ! if( fd < 0 ) ! throw OpenErr( errno ); } } --- 62,75 ---- { open(); ! bind( addr ); connect( dest ); } ! bool ! UDPSocket::doOpen( SocketDesc& fd ) { close(); fd = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP ); ! return fd >= 0; } } Index: exception.cpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/exception.cpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** exception.cpp 19 Dec 2005 19:13:30 -0000 1.1 --- exception.cpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 25,30 **** --- 25,35 ---- #include "exception.hpp" #include <cstring> + + #ifdef WIN32 + #include <winsock2.h> + #else #include <sys/types.h> #include <netinet/in.h> + #endif #ifdef HAVE_ARPA_NAMESER_H *************** *** 57,61 **** return "Network error"; #else ! return hstrerror( m_err ); #endif } --- 62,66 ---- return "Network error"; #else ! return std::strerror( m_err ); #endif } Index: tcpsocket.hpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/utility/rcssnet/tcpsocket.hpp,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -C2 -d -r1.1 -r1.1.2.1 *** tcpsocket.hpp 19 Dec 2005 19:13:30 -0000 1.1 --- tcpsocket.hpp 9 Feb 2007 20:09:32 -0000 1.1.2.1 *************** *** 1,23 **** ! /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- ! this file is part... [truncated message content] |