UDPSocketDataIO.h

00001 /* This file is Copyright 2000-2008 Meyer Sound Laboratories Inc.  See the included LICENSE.txt file for details. */
00002 
00003 #ifndef MuscleUDPSocketDataIO_h
00004 #define MuscleUDPSocketDataIO_h
00005 
00006 #include "support/MuscleSupport.h"
00007 
00008 #include "dataio/DataIO.h"
00009 #include "util/NetworkUtilityFunctions.h"
00010 
00011 BEGIN_NAMESPACE(muscle);
00012 
00016 class UDPSocketDataIO : public DataIO
00017 {
00018 public:
00026    UDPSocketDataIO(const SocketRef & sock, bool blocking) : _sock(sock)
00027    {
00028       (void) SetBlockingIOEnabled(blocking);
00029    }
00030 
00034    virtual ~UDPSocketDataIO() 
00035    {
00036       // empty
00037    }
00038 
00039    virtual int32 Read(void * buffer, uint32 size) 
00040    {
00041       ip_address tmpAddr = invalidIP;
00042       uint16 tmpPort = 0;
00043       int32 ret = ReceiveDataUDP(_sock, buffer, size, _blocking, &tmpAddr, &tmpPort);
00044       _recvFrom.SetIPAddress(tmpAddr);
00045       _recvFrom.SetPort(tmpPort);
00046       return ret;
00047    }
00048 
00049    virtual int32 Write(const void * buffer, uint32 size) {return SendDataUDP(_sock, buffer, size, _blocking, _sendTo.GetIPAddress(), _sendTo.GetPort());}
00050 
00054    virtual status_t Seek(int64 /*seekOffset*/, int /*whence*/) {return B_ERROR;}
00055 
00057    virtual int64 GetPosition() const {return -1;}
00058 
00060    virtual void FlushOutput() {/* empty */}
00061    
00065    virtual void Shutdown() {_sock.Reset();}
00066 
00068    virtual const SocketRef & GetSelectSocket() const {return _sock;}
00069 
00074    void SetSendDestination(const IPAddressAndPort & dest) {_sendTo = dest;}
00075 
00079    const IPAddressAndPort & GetSendDestination() const {return _sendTo;}
00080 
00088    status_t SetBlockingIOEnabled(bool blocking)
00089    {
00090       status_t ret = SetSocketBlockingEnabled(_sock, blocking);
00091       if (ret == B_NO_ERROR) _blocking = blocking;
00092       return ret;
00093    }
00094 
00098    bool IsBlockingIOEnabled() const {return _blocking;}
00099 
00103    const IPAddressAndPort & GetSourceOfLastReadPacket() const {return _recvFrom;}
00104 
00105 private:
00106    SocketRef _sock;
00107    bool _blocking;
00108 
00109    IPAddressAndPort _recvFrom;
00110    IPAddressAndPort _sendTo;
00111 };
00112 
00113 END_NAMESPACE(muscle);
00114 
00115 #endif

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