216 lines
5.2 KiB
C++
216 lines
5.2 KiB
C++
#ifndef _SOCKET_SOCKET_HPP_
|
|
#define _SOCKET_SOCKET_HPP_
|
|
#ifndef _COMMON_WINSOCK_HPP_
|
|
#include <common/winsock.hpp>
|
|
#endif
|
|
#ifndef _SOCKET_INETSOCKETADDRESS_HPP_
|
|
#include <socket/intsaddr.hpp>
|
|
#endif
|
|
#ifndef _SOCKET_SOCKETADDRESS_HPP_
|
|
#include <socket/sockaddr.hpp>
|
|
#endif
|
|
#ifndef _SOCKET_IPMULTICAST_HPP_
|
|
#include <socket/ipmcast.hpp>
|
|
#endif
|
|
#ifndef _SOCKET_WSADATA_HPP_
|
|
#include <socket/wsadata.hpp>
|
|
#endif
|
|
#ifndef _SOCKET_TIMEINFO_HPP_
|
|
#include <socket/timeinfo.hpp>
|
|
#endif
|
|
#ifndef _SOCKET_RECEIVECACHE_HPP_
|
|
#include <socket/cache.hpp>
|
|
#endif
|
|
|
|
template <class T>
|
|
class Block;
|
|
|
|
class Socket
|
|
{
|
|
public:
|
|
enum Type{SocketStream=SOCK_STREAM,SocketDGram=SOCK_DGRAM,SocketRaw=SOCK_RAW,SocketSeqPacket=SOCK_SEQPACKET,SocketRdm=SOCK_RDM};
|
|
enum Protocol{ProtocolNone=0,ProtocolIPTCP=IPPROTO_TCP};
|
|
enum Domain{DomainUnix=AF_UNIX,DomainInternet=AF_INET};
|
|
enum Disposition{CloseSocket,AssumedSocket};
|
|
enum MsgType{None=0,OutOfBand=MSG_OOB,Peek=MSG_PEEK,DontRoute=MSG_DONTROUTE};
|
|
Socket(void);
|
|
Socket(const Socket &someSocket);
|
|
Socket(SOCKET someSOCKET);
|
|
virtual ~Socket();
|
|
Socket &operator=(const Socket &someSocket);
|
|
Socket &operator=(SOCKET someSOCKET);
|
|
bool operator==(const Socket &someSocket)const;
|
|
operator SOCKET&(void);
|
|
bool create(Domain domain=DomainInternet,Type type=SocketStream,Protocol protocol=ProtocolIPTCP);
|
|
void destroy(void);
|
|
bool connect(INETSocketAddress &internetSocketAddress);
|
|
bool bind(INETSocketAddress &internetSocketAddress);
|
|
bool bind(SocketAddress &socketAddress);
|
|
bool listen(int maxPendingRequests=1);
|
|
bool accept(Socket &someSocket);
|
|
bool accept(Socket &someSocket,INETSocketAddress &internetSocketAddress);
|
|
DWORD receive(char *buffer,DWORD count,bool waitForData=true);
|
|
bool receive(char &charData,bool waitForData=true);
|
|
bool receive(String &receiveString,bool waitForData=true);
|
|
bool receive(Block<String> &receiveStrings);
|
|
bool receiveFrom(void *pBuffer,int buflen,DWORD msgType,INETSocketAddress &from);
|
|
bool receivePage(Block<String> &receiveStrings);
|
|
bool receiveHeader(Block<String> &headerLines);
|
|
bool receiveImage(String pathFileName,TimeInfo &someTimeInfo);
|
|
bool receiveBinary(String pathFileName,DWORD sizeData,TimeInfo &someTimeInfo);
|
|
bool receiveBinary(String pathFileName,TimeInfo &someTimeInfo);
|
|
bool sendBinary(String pathFileName,TimeInfo &someTimeInfo);
|
|
bool getSocketName(INETSocketAddress &internetSocketAddress);
|
|
bool getPeerName(INETSocketAddress &internetSocketAddress);
|
|
bool send(const char *buffer,DWORD count);
|
|
bool send(const char &charData);
|
|
bool send(const String &sendString);
|
|
bool send(Block<String> &strings);
|
|
bool sendTo(void *pBuffer,int buflen,INETSocketAddress &inetSocketAddress);
|
|
bool hasData(void)const;
|
|
bool isConnected(void)const;
|
|
bool isBound(void)const;
|
|
bool isListening(void)const;
|
|
bool isInitialized(void)const;
|
|
bool isOkay(void)const;
|
|
SOCKET getSocket(void)const;
|
|
bool reuseAddress(bool reuseAddress=true);
|
|
bool setLinger(int seconds);
|
|
bool setDontLinger(void);
|
|
bool addMembership(IPMReq &ipMReq);
|
|
bool dropMembership(IPMReq &ipMReq);
|
|
bool setMulticastTTL(int ttl);
|
|
bool setMulticastLoopback(bool loopback);
|
|
private:
|
|
enum Status{StatusConnect=0x0001,StatusBind=0x0002,StatusListen=0x0004};
|
|
enum {MaxLength=32768,BlockLength=16384};
|
|
bool canWrite(void)const;
|
|
void isConnected(bool isConnected);
|
|
void isBound(bool isBound);
|
|
void isListening(bool isListening);
|
|
|
|
SOCKET mSocket;
|
|
WORD mStatusBits;
|
|
WSASystem mWSASystem;
|
|
ReceiveCache mReceiveCache;
|
|
Disposition mDisposition;
|
|
};
|
|
|
|
inline
|
|
Socket::Socket(void)
|
|
: mSocket(INVALID_SOCKET), mDisposition(CloseSocket), mStatusBits(0L)
|
|
{
|
|
}
|
|
|
|
inline
|
|
Socket::Socket(const Socket &someSocket)
|
|
{
|
|
*this=someSocket;
|
|
}
|
|
|
|
inline
|
|
Socket::Socket(SOCKET someSOCKET)
|
|
{
|
|
*this=someSOCKET;
|
|
}
|
|
|
|
inline
|
|
Socket::~Socket()
|
|
{
|
|
destroy();
|
|
}
|
|
|
|
inline
|
|
bool Socket::operator==(const Socket &someSocket)const
|
|
{
|
|
return mSocket==someSocket.mSocket;
|
|
}
|
|
|
|
inline
|
|
Socket::operator SOCKET&(void)
|
|
{
|
|
return mSocket;
|
|
}
|
|
|
|
inline
|
|
bool Socket::isOkay(void)const
|
|
{
|
|
return (INVALID_SOCKET==mSocket?false:true);
|
|
}
|
|
|
|
inline
|
|
bool Socket::canWrite(void)const
|
|
{
|
|
fd_set setDescriptor;
|
|
|
|
setDescriptor.fd_count=1;
|
|
setDescriptor.fd_array[0]=mSocket;
|
|
return ::select(0,0,&setDescriptor,0,0);
|
|
}
|
|
|
|
inline
|
|
bool Socket::hasData(void)const
|
|
{
|
|
fd_set setDescriptor;
|
|
timeval waitTime;
|
|
|
|
setDescriptor.fd_count=1;
|
|
setDescriptor.fd_array[0]=mSocket;
|
|
waitTime.tv_sec=0;
|
|
waitTime.tv_usec=0;
|
|
return ::select(0,&setDescriptor,0,0,&waitTime);
|
|
}
|
|
|
|
inline
|
|
bool Socket::isInitialized(void)const
|
|
{
|
|
return mWSASystem.isInitialized();
|
|
}
|
|
|
|
inline
|
|
bool Socket::isConnected(void)const
|
|
{
|
|
return (mStatusBits&StatusConnect?true:false);
|
|
}
|
|
|
|
inline
|
|
void Socket::isConnected(bool isConnected)
|
|
{
|
|
if(isConnected)mStatusBits|=StatusConnect;
|
|
else mStatusBits&=~StatusConnect;
|
|
}
|
|
|
|
inline
|
|
bool Socket::isBound(void)const
|
|
{
|
|
return (mStatusBits&StatusBind?true:false);
|
|
}
|
|
|
|
inline
|
|
void Socket::isBound(bool isBound)
|
|
{
|
|
if(isBound)mStatusBits|=StatusBind;
|
|
else mStatusBits&=~StatusBind;
|
|
}
|
|
|
|
inline
|
|
bool Socket::isListening(void)const
|
|
{
|
|
return (mStatusBits&StatusListen?true:false);
|
|
}
|
|
|
|
inline
|
|
void Socket::isListening(bool isListening)
|
|
{
|
|
if(isListening)mStatusBits|=StatusListen;
|
|
else mStatusBits&=~StatusListen;
|
|
}
|
|
|
|
inline
|
|
SOCKET Socket::getSocket(void)const
|
|
{
|
|
return mSocket;
|
|
}
|
|
#endif
|
|
|