From: Markus R. <rol...@us...> - 2005-12-19 19:13:38
|
Update of /cvsroot/simspark/simspark/spark/utility/rcssnet In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24814/rcssnet Added Files: .cvsignore Makefile.am README addr.cpp addr.hpp exception.cpp exception.hpp iosocketstream.hpp isocketstream.hpp osocketstream.hpp socket.cpp socket.hpp socketstreambuf.hpp tcpsocket.cpp tcpsocket.hpp udpsocket.cpp udpsocket.hpp Log Message: - added spark utility library --- NEW FILE: exception.hpp --- // -*-c++-*- /*************************************************************************** exception.hpp - Network associated excpetions ------------------- begin : 07-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. * * * ***************************************************************************/ #ifndef RCSS_NET_EXCPETION_HPP #define RCSS_NET_EXCEPTION_HPP #include <exception> namespace rcss { namespace net { class HostNotFound : public std::exception { public: HostNotFound( int err ) throw(); const char* what() const throw(); int err() const throw(); private: int m_err; }; class OpenErr : public std::exception { public: OpenErr( int err ) throw(); const char* what() const throw(); int err() const throw(); private: int m_err; }; class BindErr : public std::exception { public: BindErr( int err ) throw(); const char* what() const throw(); int err() const throw(); private: int m_err; }; class ListenErr : public std::exception { public: ListenErr( int err ) throw(); const char* what() const throw(); int err() const throw(); private: int m_err; }; class AcceptErr : public std::exception { public: AcceptErr( int err ) throw(); const char* what() const throw(); int err() const throw(); private: int m_err; }; class GetNameErr : public std::exception { public: GetNameErr( int err ) throw(); const char* what() const throw(); int err() const throw(); private: int m_err; }; class ConnectErr : public std::exception { public: ConnectErr( int err ) throw(); const char* what() const throw(); int err() const throw(); private: int m_err; }; } } #endif --- NEW FILE: udpsocket.hpp --- // -*-c++-*- /*************************************************************************** updsocket.hpp - A simple upd 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. * * * ***************************************************************************/ #ifndef RCSS_NET_UDPSOCKET_HPP #define RCSS_NET_UDPSOCKET_HPP #include "socket.hpp" namespace rcss { namespace net { class UDPSocket : public Socket { public: UDPSocket(); UDPSocket( const Addr& addr ); UDPSocket( const Addr& addr, const Addr& dest ); protected: virtual void doOpen( int& fd ); }; } } #endif --- NEW FILE: Makefile.am --- pkglib_LTLIBRARIES = librcssnet3D.la librcssnet3D_la_SOURCES = \ addr.cpp \ exception.cpp \ socket.cpp \ tcpsocket.cpp \ udpsocket.cpp libpkginclude_HEADERS = \ addr.hpp \ exception.hpp \ iosocketstream.hpp \ isocketstream.hpp \ osocketstream.hpp \ socket.hpp \ socketstreambuf.hpp \ tcpsocket.hpp \ udpsocket.hpp librcssnet3D_la_LIBADD = @NET_LIBS@ librcssnet3D_la_LDFLAGS = -version-info @rcssnet3D_version_info@ ## define include directory local to the pkgincludedir libpkgincludedir = $(includedir)/@PACKAGE@/rcssnet EXTRA_DIST = README --- NEW FILE: socketstreambuf.hpp --- // -*-c++-*- /*************************************************************************** socketstreambuf.hpp - A socket stream buffer ------------------- 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. * * * ***************************************************************************/ #ifndef RCSS_NET_SOCKETSTREAMBUF_HPP #define RCSS_NET_SOCKETSTREAMBUF_HPP //g++ 2.95.6 doesn't have the streambuf header, so iostream is used instead //#include <streambuf> #include<iostream> #include "socket.hpp" namespace rcss { namespace net { class SocketStreamBuf : public std::streambuf { public: typedef int int_type; typedef char char_type; enum ConnType { CONN_ON_READ, NO_CONN }; public: SocketStreamBuf( Socket& socket, const Addr& dest, ConnType conn = CONN_ON_READ, std::streamsize bufsize = 8192 ) : m_socket( socket ), m_end_point( dest ), m_bufsize( bufsize ), m_inbuf( NULL ), m_outbuf( NULL ), m_remained( 0 ), m_connect( conn ) {} SocketStreamBuf( Socket& socket, ConnType conn = NO_CONN, std::streamsize bufsize = 8192 ) : m_socket( socket ), m_bufsize( bufsize ), m_inbuf( NULL ), m_outbuf( NULL ), m_remained( 0 ), m_connect( conn ) {} virtual ~SocketStreamBuf() { overflow(); delete [] m_inbuf; 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; if( m_socket.isConnected() ) return m_socket.send( m_outbuf, size ) > 0; else return m_socket.send( m_outbuf, size, m_end_point ) > 0; } virtual int_type overflow( int_type c = EOF ) { // 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 if( pptr() == NULL ) { m_outbuf = new char_type[m_bufsize]; } else { if( !writeData() ) { return EOF; } } setp( m_outbuf, m_outbuf + m_bufsize ); if( c != EOF ) sputc( c ); return 0; } virtual int sync() { if( pptr() != NULL ) { // just flush the put area if( !writeData() ) { return EOF; } setp( m_outbuf, m_outbuf + m_bufsize ); } 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 if( gptr() == NULL ) { 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, readn ); else { Addr addr; readn = m_socket.recv( m_inbuf + m_remained, readn, addr ); if( m_connect == CONN_ON_READ && readn > 0 ) m_socket.connect( addr ); } if( readn < 0 || readn == 1 && (m_inbuf + m_remained)[ 0 ] == -1 ) { (m_inbuf + m_remained)[ 0 ] = -1; return EOF; } int totalbytes = readn + m_remained; 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; std::streamsize m_bufsize; char_type* m_inbuf; char_type* m_outbuf; int m_remained; char_type m_remained_char; ConnType m_connect; }; } } #endif --- NEW FILE: tcpsocket.cpp --- /* -*- 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: tcpsocket.cpp,v 1.1 2005/12/19 19:13:30 rollmark Exp $ 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 { namespace net { TCPSocket::TCPSocket() { open(); } TCPSocket::TCPSocket( const Addr& addr ) { open(); bind( addr ); } TCPSocket::TCPSocket( const Addr& addr, const Addr& dest ) { 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); } } } --- NEW FILE: .cvsignore --- *.lo .deps .libs Makefile Makefile.in librcssnet3D.la --- NEW FILE: addr.cpp --- // -*-c++-*- /*************************************************************************** addr.cpp - A network address class ------------------- begin : 07-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 "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 { namespace net { const Addr::HostType Addr::BROADCAST = INADDR_BROADCAST; const Addr::HostType Addr::ANY = INADDR_ANY; 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 = (struct hostent*)gethostbyname( host.c_str() ); if( host_ent == NULL ) throw HostNotFound( h_errno ); memset( (char *)&m_addr, 0, sizeof( m_addr ) ) ; m_addr.sin_family = AF_INET ; m_addr.sin_addr.s_addr = ((struct in_addr *)host_ent->h_addr_list[0])->s_addr; m_addr.sin_port = htons( port ); } const Addr::AddrType& getAddr() const { return m_addr; } Addr::PortType getPort() const { return ntohs( m_addr.sin_port ); } void setPort(Addr::PortType port) { m_addr.sin_port = htons(port); } Addr::HostType getHost() const { return htonl( m_addr.sin_addr.s_addr ); } std::string getHostStr() const { if( m_host_name.empty() ) { m_host_name = inet_ntoa( m_addr.sin_addr ); } 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 ) : m_impl( new AddrImpl( port, host ) ) {} const Addr::AddrType& Addr::getAddr() const { return m_impl->getAddr(); } Addr::PortType Addr::getPort() const { return m_impl->getPort(); } void Addr::setPort(PortType port) { m_impl->setPort(port); } Addr::HostType Addr::getHost() const { return m_impl->getHost(); } std::string Addr::getHostStr() const { 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) { return host_a < host_b; } return getPort() < addr.getPort(); } std::ostream& operator<<( std::ostream& o, const rcss::net::Addr& addr ) { return o << '(' << addr.getPort() << ':' << addr.getHostStr() << ')'; } } } --- NEW FILE: addr.hpp --- // -*-c++-*- /*************************************************************************** addr.hpp - A network address class ------------------- begin : 07-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. * * * ***************************************************************************/ #ifndef RCSS_NET_ADDR_HPP #define RCSS_NET_ADDR_HPP struct sockaddr_in; #include <string> #include <boost/shared_ptr.hpp> #include <boost/cstdint.hpp> namespace rcss { namespace net { class AddrImpl; class Addr { public: typedef boost::uint16_t PortType; typedef boost::uint32_t HostType; 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 ); } } #endif --- NEW FILE: iosocketstream.hpp --- // -*-c++-*- /*************************************************************************** iosocketstream.hpp - An iostream for sockets ------------------- 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. * * * ***************************************************************************/ #ifndef RCSS_NET_IOSOCKETSTREAM_HPP #define RCSS_NET_IOSOCKETSTREAM_HPP #include "socketstreambuf.hpp" #include "isocketstream.hpp" #include "osocketstream.hpp" namespace rcss { namespace net { class IOSocketStream : public SocketStreamBuf, public std::iostream { // The IOSocketStream can be used in threads, however if a read // blocks waiting for input, then the sending at the same time // will also block. If you want to do this then you will need to // either protect the stream with a mutex or create a // ISocketStream for reading and a OSocketStream for // writing. You can pass the same socket to both to have the // reading and writing performed by the same socket. public: IOSocketStream( Socket& socket, const Addr& dest, ConnType conn = CONN_ON_READ, int buffer_size = 8192 ) : SocketStreamBuf( socket, dest, conn, buffer_size ), std::iostream( this ) {} IOSocketStream( Socket& socket, ConnType conn = NO_CONN, int buffer_size = 8192 ) : SocketStreamBuf( socket, conn, buffer_size ), std::iostream( this ) {} private: // not for use IOSocketStream( const IOSocketStream& ); IOSocketStream& operator=( const IOSocketStream& ); }; } } #endif --- NEW FILE: socket.cpp --- /*************************************************************************** socket.cpp - Base newtork 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 "socket.hpp" #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <fcntl.h> #include <cerrno> #if defined (HAVE_POLL_H) #include <poll.h> #endif #include "exception.hpp" #ifndef HAVE_SOCKLEN_T typedef int socklen_t; #endif namespace rcss { 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 ) { 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; } Addr 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; } Addr 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 ); } void Socket::close() { if( m_open ) { m_open = false; ::close( m_socket ); m_socket = 0; } m_connected = false; } int Socket::setCloseOnExec( bool on ) { return fcntl( m_socket, F_SETFD, ( on ? FD_CLOEXEC : ~FD_CLOEXEC ) ); } int 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 ); } int Socket::setAsync( bool on ) { #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 } int Socket::setBroadcast( bool on ) { #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; } Addr Socket::getDest() const { return getPeer(); } int Socket::send( const char* msg, size_t len, const Addr& dest, int flags, CheckingType check ) { if( check == DONT_CHECK ) { return ::sendto( m_socket, msg, len, flags, (struct sockaddr *)&( dest.getAddr() ), sizeof( dest.getAddr() ) ); } else { 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; } } } int Socket::send( const char* msg, size_t len, int flags, CheckingType check ) { if( check == DONT_CHECK ) { return ::send( m_socket, msg, len, flags ); } else { for(;;) { int sent = ::send( m_socket, msg, len, flags ); if( sent != -1 || ( errno != EINTR && errno != EWOULDBLOCK ) ) return sent; } } } int Socket::recv( char* msg, size_t len, Addr& from, int flags, CheckingType check ) { if( check == DONT_CHECK ) { 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 ); return rval; } else { for(;;) { Addr::AddrType addr; socklen_t from_len = sizeof( addr ); int received = ::recvfrom( m_socket, msg, len, flags, (struct sockaddr *)&addr, &from_len ); from = Addr( addr ); if( received != -1 || errno != EINTR ) return received; } } } int Socket::recv( char* msg, size_t len, int flags, CheckingType check ) { 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 ) return received; } } } int Socket::recv( int timeout, char* msg, size_t len, Addr& from, int flags ) { #if defined (HAVE_POLL_H) pollfd fd = { m_socket, POLLIN | POLLPRI, 0 }; int res = poll( &fd, 1, timeout ); if( res == 0 ) { errno = EAGAIN; return -1; } else if( res == 1 ) { return recv( msg, len, from, flags ); } else { return res; } #else errno = EPERM; return -1; #endif } int Socket::recv( int timeout, char* msg, size_t len, int flags ) { #if defined (HAVE_POLL_H) pollfd fd = { m_socket, POLLIN | POLLPRI, 0 }; int res = poll( &fd, 1, timeout ); if( res == 0 ) { errno = EAGAIN; return -1; } else if( res == 1 ) { return recv( msg, len, flags ); } else { return res; } #else errno = EPERM; return -1; #endif } } } --- NEW FILE: socket.hpp --- // -*-c++-*- /*************************************************************************** socket.hpp - Base newtork 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. * * * ***************************************************************************/ #ifndef RCSS_NET_SOCKET_HPP #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 ); Addr getPeer() const; void close(); int setCloseOnExec( bool on = true ); int setNonBlocking( bool on = true ); int setAsync( bool on = true ); int setBroadcast( bool on = true ); int getFD() const; bool isOpen() const; bool isConnected() const; Addr getDest() const; // deprecated. Use getPeer instead. int send( const char* msg, size_t len, const Addr& dest, int flags = 0, CheckingType check = CHECK ); int send( const char* msg, size_t len, int flags = 0, CheckingType check = CHECK ); int recv( char* msg, size_t len, Addr& from, int flags = 0, CheckingType check = CHECK ); int recv( char* msg, size_t len, int flags = 0, CheckingType check = CHECK ); // The following two methods allow a timeout to be specified. // Overall, it's slower than the untimed varients so if you do // need to specify a timeout and you just want it the recv to // block or not to block, then you are better off setting the // socket to blocking or non-blocking and using the version // without timeouts. int recv( int timeout, char* msg, size_t len, Addr& from, int flags = 0 ); int recv( int timeout, char* msg, size_t len, 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; }; } } #endif --- NEW FILE: udpsocket.cpp --- // -*-c++-*- /*************************************************************************** updsocket.cpp - A simple upd 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 "udpsocket.hpp" #include <sys/types.h> #include <sys/socket.h> #include <cerrno> #include "exception.hpp" #include <netinet/in.h> namespace rcss { namespace net { UDPSocket::UDPSocket() { open(); } UDPSocket::UDPSocket( const Addr& addr ) { open(); bind( addr ); } UDPSocket::UDPSocket( const Addr& addr, const Addr& dest ) { 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 ); } } } --- NEW FILE: exception.cpp --- // -*-c++-*- /*************************************************************************** exception.cpp - Network associated excpetions ------------------- begin : 07-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. * * * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "exception.hpp" #include <cstring> #include <sys/types.h> #include <netinet/in.h> #ifdef HAVE_ARPA_NAMESER_H #include <arpa/nameser.h> #endif #ifdef HAVE_NETDB_H #include <netdb.h> #endif #ifdef HAVE_RESOLV_H #include <resolv.h> #endif namespace rcss { namespace net { HostNotFound::HostNotFound( int err ) throw() : m_err( err ) {} const char* HostNotFound::what() const throw() { #if defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) return "Network error"; #else return hstrerror( m_err ); #endif } int HostNotFound::err() const throw() { return m_err; } OpenErr::OpenErr( int err ) throw() : m_err( err ) {} const char* OpenErr::what() const throw() { return std::strerror( m_err ); } int OpenErr::err() const throw() { return m_err; } BindErr::BindErr( int err ) throw() : m_err( err ) {} const char* BindErr::what() const throw() { return strerror( m_err ); } int BindErr::err() const throw() { return m_err; } ListenErr::ListenErr( int err ) throw() : m_err( err ) {} const char* ListenErr::what() const throw() { return strerror( m_err ); } int ListenErr::err() const throw() { return m_err; } AcceptErr::AcceptErr( int err ) throw() : m_err( err ) {} const char* AcceptErr::what() const throw() { return strerror( m_err ); } int AcceptErr::err() const throw() { return m_err; } GetNameErr::GetNameErr( int err ) throw() : m_err( err ) {} const char* GetNameErr::what() const throw() { return strerror( m_err ); } int GetNameErr::err() const throw() { return m_err; } ConnectErr::ConnectErr( int err ) throw() : m_err( err ) {} const char* ConnectErr::what() const throw() { return strerror( m_err ); } int ConnectErr::err() const throw() { return m_err; } } } --- NEW FILE: tcpsocket.hpp --- /* -*- 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: tcpsocket.hpp,v 1.1 2005/12/19 19:13:30 rollmark Exp $ 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. */ #ifndef RCSS_NET_TCPSOCKET_HPP #define RCSS_NET_TCPSOCKET_HPP #include "socket.hpp" namespace rcss { namespace net { class TCPSocket : public Socket { public: TCPSocket(); TCPSocket( const Addr& addr ); TCPSocket( const Addr& addr, const Addr& dest ); virtual Socket* accept(Addr& addr); protected: TCPSocket( int socket ); virtual void doOpen( int& fd ); }; } } #endif // RCSS_NET_TCPSOCKET_HPP --- NEW FILE: README --- The files for this library have been taken from the rcssbase package (9.4.5) that can be found in the soccer server repository (www.sf.net/projects/sserver). Additionally, some files have been added to complete the library. This code has been ripped from the rcssbase library so that rcssserver3D users don't need to install so many libraries. Some features were missing (like a tcpsocket), which also could have been added to rcssbase. This way was easier for us (and probably also for most of the users). --- NEW FILE: osocketstream.hpp --- // -*-c++-*- /*************************************************************************** osocketstream.hpp - An ostream for sockets ------------------- 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. * * * ***************************************************************************/ #ifndef RCSS_NET_OSOCKETSTREAM_HPP #define RCSS_NET_OSOCKETSTREAM_HPP #include "socketstreambuf.hpp" namespace rcss { namespace net { class OSocketStream : public SocketStreamBuf, public std::ostream { public: OSocketStream( Socket& socket, const Addr& dest, int buffer_size = 8192 ) : SocketStreamBuf( socket, dest, NO_CONN, buffer_size ), std::ostream( this ) {} OSocketStream( Socket& socket, int buffer_size = 8192 ) : SocketStreamBuf( socket, NO_CONN, buffer_size ), std::ostream( this ) {} private: // not for use OSocketStream(const OSocketStream&); OSocketStream& operator=(const OSocketStream&); }; } } #endif --- NEW FILE: isocketstream.hpp --- // -*-c++-*- /*************************************************************************** isocketstream.hpp - An istream for sockets ------------------- 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. * * * ***************************************************************************/ #ifndef RCSS_NET_ISOCKETSTREAM_HPP #define RCSS_NET_ISOCKETSTREAM_HPP #include "socketstreambuf.hpp" namespace rcss { namespace net { class ISocketStream : public SocketStreamBuf, public std::istream { public: ISocketStream( Socket& socket, const Addr& dest, ConnType conn = CONN_ON_READ, int buffer_size = 8192 ) : SocketStreamBuf( socket, dest, conn, buffer_size ), std::istream( this ) {} ISocketStream( Socket& socket, ConnType conn = NO_CONN, int buffer_size = 8192 ) : SocketStreamBuf( socket, conn, buffer_size ), std::istream( this ) {} private: // not for use ISocketStream(const ISocketStream&); ISocketStream& operator=(const ISocketStream&); }; } } #endif |