TCPSocketDataIO.h

00001 /* This file is Copyright 2000-2008 Meyer Sound Laboratories Inc.  See the included LICENSE.txt file for details. */
00002 
00003 #ifndef MuscleTCPSocketDataIO_h
00004 #define MuscleTCPSocketDataIO_h
00005 
00006 #include "support/MuscleSupport.h"
00007 
00008 #include "dataio/DataIO.h"
00009 #include "util/NetworkUtilityFunctions.h"
00010 
00011 BEGIN_NAMESPACE(muscle);
00012 
00013 #ifndef MUSCLE_DEFAULT_TCP_STALL_TIMEOUT
00014 # define MUSCLE_DEFAULT_TCP_STALL_TIMEOUT (3*60*((uint64)1000000))  // 3 minutes is our default timeout period
00015 #endif
00016 
00020 class TCPSocketDataIO : public DataIO
00021 {
00022 public:
00030    TCPSocketDataIO(const SocketRef & sock, bool blocking) : _sock(sock), _blocking(true), _naglesEnabled(true), _stallLimit(MUSCLE_DEFAULT_TCP_STALL_TIMEOUT)
00031    {
00032       (void) SetBlockingIOEnabled(blocking);
00033    }
00034 
00038    virtual ~TCPSocketDataIO() 
00039    {
00040       Shutdown();
00041    }
00042 
00043    virtual int32 Read(void * buffer, uint32 size) {return ReceiveData(_sock, buffer, size, _blocking);}
00044    virtual int32 Write(const void * buffer, uint32 size) {return SendData(_sock, buffer, size, _blocking);}
00045 
00049    virtual status_t Seek(int64 /*seekOffset*/, int /*whence*/) {return B_ERROR;}
00050 
00052    virtual int64 GetPosition() const {return -1;}
00053 
00058    virtual uint64 GetOutputStallLimit() const {return _stallLimit;}
00059 
00061    void SetOutputStallLimit(uint64 limit) {_stallLimit = limit;}
00062 
00067    virtual void FlushOutput()
00068    {
00069       if ((_sock())&&(_naglesEnabled))
00070       {
00071          SetSocketNaglesAlgorithmEnabled(_sock, false);
00072 #ifndef __linux__
00073          (void) SendData(_sock, NULL, 0, _blocking);  // Force immediate buffer flush (not necessary under Linux)
00074 #endif
00075          SetSocketNaglesAlgorithmEnabled(_sock, true);
00076       }
00077    }
00078    
00082    virtual void Shutdown() {_sock.Reset();}
00083 
00085    virtual const SocketRef & GetSelectSocket() const {return _sock;}
00086 
00094    status_t SetBlockingIOEnabled(bool blocking)
00095    {
00096       status_t ret = SetSocketBlockingEnabled(_sock, blocking);
00097       if (ret == B_NO_ERROR) _blocking = blocking;
00098       return ret;
00099    }
00100 
00107    status_t SetNaglesAlgorithmEnabled(bool enabled)
00108    {
00109       status_t ret = SetSocketNaglesAlgorithmEnabled(_sock, enabled);
00110       if (ret == B_NO_ERROR) _naglesEnabled = enabled;
00111       return ret;
00112    }
00113 
00117    bool IsBlockingIOEnabled() const {return _blocking;}
00118 
00122    bool IsNaglesAlgorithmEnabled() const {return _naglesEnabled;}
00123 
00124 private:
00125    SocketRef _sock;
00126    bool _blocking;
00127    bool _naglesEnabled;
00128    uint64 _stallLimit;
00129 };
00130 
00131 END_NAMESPACE(muscle);
00132 
00133 #endif

Generated on Thu Jun 5 17:47:54 2008 for MUSCLE by  doxygen 1.5.1