PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ClassMaster2014/barahon/Transcend_0.3_UnixSource/minorGems/network/SocketStream.h

https://gitlab.com/garheade/linux_camp
C Header | 168 lines | 54 code | 57 blank | 57 comment | 6 complexity | fce3ceede04edfbdd1792bd3442484dc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Modification History
  3. *
  4. * 2001-January-10 Jason Rohrer
  5. * Created.
  6. *
  7. * 2001-January-15 Jason Rohrer
  8. * Changed so that underlying socket is not destroyed when the stream
  9. * is destroyed. Updated to match new Stream interface, which uses longs
  10. * instead of ints.
  11. *
  12. * 2001-February-21 Jason Rohrer
  13. * Changed to set stream error messages on socket errors.
  14. *
  15. * 2002-July-31 Jason Rohrer
  16. * Added fix for partial sends.
  17. *
  18. * 2003-August-12 Jason Rohrer
  19. * Added support for read timeouts.
  20. *
  21. * 2004-January-27 Jason Rohrer
  22. * Made functions virtual to support subclassing.
  23. */
  24. #include "minorGems/common.h"
  25. #ifndef SOCKET_STREAM_CLASS_INCLUDED
  26. #define SOCKET_STREAM_CLASS_INCLUDED
  27. #include "Socket.h"
  28. #include "minorGems/io/InputStream.h"
  29. #include "minorGems/io/OutputStream.h"
  30. /**
  31. * A input and output stream interface for a network socket.
  32. *
  33. * @author Jason Rohrer
  34. */
  35. class SocketStream : public InputStream, public OutputStream {
  36. public:
  37. /**
  38. * Constructs a SocketStream.
  39. *
  40. * @param inSocket the newtork socket wrapped by this stream.
  41. * inSocket is NOT destroyed when the stream is destroyed.
  42. */
  43. SocketStream( Socket *inSocket );
  44. // a virtual destructor to ensure that subclass destructors are called
  45. virtual ~SocketStream();
  46. /**
  47. * Sets the timeout for reads on this socket.
  48. *
  49. * The timeout defaults to -1 (no timeout).
  50. *
  51. * @param inMilliseconds the timeout in milliseconds,
  52. * or -1 to specify no timeout.
  53. */
  54. void setReadTimeout( long inMilliseconds );
  55. /**
  56. * Gets the timeout for reads on this socket.
  57. *
  58. * @return the timeout in milliseconds,
  59. * or -1 to indicate no timeout.
  60. */
  61. long getReadTimeout();
  62. // implements the InputStream interface.
  63. // virtual to allow subclasses to override
  64. // in addition, -2 is returned if the read times out.
  65. virtual long read( unsigned char *inBuffer, long inNumBytes );
  66. // implements the OutputStream interface
  67. virtual long write( unsigned char *inBuffer, long inNumBytes );
  68. protected:
  69. Socket *mSocket;
  70. long mReadTimeout;
  71. };
  72. inline SocketStream::SocketStream( Socket *inSocket )
  73. : mSocket( inSocket ),
  74. mReadTimeout( -1 ) {
  75. }
  76. inline SocketStream::~SocketStream() {
  77. // does nothing
  78. // exists only to allow for subclass destructors
  79. }
  80. inline void SocketStream::setReadTimeout( long inMilliseconds ) {
  81. mReadTimeout = inMilliseconds;
  82. }
  83. inline long SocketStream::getReadTimeout() {
  84. return mReadTimeout;
  85. }
  86. inline long SocketStream::read( unsigned char *inBuffer, long inNumBytes ) {
  87. int numReceived = mSocket->receive( inBuffer, inNumBytes, mReadTimeout );
  88. if( numReceived == -1 ) {
  89. // socket error
  90. InputStream::setNewLastErrorConst(
  91. "Network socket error on receive." );
  92. }
  93. return numReceived;
  94. }
  95. inline long SocketStream::write( unsigned char *inBuffer, long inNumBytes ) {
  96. long numTotalSent = 0;
  97. while( numTotalSent < inNumBytes ) {
  98. unsigned char *partialBuffer = &( inBuffer[numTotalSent] );
  99. int numRemaining = inNumBytes - numTotalSent;
  100. int numSent = mSocket->send( partialBuffer, numRemaining );
  101. if( numSent == -1 || numSent == 0 ) {
  102. // socket error
  103. OutputStream::setNewLastErrorConst(
  104. "Network socket error on send." );
  105. return -1;
  106. }
  107. numTotalSent += numSent;
  108. }
  109. return numTotalSent;
  110. }
  111. #endif