- Main Page
- Classes
- Files

- Search for |

# sockwrap.h

    00001 /*****************************
    00002 File:      sockwrap.h
    00003 Language:   C++ (header)
    00004 Project:    H3DNetworkingUtils
    00005 The contents of this file are subject to the Mozilla Public License
    00006 Version 1.1 (the "License"); you may not use this file except in
    00007 compliance with the License. You may obtain a copy of the License at
    00008 http://www.mozilla.org/MPL/
    00009
    00010 Software distributed under the License is distributed on an "AS IS"
    00011 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
    00012 License for the specific language governing rights and limitations
    00013 under the License.
    00014
    00015 The Original Code is H3DNetworkingUtils v1.0.
    00016
    00017 The Initial Developer of the Original Code is CSIRO.
    00018 Portions created by the Initial Developer are Copyright (C) 1009 CSIRO. All Rights Reserved.
    00019 Contributor(s):
    00020     Chris Gunn  <Chris.Gunn@csiro.au> <ChrisJGunn@gmail.com>
    00021 ***************************/
    00022
    00023 // The sockwrap class provides a class wrapper for socket communications.
    00024
    00025 #ifndef _sockwrap_
    00026 #define _sockwrap_
    00027
    00028 /* Distributed under a BSD type license. See the netwrap COPYRIGHT file */
    00029
    00030 #include "H3DNetworkingUtils/inetaddr.h"
    00031 #include "H3DNetworkingUtils/Config.h"
    00032
    00033 #ifndef SHUT_RD
    00034 #define SHUT_RD   0
    00035 #define SHUT_WR     1
    00036 #define SHUT_RDWR 2
    00037 #endif
    00038
    00039 //****    Abstract socket class, all inherit from
    00040
    00041 class H3D_NETWORKING_UTILS_DLL_SPEC  CoreSock {
    00042   public:
    00043     CoreSock ();
    00044     virtual ~CoreSock ();
    00045
    00046     // No operations to open: depends on subclass
    00047     virtual void close ();
    00048
    00049     // Binary IO
    00050     virtual int read (void * dest, int max);
    00051     virtual int readLine (char * line, int max);
    00052
    00053     // Text IO
    00054     virtual int write (void * source, int count);
    00055     virtual int writeLine (char * line);
    00056
    00057 };
    00058
    00059 //****   Common code for UDP and TCP sockets
    00060
    00061 class H3D_NETWORKING_UTILS_DLL_SPEC  InetSock : public CoreSock {
    00062   protected:
    00063     int     sock;
    00064     int     kind; // STREAM or DGRAM
    00065     InetAddr local, remote;
    00066
    00067   public:
    00068     InetSock (int kind, int fd);
    00069     virtual ~InetSock ();
    00070
    00071     virtual void close ();
    00072
    00073     // Client connect
    00074     virtual int connect (InetAddr * endpoint);
    00075
    00076     // For servers. Queue length is meaningless for DGRAM sockets
    00077     // and has one special value "don't listen" for STREAMs
    00078     virtual int bind (InetAddr * endpoint, int qLength = 4);
    00079     enum { ActiveBind = -1 };
    00080
    00081     // Attributes
    00082     void * sysRef ()  { return (void *)sock; };
    00083
    00084     virtual InetAddr * localAddress ();
    00085     virtual InetAddr * remoteAddress ();
    00086
    00087     virtual int isUDP ();
    00088     virtual int isTCP ();
    00089
    00090     bool dataReady();
    00091     // Returns true if there is data on the socket ready for reading.
    00092 };
    00093
    00094 //****   UDP datagram socket  ****
    00095
    00096 class H3D_NETWORKING_UTILS_DLL_SPEC  UDPSock : public InetSock {
    00097   protected:
    00098     int     ioWait;  // In milliseconds. Default zero = forever
    00099
    00100   public:
    00101     UDPSock ();
    00102     virtual ~UDPSock ();
    00103
    00104     // Remember that these won't return EOF for unconnected
    00105     virtual int read (void * dest, int max);
    00106     virtual int write (void * source, int count);
    00107
    00108     // Needed by servers which accept incoming from anyone.
    00109     // Typically address filled in by receive is used for send.
    00110     virtual int receive (void * dest, int max, InetAddr * sender);
    00111     virtual int send (void * source, int count, InetAddr * target);
    00112
    00113     // Set time out for read (useful) and write (less so)
    00114     virtual void setTimeOut (int millisecs);
    00115     virtual int  timeOut ();
    00116
    00117 };
    00118
    00119 //****   TCP stream socket
    00120
    00121 class H3D_NETWORKING_UTILS_DLL_SPEC  TCPSock : public InetSock {
    00122   protected:
    00123     // Needed for thread-safe readline
    00124     char * readBuf;  // Allocated MaxLineSize
    00125     char * bufPtr;
    00126     int     bufCount;
    00127
    00128     int nextChar (char * c);
    00129
    00130   public:
    00131     TCPSock (int fd); // Need to create from existing socket on accept
    00132     virtual ~TCPSock ();
    00133
    00134     enum { MaxLineSize = 256 };
    00135
    00136     virtual TCPSock * accept ();
    00137
    00138     virtual void shutdown (int direction);
    00139
    00140     virtual int read (void * dest, int max);
    00141     virtual int readLine (char * line, int max);
    00142
    00143     virtual int write (void * source, int count);
    00144     virtual int writeLine (char * line);
    00145
    00146 };
    00147
    00148 // Next version: add a file socket that is created from two
    00149 // stdio streams so we can simulate network traffic from disk.
    00150
    00151 #endif