Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGStreamSocket.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042 
00043 #ifdef WIN32
00044 #include <windows.h>
00045 #include <io.h>
00046 #else
00047 #include <sys/types.h>
00048 #include <sys/socket.h>
00049 #include <netinet/in.h>
00050 #include <netinet/tcp.h>
00051 #include <arpa/inet.h>
00052 #include <netdb.h>
00053 #include <unistd.h>
00054 #include <fcntl.h>
00055 #include <sys/time.h>
00056 #endif
00057 
00058 #include <errno.h>
00059 #include <stdio.h>
00060 #include <math.h>
00061 #include <map>
00062 #include <OSGBase.h>
00063 #include <OSGSocketAddress.h>
00064 #include <OSGStreamSocket.h>
00065 
00066 OSG_USING_NAMESPACE
00067 
00099 /*-------------------------------------------------------------------------*/
00100 /*                            constructor destructor                       */
00101 
00106 StreamSocket::StreamSocket():
00107     Socket()
00108 {
00109 }
00110 
00113 StreamSocket::StreamSocket(const StreamSocket &source):
00114     Socket(source)
00115 {
00116 }
00117 
00118 /*-------------------------------------------------------------------------*/
00119 /*                           Socket functionaliy                           */
00120 
00125 void StreamSocket::open()
00126 {
00127     _sd = ::socket(AF_INET, SOCK_STREAM, 0);
00128     if(_sd < 0)
00129     {
00130         throw SocketError("socket()");
00131     }
00132     struct linger li;
00133     li.l_onoff = 1;
00134     li.l_linger = 1;
00135     int rc = setsockopt(_sd, SOL_SOCKET, SO_LINGER, 
00136                         (SocketOptT*)&li, sizeof(li));
00137 }
00138 
00141 void StreamSocket::close(void)
00142 {
00143 #ifdef WIN32
00144     ::closesocket(_sd);
00145 #else
00146     ::close(_sd);
00147 #endif
00148 }
00149 
00155 StreamSocket StreamSocket::acceptFrom(SocketAddress &address)
00156 {
00157     StreamSocket client;
00158     SocketLenT len;
00159     
00160     len=address.getSockAddrSize();
00161     client._sd=::accept(_sd,
00162                         address.getSockAddr(),
00163                         &len);
00164     if(client._sd < 0)
00165     {
00166         throw SocketError("accept()");
00167     }
00168     return client;
00169 }
00170 
00175 StreamSocket StreamSocket::accept()
00176 {
00177     SocketAddress addr;
00178     return acceptFrom(addr);
00179 }
00180 
00185 void StreamSocket::setDelay(bool value)
00186 {
00187     int rc,on;
00188     on=!value;
00189     rc=setsockopt(_sd, IPPROTO_TCP, TCP_NODELAY, 
00190                   (SocketOptT*)&on, sizeof(on));
00191     if(rc < 0)
00192     {
00193         throw SocketError("setsockopt(,SOCK_STREAM,TCP_NODELAY)");
00194     }
00195 }
00196 
00197 /*-------------------------------------------------------------------------*/
00198 /*                              assignment                                 */
00199 
00202 const StreamSocket & StreamSocket::operator =(const StreamSocket &source)
00203 {
00204     _sd=source._sd;
00205     return *this;
00206 }
00207 
00208 /*-------------------------------------------------------------------------*/
00209 /*                              cvs id's                                   */
00210 
00211 #ifdef __sgi
00212 #pragma set woff 1174
00213 #endif
00214 
00215 #ifdef OSG_LINUX_ICC
00216 #pragma warning( disable : 177 )
00217 #endif
00218 
00219 namespace
00220 {
00221     static Char8 cvsid_cpp       [] = "@(#)$Id: $";
00222     static Char8 cvsid_hpp       [] = OSG_STREAMSOCKET_HEADER_CVSID;
00223 }

Generated on Thu Aug 25 04:11:05 2005 for OpenSG by  doxygen 1.4.3