I've made a class for sockets and now i want to hear your opinions on the OOD of it
Can i optimize performance without ruining the OOD of it?
Should an help class contain so much error handling ( and if yes, then do you know any better way of doing it ? ) or should each class that uses Socket contain error handling?
Do you know any english / swedish book on OOA & OOD & OOP for C++ ?
Code:
#if defined(_WIN32)
#include <windows.h>
#include <winsock.h>
#define socket_recive(Socket,buffer,len, flags) recv(Socket, (char*)buffer, len , flags)
#define socket_send(Socket,buffer,len, flags) send(Socket, (char*)buffer, len , flags)
#define socket_close closesocket
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#define socket_recive read
#define socket_send write
#define socket_close close
#endif
#include <deque>
class Socket
{
private:
SOCKET m_Socket;
std::deque<int> errorVector;
bool IsOpened;
public:
Socket()
{
this->setIsOpened(false);
}
virtual ~Socket()
{
this->Close();
this->errorVector.clear();
}
int retrieveError(int num)
{
return this->errorVector.at(num);
}
private:
bool testForError(int func)
{
if(func != 0)
this->insertError(func);
return func == 0 ? true : false ;
}
void insertError(int errorVal)
{
this->errorVector.push_front(errorVal);
}
void setIsOpened(bool bVal)
{
this->IsOpened = bVal;
}
bool getIsOpened()
{
return this->IsOpened;
}
public:
bool Open(int af, int type, int protocol)
{
if(this->getIsOpened() == true)
return true;
SOCKET tempSock = socket(af, type, protocol);
if(tempSock == INVALID_SOCKET)
{
this->insertError( (int)tempSock );
return false;
}
else
{
this->setIsOpened(true);
this->m_Socket = tempSock;
return true;
}
}
bool Open(SOCKET sock)
{
if(this->getIsOpened() == true)
return true;
if(sock == INVALID_SOCKET)
{
this->insertError( (int)sock );
return false;
}
else
{
this->setIsOpened(true);
this->m_Socket = sock;
return true;
}
}
bool Close()
{
bool didSucceed = this->testForError( socket_close(this->m_Socket) );
if(didSucceed)
{
this->setIsOpened(false);
return true;
}
else return false;
}
bool Bind(sockaddr* name, int namelen)
{
return this->testForError( bind(this->m_Socket, name, namelen) );
}
bool Connect(sockaddr* name, int namelen)
{
return this->testForError( connect(this->m_Socket, name, namelen) );
}
bool Connect(std::string host, int port, int af)
{
hostent* he;
if( (he = gethostbyname( host.c_str() )) == 0 )
return false;
sockaddr_in clientService;
clientService.sin_family = af;
clientService.sin_addr.s_addr = inet_addr( inet_ntoa( (**(in_addr**)he->h_addr_list ) ) );
clientService.sin_port = htons( port );
return this->testForError( connect(this->m_Socket, (SOCKADDR*) &clientService, sizeof(clientService) ) );
}
bool Listen(int backlog)
{
return this->testForError( listen(this->m_Socket, backlog) );
}
Socket* Accept(sockaddr* addr, int* addrlen)
{
Socket* tempSock = new Socket();
tempSock->Open( accept(this->m_Socket, addr, addrlen) );
return tempSock;
}
int Receive(char* &buf, int len, int flags)
{
return socket_recive(this->m_Socket, buf, len, flags);
}
int Send(char* &buf, int len, int flags)
{
return socket_send(this->m_Socket, buf, len, flags);
}
};