1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/*
* Modification History
*
* 2001-January-10 Jason Rohrer
* Created.
*
* 2001-January-15 Jason Rohrer
* Changed so that underlying socket is not destroyed when the stream
* is destroyed. Updated to match new Stream interface, which uses longs
* instead of ints.
*
* 2001-February-21 Jason Rohrer
* Changed to set stream error messages on socket errors.
*
* 2002-July-31 Jason Rohrer
* Added fix for partial sends.
*
* 2003-August-12 Jason Rohrer
* Added support for read timeouts.
*
* 2004-January-27 Jason Rohrer
* Made functions virtual to support subclassing.
*/
#include "minorGems/common.h"
#ifndef SOCKET_STREAM_CLASS_INCLUDED
#define SOCKET_STREAM_CLASS_INCLUDED
#include "Socket.h"
#include "minorGems/io/InputStream.h"
#include "minorGems/io/OutputStream.h"
/**
* A input and output stream interface for a network socket.
*
* @author Jason Rohrer
*/
class SocketStream : public InputStream, public OutputStream {
public:
/**
* Constructs a SocketStream.
*
* @param inSocket the newtork socket wrapped by this stream.
* inSocket is NOT destroyed when the stream is destroyed.
*/
SocketStream( Socket *inSocket );
// a virtual destructor to ensure that subclass destructors are called
virtual ~SocketStream();
/**
* Sets the timeout for reads on this socket.
*
* The timeout defaults to -1 (no timeout).
*
* @param inMilliseconds the timeout in milliseconds,
* or -1 to specify no timeout.
*/
void setReadTimeout( long inMilliseconds );
/**
* Gets the timeout for reads on this socket.
*
* @return the timeout in milliseconds,
* or -1 to indicate no timeout.
*/
long getReadTimeout();
// implements the InputStream interface.
// virtual to allow subclasses to override
// in addition, -2 is returned if the read times out.
virtual long read( unsigned char *inBuffer, long inNumBytes );
// implements the OutputStream interface
virtual long write( unsigned char *inBuffer, long inNumBytes );
protected:
Socket *mSocket;
long mReadTimeout;
};
inline SocketStream::SocketStream( Socket *inSocket )
: mSocket( inSocket ),
mReadTimeout( -1 ) {
}
inline SocketStream::~SocketStream() {
// does nothing
// exists only to allow for subclass destructors
}
inline void SocketStream::setReadTimeout( long inMilliseconds ) {
mReadTimeout = inMilliseconds;
}
inline long SocketStream::getReadTimeout() {
return mReadTimeout;
}
inline long SocketStream::read( unsigned char *inBuffer, long inNumBytes ) {
int numReceived = mSocket->receive( inBuffer, inNumBytes, mReadTimeout );
if( numReceived == -1 ) {
// socket error
InputStream::setNewLastErrorConst(
"Network socket error on receive." );
}
return numReceived;
}
inline long SocketStream::write( unsigned char *inBuffer, long inNumBytes ) {
long numTotalSent = 0;
while( numTotalSent < inNumBytes ) {
unsigned char *partialBuffer = &( inBuffer[numTotalSent] );
int numRemaining = inNumBytes - numTotalSent;
int numSent = mSocket->send( partialBuffer, numRemaining );
if( numSent == -1 || numSent == 0 ) {
// socket error
OutputStream::setNewLastErrorConst(
"Network socket error on send." );
return -1;
}
numTotalSent += numSent;
}
return numTotalSent;
}
#endif
|