Better document.

Also fixed some small bugs.
This commit is contained in:
Kirigaya Kazuto 2018-06-06 01:04:41 +08:00
parent bb97f75944
commit 8cf93c3f9a
2 changed files with 108 additions and 42 deletions

View File

@ -406,6 +406,15 @@ serversock::serversock(int use_family) :_pp(new _impl)
} }
} }
serversock::~serversock()
{
if (_pp)
{
delete _pp;
_pp = nullptr;
}
}
int serversock::bind(int Port) int serversock::bind(int Port)
{ {
myliblog("serversock::bind() %p\n",this); myliblog("serversock::bind() %p\n",this);
@ -546,6 +555,12 @@ struct udpsock::_impl
} }
} }
// Decide the protocol
// Return:
// GSOCK_OK
// GSOCK_MISMATCH_PROTOCOL
// GSOCK_INVALID_SOCKET
// GSOCK_ERROR_CREAT
int try_decide(vsock::_impl* _vp, int in_protocol) int try_decide(vsock::_impl* _vp, int in_protocol)
{ {
if (is_protocol_decided) if (is_protocol_decided)
@ -719,7 +734,7 @@ int udpsock::broadcast_at(int Port)
else else
{ {
myliblog("IPv6 does not support broadcast!\n"); myliblog("IPv6 does not support broadcast!\n");
return -1; return GSOCK_BAD_PROTOCOL;
} }
} }
else else
@ -829,7 +844,7 @@ int udpsock::broadcast(int Port,const void* buffer,int length)
else else
{ {
myliblog("IPv6 does not support broadcast!\n"); myliblog("IPv6 does not support broadcast!\n");
return -1; return GSOCK_BAD_PROTOCOL;
} }
} }
else else
@ -1054,7 +1069,7 @@ int DNSResolve(const std::string& HostName, std::vector<std::string>& _out_IPStr
int ret = getaddrinfo(HostName.c_str(), NULL, &hints, &result); int ret = getaddrinfo(HostName.c_str(), NULL, &hints, &result);
if (ret != 0) if (ret != 0)
{ {
return -1;/// API Call Failed. return GSOCK_API_ERROR;/// API Call Failed.
} }
int cnt = 0; int cnt = 0;

127
gsock.h
View File

@ -20,6 +20,7 @@ enum
GSOCK_UNKNOWN_PROTOCOL = -5, // Unknown Protocol GSOCK_UNKNOWN_PROTOCOL = -5, // Unknown Protocol
GSOCK_ERROR_NTOP = -6, // inet_ntop failed. GSOCK_ERROR_NTOP = -6, // inet_ntop failed.
GSOCK_MISMATCH_PROTOCOL = -7, // Protocol mismatch. GSOCK_MISMATCH_PROTOCOL = -7, // Protocol mismatch.
GSOCK_BAD_PROTOCOL = -8, // Bad protocol.
}; };
class vsock class vsock
@ -43,32 +44,34 @@ protected:
class sock : public vsock class sock : public vsock
{ {
public: public:
/// Return: // Return:
/// 0: Connection Established. No Error. // GSOCK_OK: Connection Established. No Error.
/// -1: connect() call error. See errno. // GSOCK_API_ERROR: connect() call error. See errno.
/// -2: This socket has been connected before. // GSOCK_INVALID_SOCKET: This socket has been connected before.
/// -3: socket() call error. Failed to create socket. See errno. // GSOCK_ERROR_CREAT
/// -4: IP Address invalid. // GSOCK_INVALID_IP
int connect(const std::string& IPStr,int Port); int connect(const std::string& IPStr,int Port);
/// Return: // Return:
/// return what send() and recv() call returns. // return what send() and recv() call returns.
int send(const void* Buffer,int Length); int send(const void* Buffer,int Length);
int recv(void* Buffer, int MaxToRecv); int recv(void* Buffer, int MaxToRecv);
// Return:
// GSOCK_OK
// GSOCK_API_ERROR
int getsendtime(int& _out_Second,int& _out_uSecond); int getsendtime(int& _out_Second,int& _out_uSecond);
int getrecvtime(int& _out_Second,int& _out_uSecond); int getrecvtime(int& _out_Second,int& _out_uSecond);
int setsendtime(int Second); int setsendtime(int Second);
int setrecvtime(int Second); int setrecvtime(int Second);
/// Return: // Return:
/// 0: Success. No Error. IPv4 // 0: Success. No Error. IPv4
/// 1: Success. No Error. IPv6 // 1: Success. No Error. IPv6
/// -1: getlocalname() or getpeername() call error. See errno. // GSOCK_API_ERROR: getlocalname() or getpeername() call error. See errno.
/// -2: Socket not created. // GSOCK_INVALID_SOCKET: Socket not created.
int getpeer(std::string& IPStr, int& Port);
int getlocal(std::string& IPStr,int& Port); int getlocal(std::string& IPStr,int& Port);
int getpeer(std::string& IPStr,int& Port);
friend class serversock; friend class serversock;
private: private:
@ -85,23 +88,29 @@ public:
serversock(int use_family=0); serversock(int use_family=0);
~serversock(); ~serversock();
/// Return: // Return:
/// 0: Bind Succeed. No Error. // GSOCK_OK: Bind Succeed. No Error.
/// -1: bind() call error. See errno. // GSOCK_API_ERROR: bind() call error. See errno.
/// -2: This socket has been created before. // GSOCK_INVALID_SOCKET: This socket has been created before.
/// -3: socket() call error. Failed to create socket. See errno. // GSOCK_ERROR_CREAT
int bind(int Port); int bind(int Port);
// Return:
// GSOCK_OK
// GSOCK_ERROR_CREAT
// GSOCK_API_ERROR: setsockopt() call error.
int set_reuse(); int set_reuse();
/// Return: // Return:
/// return what listen() call returns // GSOCK_OK
// GSOCK_API_ERROR: listen() call error.
// GSOCK_INVALID_SOCKET
int listen(int MaxCount); int listen(int MaxCount);
/// Return: // Return:
/// 0: Accept Succeed. No Error. _out_s holds the new socket. // GSOCK_OK: Accept Succeed. No Error. _out_s holds the new socket.
/// -1: accept() call error. See errno. // GSOCK_API_ERROR: accept() call error. See errno.
/// -2: _out_s is a connected socket, which should not be passed in. // GSOCK_INVALID_SOCKET: _out_s is not an empty socket, which should not be passed in.
int accept(sock& _out_s); int accept(sock& _out_s);
private: private:
struct _impl; struct _impl;
@ -118,26 +127,66 @@ public:
udpsock(int use_family=0); udpsock(int use_family=0);
~udpsock(); ~udpsock();
/// Use udp socket as tcp socket. (but of course it is not). // Use udp socket as tcp socket. (but of course it is not).
/// connect call just copy the target socket data to kernel. See connect() for more info. // connect call just copy the target socket data to kernel. See connect() for more info.
/// Return: // Return:
/// -1: connect() error. // GSOCK_OK: data copied.
/// -4: IP Address Invalid. // GSOCK_API_ERROR: connect() call error.
// GSOCK_INVALID_IP
// GSOCK_MISMATCH_PROTOCOL
// GSOCK_INVALID_SOCKET
// GSOCK_ERROR_CREAT
int connect(const std::string& IPStr,int Port); int connect(const std::string& IPStr,int Port);
// Return:
// Besides all returns of connect(...), adding the following:
// GSOCK_BAD_PROTOCOL: broadcast is not supported.
int broadcast_at(int Port); int broadcast_at(int Port);
/// Must be called in broadcast mode. // Must be called in broadcast mode before any broadcasting.
// Return:
// GSOCK_OK
// GSOCK_MISMATCH_PROTOCOL
// GSOCK_INVALID_SOCKET
// GSOCK_ERROR_CREAT
int set_broadcast(); int set_broadcast();
/// Explict bind() call is only need when you have to receive data. // Explict bind() call is only need when you have to receive data.
// Return:
// GSOCK_OK
// GSOCK_MISMATCH_PROTOCOL
// GSOCK_INVALID_SOCKET
// GSOCK_ERROR_CREAT
int bind(int Port); int bind(int Port);
// Return:
// ret>=0: sendto() returns
// GSOCK_API_ERROR(-1): sendto() call error.
// GSOCK_INVALID_IP
// GSOCK_MISMATCH_PROTOCOL
// GSOCK_INVALID_SOCKET
// GSOCK_ERROR_CREAT
int sendto(const std::string& IPStr, int Port, const void* buffer, int length); int sendto(const std::string& IPStr, int Port, const void* buffer, int length);
// Return:
// Besides all returns of sendto(...), adding the following:
// GSOCK_BAD_PROTOCOL: broadcast is not supported.
int broadcast(int Port,const void* buffer,int length); int broadcast(int Port,const void* buffer,int length);
/// Must call bind() before calling recvfrom().
// Must call bind() before calling recvfrom().
// Return:
// ret>=0: recvfrom() returns
// GSOCK_API_ERROR(-1): recvfrom() call error.
// GSOCK_ERROR_NTOP
// GSOCK_UNKNOWN_PROTOCOL
// GSOCK_MISMATCH_PROTOCOL
// GSOCK_INVALID_SOCKET
// GSOCK_ERROR_CREAT
int recvfrom(std::string& fromIP, int& fromPort, void* buffer, int bufferLength); int recvfrom(std::string& fromIP, int& fromPort, void* buffer, int bufferLength);
/// send() and recv() should only be called after connect(). Or it will fail. // send() and recv() should only be called after connect(). Or it will fail.
// Return:
// ret>=0: send(), recv() returns.
// GSOCK_API_ERROR(-1): send(), recv() call error.
// GSOCK_INVALID_SOCKET: socket not created, and connect() has not been called yet.
int send(const void* buffer,int length); int send(const void* buffer,int length);
int recv(void* buffer,int bufferLength); int recv(void* buffer,int bufferLength);
private: private:
@ -171,16 +220,18 @@ private:
}; };
/// Net Tools /// Net Tools
// Return: // Return:
// >=0: Number of fetched results from getaddrinfo() call.
// -1: getaddrinfo() call failed. // -1: getaddrinfo() call failed.
// Other: Number of fetched results from getaddrinfo() call.
int DNSResolve(const std::string& HostName, std::vector<std::string>& _out_IPStrVec); int DNSResolve(const std::string& HostName, std::vector<std::string>& _out_IPStrVec);
// A wrapper of DNSResolve(...,std::vector<std::string>&) // A wrapper of the vector version of DNSResolve.
// _out_IPStr will be assigned with the first result in vector.
// Return: // Return:
// 0: Success.
// -1: getaddrinfo() call failed. // -1: getaddrinfo() call failed.
// -2: Failed to resolve. (No results in vector) // -2: Failed to resolve. (No results in vector)
// 0: Success.
int DNSResolve(const std::string& HostName,std::string& _out_IPStr); int DNSResolve(const std::string& HostName,std::string& _out_IPStr);
#endif // _gsock_h #endif // _gsock_h