📄 socket.h
字号:
// Socket.h: interface for the Socket class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SOCKET_H__FBB9FA86_7BFD_4384_88FA_5A26AA545E07__INCLUDED_)
#define AFX_SOCKET_H__FBB9FA86_7BFD_4384_88FA_5A26AA545E07__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Util.h"
#include "Exception.h"
#include "FileDescriptor.h"
#define LOCAL_MACHINE "127.0.0.1"
#ifdef _WIN32
#include <winsock2.h>
// Berkely constants converted to the windows equivs...
#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
typedef SOCKET socket_t;
#else
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
typedef int socket_t;
const int INVALID_SOCKET = -1;
#define SOCKET_ERROR -1
#endif
class SocketException : public Exception {
public:
#ifdef _DEBUG
SocketException(const string& aError) throw() : Exception("SocketException: " + aError) { }
#else //_DEBUG
SocketException(const string& aError) throw() : Exception(aError) { }
#endif // _DEBUG
SocketException(int aType,int aError) throw();
virtual ~SocketException() throw() { }
private:
static string errorToString(int aType,int aError) throw();
};
enum socket_type{
TYPE_TCP,
TYPE_UDP
};
enum wait_type{
WAIT_NONE = 0x00,
WAIT_CONNECT = 0x01,
WAIT_READ = 0x02,
WAIT_WRITE = 0x04
};
class Socket:public FileDescriptor
{
public:
Socket(socket_type socketType=TYPE_TCP): FileDescriptor(PSOCKET),sock(INVALID_SOCKET), connected(false), blocking(true) {}
virtual ~Socket(){}
/**
* Connects a socket to an address/ip, closing any other connections made with
* this instance.
* @param aAddr Server address, in dns or xxx.xxx.xxx.xxx format.
* @param aPort Server port.
* @throw SocketException If any connection error occurs.
*/
virtual void connect(const string& aIp, unsigned short aPort) throw(SocketException);
virtual void shutdown() throw(SocketException);
virtual void close();
void disconnect() throw(SocketException);
/**
* Reads zero to aBufLen characters from this socket,
* @param aBuffer A buffer to store the data in.
* @param aBufLen Size of the buffer.
* @return Number of bytes read, 0 if disconnected and -1 if the call would block.
* @throw SocketException On any failure.
*/
virtual int read(void* aBuffer, int aBufLen) throw(SocketException);
virtual int write(const void* aBuffer, int aLen) throw(SocketException);
virtual int wait(u_int32_t millis, int waitFor) throw(SocketException);
bool isConnected() { return connected; }
bool isValidSocket() {return sock != INVALID_SOCKET;}
static string resolve(const string& aDns);
#ifdef _WIN32
void setBlocking(bool block) throw() {
u_long b = block ? 0 : 1;
ioctlsocket(sock, FIONBIO, &b);
blocking = block;
}
#else
void setBlocking(bool block) throw() {
int flags = fcntl(sock, F_GETFL, 0);
if(block) {
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
} else {
fcntl(sock, F_SETFL, flags & (~O_NONBLOCK));
}
blocking = block;
}
#endif
bool getBlocking() throw() { return blocking; }
string getLocalIp() throw();
// Low level interface
virtual void create(socket_type aType = TYPE_TCP) throw(SocketException);
/** Binds a socket to a certain local port and possibly IP. */
void bind(unsigned short aPort = 0, const string& aIp = LOCAL_MACHINE) throw(SocketException);
virtual void listen() throw(SocketException);
virtual void accept(const Socket& listeningSocket) throw(SocketException);
int getSocketOptInt(int option) throw(SocketException);
void setSocketOpt(int option, int value) throw(SocketException);
socket_type getSocketType(){return stype;}
void setSocketType(socket_type socketType){stype = socketType;}
socket_t getSock(){return sock;}
void setSock(socket_t s)
{
if(sock!=INVALID_SOCKET)
disconnect();
sock = s;
status = OPEN;
}
#ifdef WIN32
virtual int getLastError() { return ::WSAGetLastError(); }
#else
virtual int getLastError() { return errno; }
#endif
virtual void setLastError(int errCode){err = errCode;}
protected:
socket_t sock;
socket_type stype;
bool connected;
bool blocking;
int err;
static int totalDown;
static int totalUp;
//static string udpServer;
//static short udpPort;
private:
Socket(const Socket&);
Socket& operator=(const Socket&);
//void socksAuth(u_int32_t timeout) throw(SocketException);
#ifdef _WIN32
int checksocket(socket_t ret) {
if(ret == (socket_t) SOCKET_ERROR) {
char buf[25];
sprintf(buf,"%d",getLastError());
throw SocketException(buf);
}
return ret;
}
int check(int ret, bool blockOk = false) {
if(ret == SOCKET_ERROR) {
int error = getLastError();
if(blockOk && error == WSAEWOULDBLOCK) {
return SOCKET_ERROR;
} else {
char buf[25];
sprintf(buf,"%d",error);
throw SocketException(buf);
}
}
return ret;
}
#else
//static int getLastError() { return errno; }
int checksocket(int ret) {
if(ret < 0) {
throw SocketException(getLastError());
}
return ret;
}
int check(int ret, bool blockOk = false) {
if(ret == -1) {
int error = getLastError();
if(blockOk && (error == EWOULDBLOCK || error == ENOBUFS) ) {
return -1;
} else {
throw SocketException(error);
}
}
return ret;
}
#endif
};
#endif // !defined(AFX_SOCKET_H__FBB9FA86_7BFD_4384_88FA_5A26AA545E07__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -