00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __SOCKET_H__
00012 #define __SOCKET_H__
00013
00014 #include "Network/NetworkDll.h"
00015 #include <windows.h>
00016 #include "Common/Types.h"
00017 #include "Common/Str.h"
00018 #include "Network/SocketDefs.h"
00019
00020 class KInetAddr;
00021
00022
00023 class NETWORK_API KSocket
00024 {
00025 protected:
00026 SOCKET m_hSocket;
00027 bool m_bConnected;
00028 bool m_bCompress;
00029 bool m_bAttached;
00030
00031 u32 m_nBytesSent;
00032 u32 m_nBytesReceived;
00033 u32 m_nRealBytesSent;
00034 u32 m_nRealBytesReceived;
00035
00036 public:
00037 KSocket();
00038 virtual ~KSocket();
00039
00040 bool Attach( SOCKET hSocket );
00041 bool Detach();
00042
00043 bool Create( KNSOCKETTYPE Type );
00044 bool Close();
00045 bool Bind( KInetAddr& Addr );
00046 bool SelectRead();
00047 bool SelectWrite();
00048 bool SelectExcept();
00049 bool Connect( KInetAddr& Addr );
00050 bool Disconnect();
00051 bool Listen();
00052 bool Accept( KSocket& AcceptedSocket, KInetAddr& Addr );
00053 bool Send( void* pBuffer, s32 Size );
00054 bool Receive( void* pBuffer, s32 Size, u32* pBytesRead );
00055 bool SendTo( KInetAddr& Addr, void* pBuffer, u32 Size );
00056 bool ReceiveFrom( KInetAddr& Addr, void* pBuffer, u32 Size, u32* pBytesRead );
00057 bool AsyncSelect();
00058
00059 bool IsConnected() { return m_bConnected; }
00060 void SetConnected( bool bConnected ) { m_bConnected = bConnected; }
00061
00062 void EnableBroadcast( bool bEnable );
00063
00064 virtual void OnAccept();
00065 virtual void OnRead();
00066 virtual void OnWrite();
00067 virtual void OnClose();
00068 virtual void OnOOB();
00069
00070 static bool StartNetwork();
00071 static bool StopNetwork();
00072
00073 static bool GetHostName( char* pBuffer, s32 Size );
00074 static hostent* GetHostByName( char* pName );
00075 static hostent* GetHostByAddr( KInetAddr& Addr );
00076 static bool GetAddrByName( KStr& sHostName, KInetAddr& Addr );
00077
00078 SOCKET GethSocket() { return m_hSocket; }
00079
00080
00081 u32 GetnBytesSent() { return m_nBytesSent; }
00082 u32 GetnBytesReceived() { return m_nBytesReceived; }
00083 u32 GetnRealBytesSent() { return m_nRealBytesSent; }
00084 u32 GetnRealBytesReceived() { return m_nRealBytesReceived; }
00085 void ResetStats() { m_nBytesSent = m_nBytesReceived = m_nRealBytesSent = m_nRealBytesReceived = 0;}
00086 };
00087
00088 #endif __SOCKET_H__