• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/net/UdpSocket.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _udpsocket_h
00018 #define _udpsocket_h
00019 
00020 #include <spl/Debug.h>
00021 #include <spl/collection/Array.h>
00022 #include <spl/RefCountPtr.h>
00023 #include <spl/net/Socket.h>
00024 
00031 class UdpSocket;
00032 typedef RefCountPtr<UdpSocket> UdpSocketPtr;
00033 typedef WeakReference<UdpSocket, UdpSocketPtr> UdpSocketRef;
00034 
00038 class UdpSocket : public IMemoryValidate
00039 {
00040 private:
00041 
00042 protected:
00043         String m_address;
00044         SocketPtr m_sock;
00045         int m_port;
00046         SOCKADDR_IN m_recvFromAddr;
00047 
00048 public:
00049         UdpSocket(const UdpSocket& sock);
00050         UdpSocket( int listenPortSetToZeroForSending = 0 );
00051         UdpSocket( const String& address, int listenPort = 0 );
00052         virtual ~UdpSocket();
00053 
00054         UdpSocket& operator =(const UdpSocket& pmp);
00055 
00056         void Send (const Array<byte>& packet);
00057         void Send (const Array<byte>& packet, const String& address);
00058         int Recv(Array<byte>& packet);
00059         
00060         SOCKADDR_IN ReceivedFromAddress() const { return m_recvFromAddr; }
00061 
00062         inline bool IsClosed() { return m_sock->IsClosed(); }
00063         inline void Close() { m_sock->Close(); }
00064         inline void Shutdown( int i=SD_BOTH ) { m_sock->Shutdown(i); }
00065         
00066         inline void SetNonBlocking() { m_sock->SetNonBlocking(); }
00067         inline void SetBlocking() { m_sock->SetBlocking(); }
00068         inline void SetLingerOn() { m_sock->SetLingerOn(); }
00069         inline void SetLingerOff() { m_sock->SetLingerOff(); }
00070         inline void SetNoDelay() { m_sock->SetNoDelay(); }
00071 
00072         inline void SetSendTimeout(int toMS) { m_sock->SetSendTimeout(toMS); }
00073         inline void SetRecvTimeout(int toMS) { m_sock->SetRecvTimeout(toMS); }
00074         inline int GetSendTimeout() { return m_sock->GetSendTimeout(); }
00075         inline int GetRecvTimeout() { return m_sock->GetRecvTimeout(); }
00076 
00077         inline int GetErrorCode() { return m_sock->m_errorStatus; }
00078         inline int GetBytesAvail() { return m_sock->GetBytesAvail(); }
00079 
00080         inline String GetRemoteIp(  ) { return m_sock->GetRemoteIp(); }
00081 
00082         inline bool operator ==(const UdpSocket& s) const
00083         {
00084                 return m_sock->GetFD() == s.m_sock->GetFD();
00085         }
00086 
00087         friend class ServerSocket;
00088         friend class SocketSet;
00089 
00090 #if defined(DEBUG) || defined(_DEBUG)
00091         virtual void CheckMem() const;
00092         virtual void ValidateMem() const;
00093 #endif
00094 };
00095 
00096 REGISTER_TYPEOF(71, UdpSocket);
00097 
00100 #endif