Main Page | File List

blockingsocket.h

00001 // blocksock.h
00002 
00003 // needs winsock.h in the precompiled headers
00004 #include <winsock.h>
00005 
00006 #ifndef _BLOCKSOCK_H_
00007 #define _BLOCKSOCK_H_
00008 
00009 typedef const struct sockaddr* LPCSOCKADDR;
00010 
00011 class CSockAddr : public sockaddr_in 
00012 {
00013 public:
00014         // constructors
00015         CSockAddr()
00016         {
00017                 sin_family = AF_INET;
00018                 sin_port = 0;
00019                 sin_addr.s_addr = 0; 
00020         } // Default
00021         
00022         CSockAddr(const SOCKADDR& sa) 
00023         {
00024                 memcpy(this, &sa, sizeof(SOCKADDR)); 
00025         }
00026         
00027         CSockAddr(const SOCKADDR_IN& sin) 
00028         {
00029                 memcpy(this, &sin, sizeof(SOCKADDR_IN)); 
00030         }
00031         
00032         CSockAddr(const ULONG ulAddr, const USHORT ushPort = 0) // parms are host byte ordered
00033         {
00034                 sin_family = AF_INET;
00035                 sin_port = htons(ushPort);
00036             sin_addr.s_addr = htonl(ulAddr); 
00037         }
00038         
00039         CSockAddr(const char* pchIP, const USHORT ushPort = 0) // dotted IP addr string
00040         {
00041                 sin_family = AF_INET;
00042                 sin_port = htons(ushPort);
00043                 sin_addr.s_addr = inet_addr(pchIP); 
00044         } // already network byte ordered
00045         // Return the address in dotted-decimal format
00046         
00047         char* DottedDecimal()
00048         {
00049                 return inet_ntoa(sin_addr); 
00050         } // constructs a new CString object
00051         // Get port and address (even though they're public)
00052         
00053         USHORT Port() const
00054         {
00055                 return ntohs(sin_port); 
00056         }
00057         
00058         ULONG IPAddr() const
00059         {
00060                 return ntohl(sin_addr.s_addr); 
00061         }
00062         // operators added for efficiency
00063         const CSockAddr& operator=(const SOCKADDR& sa)
00064         {
00065                 memcpy(this, &sa, sizeof(SOCKADDR));
00066                   return *this; 
00067         }
00068         
00069         const CSockAddr& operator=(const SOCKADDR_IN& sin)
00070         {
00071                 memcpy(this, &sin, sizeof(SOCKADDR_IN));
00072                 return *this; 
00073         }
00074         
00075         operator SOCKADDR()
00076         {
00077                 return *((LPSOCKADDR) this); 
00078         }
00079         
00080         operator LPSOCKADDR()
00081         {
00082                 return (LPSOCKADDR) this; 
00083         }
00084         
00085         operator LPSOCKADDR_IN()
00086         {
00087                 return (LPSOCKADDR_IN) this; 
00088         }
00089 };
00090 
00091 // member functions truly block and must not be used in UI threads
00092 // use this class as an alternative to the MFC CSocket class
00093 class CBlockingSocket 
00094 {
00095 public:
00096         enum {DEFAULT_TIMEOUT=30};
00097         SOCKET m_hSocket;
00098         CBlockingSocket()
00099         {
00100                 m_hSocket = NULL; 
00101         }
00102         
00103         void Cleanup();
00104         void Create(int nType = SOCK_STREAM);
00105         void Close();
00106         void Bind(LPCSOCKADDR psa);
00107         void Listen();
00108         void Connect(LPCSOCKADDR psa);
00109         bool Accept(CBlockingSocket& s, LPSOCKADDR psa);
00110         int Send(const char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00111         int Write(const char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00112         int Receive(char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00113         int SendDatagram(const char* pch, const int nSize, LPCSOCKADDR psa, const int nSecs=10);
00114         int ReceiveDatagram(char* pch, const int nSize, LPSOCKADDR psa, const int nSecs=10);
00115         void GetPeerAddr(LPSOCKADDR psa);
00116         void GetSockAddr(LPSOCKADDR psa);
00117         static CSockAddr GetHostByName(const char* pchName, const USHORT ushPort = 0);
00118         static const char* GetHostByAddr(LPCSOCKADDR psa);
00119         operator SOCKET()
00120         {
00121                 return m_hSocket; 
00122         }
00123 };
00124 
00125 class CTelnetSocket : public CBlockingSocket
00126 {
00127 public:
00128         enum {nSizeRecv = 1024}; // max receive buffer size (> hdr line length)
00129         enum {FLAG_VALIDATED=1,FLAG_PLAYING=2};
00130         CTelnetSocket();
00131         ~CTelnetSocket();
00132         int ReadLine(char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00133         int ReadResponse(char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00134         int Print(const char* msg);
00135         bool SetLoginName(const char* name);
00136 
00137 public:
00138         //login information
00139         WORD m_wFlags;
00140         char* m_pLoginName;
00141 private:
00142         char* m_pReadBuf; // read buffer
00143         int m_nReadBuf; // number of bytes in the read buffer
00144 
00145 };
00146 
00147 
00148 #endif

Generated on Mon May 8 22:27:08 2006 by  doxygen 1.3.9.1