2017-07-27 19:52:58 +08:00
|
|
|
/** General Socket Wrapper
|
|
|
|
* Created By Kiritow. (https://github.com/kiritow)
|
|
|
|
* Licensed under MIT
|
|
|
|
*/
|
|
|
|
|
2018-02-11 15:41:52 +08:00
|
|
|
#ifndef _gsock_h
|
|
|
|
#define _gsock_h
|
2017-07-27 19:52:58 +08:00
|
|
|
|
|
|
|
#include <cstdint>
|
2017-08-15 09:54:10 +08:00
|
|
|
#include <string>
|
2017-07-27 19:52:58 +08:00
|
|
|
|
|
|
|
class sock
|
|
|
|
{
|
|
|
|
public:
|
2018-04-23 15:56:05 +08:00
|
|
|
sock();
|
2017-07-27 19:52:58 +08:00
|
|
|
sock(const sock&)=delete;
|
|
|
|
sock& operator = (const sock&)=delete;
|
|
|
|
sock(sock&&);
|
|
|
|
sock& operator = (sock&&);
|
|
|
|
~sock();
|
|
|
|
|
2017-08-15 09:54:10 +08:00
|
|
|
/// Return:
|
|
|
|
/// 0: Connection Established. No Error.
|
|
|
|
/// -1: connect() call error. See errno.
|
|
|
|
/// -2: This socket has been connected before.
|
|
|
|
/// -3: socket() call error. Failed to create socket. See errno.
|
2017-07-27 19:52:58 +08:00
|
|
|
int connect(const std::string& IPStr,int Port);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
int send(const T&);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
int recv(T&);
|
|
|
|
|
2017-08-15 09:54:10 +08:00
|
|
|
/// Return:
|
|
|
|
/// return what send() and recv() call returns.
|
2018-02-11 15:41:52 +08:00
|
|
|
int send(const void* Buffer,int Length);
|
2018-04-23 15:56:05 +08:00
|
|
|
int recv(void* Buffer, int MaxToRecv);
|
2017-07-27 19:52:58 +08:00
|
|
|
|
|
|
|
int getsendtime(int& _out_Second,int& _out_uSecond);
|
|
|
|
int getrecvtime(int& _out_Second,int& _out_uSecond);
|
|
|
|
|
|
|
|
int setsendtime(int Second);
|
|
|
|
int setrecvtime(int Second);
|
|
|
|
|
|
|
|
private:
|
|
|
|
sock(int);
|
|
|
|
friend class serversock;
|
|
|
|
|
2017-08-15 09:54:10 +08:00
|
|
|
struct _impl;
|
|
|
|
_impl* _pp;
|
2017-07-27 19:52:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class serversock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
serversock();
|
|
|
|
serversock(const serversock&)=delete;
|
|
|
|
serversock& operator = (const serversock&) =delete;
|
|
|
|
serversock(serversock&&)=delete;
|
|
|
|
serversock& operator = (serversock&&) =delete;
|
|
|
|
~serversock();
|
|
|
|
|
2017-08-15 09:54:10 +08:00
|
|
|
/// Return:
|
|
|
|
/// 0: Bind Succeed. No Error.
|
|
|
|
/// -1: bind() call error. See errno.
|
|
|
|
/// -2: This socket has been created before.
|
|
|
|
/// -3: socket() call error. Failed to create socket. See errno.
|
2017-07-27 19:52:58 +08:00
|
|
|
int bind(int Port);
|
2017-08-15 09:54:10 +08:00
|
|
|
|
2017-07-27 19:52:58 +08:00
|
|
|
int set_reuse();
|
2017-08-15 09:54:10 +08:00
|
|
|
|
|
|
|
/// Return:
|
|
|
|
/// return what listen() call returns
|
2017-07-27 19:52:58 +08:00
|
|
|
int listen(int MaxCount);
|
|
|
|
|
2017-08-15 09:54:10 +08:00
|
|
|
/// Return:
|
|
|
|
/// 0: Accept Succeed. No Error. _out_s holds the new socket.
|
|
|
|
/// -1: accept() call error. See errno.
|
|
|
|
/// -2: _out_s is a connected socket, which should not be passed in.
|
|
|
|
int accept(sock& _out_s);
|
2017-07-27 19:52:58 +08:00
|
|
|
private:
|
2017-08-15 09:54:10 +08:00
|
|
|
struct _impl;
|
|
|
|
_impl* _pp;
|
2017-07-27 19:52:58 +08:00
|
|
|
};
|
|
|
|
|
2018-04-23 15:56:05 +08:00
|
|
|
class udpsock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
udpsock();
|
|
|
|
udpsock(const udpsock&) = delete;
|
|
|
|
udpsock& operator = (const udpsock&) = delete;
|
|
|
|
udpsock(udpsock&&);
|
|
|
|
udpsock& operator = (udpsock&&);
|
|
|
|
~udpsock();
|
|
|
|
|
|
|
|
/// Explict bind() call is only need when you have to receive data.
|
|
|
|
int bind(int Port);
|
|
|
|
|
|
|
|
int sendto(const std::string& IPStr, int Port, const void* buffer, int length);
|
|
|
|
|
|
|
|
/// Must call bind() before calling recvfrom().
|
|
|
|
int recvfrom(std::string& fromIP, void* buffer, int bufferLength);
|
|
|
|
|
|
|
|
int getlasterror();
|
|
|
|
private:
|
|
|
|
struct _impl;
|
|
|
|
_impl* _pp;
|
|
|
|
};
|
|
|
|
|
2017-08-15 09:54:10 +08:00
|
|
|
/// Net Tools
|
2017-07-27 19:52:58 +08:00
|
|
|
int DNSResolve(const std::string& HostName,std::string& _out_IPStr);
|
2018-02-11 15:41:52 +08:00
|
|
|
|
|
|
|
#endif // _gsock_h
|