acl/lib_acl_cpp/include/acl_cpp/stream/server_socket.hpp

150 lines
3.3 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
2018-02-13 18:03:24 +08:00
#include "../stdlib/string.hpp"
#include "../stdlib/noncopyable.hpp"
#if defined(_WIN32) || defined(_WIN64)
#include <WinSock2.h>
#endif
2014-11-19 00:25:21 +08:00
namespace acl {
class socket_stream;
2018-09-22 09:58:21 +08:00
enum {
OPEN_FLAG_NONE = 0,
OPEN_FLAG_NONBLOCK = 1, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
OPEN_FLAG_REUSEPORT = 1 << 1, // <20>˿ڸ<CBBF><DAB8>ã<EFBFBD>Ҫ<EFBFBD><D2AA> Linux3.0 <20><><EFBFBD><EFBFBD>
OPEN_FLAG_EXCLUSIVE = 1 << 2, // <20>Ƿ<EFBFBD><C7B7><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>õ<EFBFBD>ַ
2018-09-22 09:58:21 +08:00
};
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽ӿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>տͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD>
*/
class ACL_CPP_API server_socket : public noncopyable
2014-11-19 00:25:21 +08:00
{
public:
#if 0
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> open <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2014-11-19 00:25:21 +08:00
* @param backlog {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽ӿڶ<EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD>
* @param block {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
*/
server_socket(int backlog, bool block);
#endif
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param flag {unsigned} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>μ<EFBFBD> OPEN_FLAG_XXX
* @param backlog {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽ӿڶ<EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD>
*/
server_socket(unsigned flag, int backlog);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD>ٵ<EFBFBD><EFBFBD><EFBFBD> open <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param sstream {ACL_VSTREAM*} <EFBFBD>ⲿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣬱<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷţ<EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>йرոü<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
server_socket(ACL_VSTREAM* sstream);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD>ٵ<EFBFBD><EFBFBD><EFBFBD> open <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param fd {ACL_SOCKET} <EFBFBD>ⲿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷţ<EFBFBD>
* <EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>йرոü<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
#if defined(_WIN32) || defined(_WIN64)
server_socket(SOCKET fd);
#else
server_socket(int fd);
#endif
server_socket(void);
~server_socket(void);
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>ַ
* @param addr {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽΪ<EFBFBD><EFBFBD>
* ip:port<EFBFBD><EFBFBD><EFBFBD><EFBFBD> unix <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽ӿڣ<EFBFBD><EFBFBD><EFBFBD>ʽΪ<EFBFBD><EFBFBD>
* /path/xxx
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
*/
bool open(const char* addr);
2018-01-11 19:13:31 +08:00
/**
* <EFBFBD>жϵ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽ӿ<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {bool}
*/
bool opened(void) const;
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD>ر<EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD>򿪵ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽ӿ<EFBFBD>
* @return {bool} <EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>
*/
bool close(void);
2014-11-19 00:25:21 +08:00
2018-01-11 19:13:31 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽ӿڴӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
* @return {SOCKET} <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><EFBFBD><EFBFBD>
*/
#if defined(_WIN32) || defined(_WIN64)
SOCKET unbind(void);
2018-01-11 19:13:31 +08:00
#else
int unbind(void);
2018-01-11 19:13:31 +08:00
#endif
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>տͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ > 0 ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD>ʱ<EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>տͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD>ÿͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD>򷵻<EFBFBD> NULL
* @param etimed {bool*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>¸ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* NULL<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ true
* @return {socket_stream*} <EFBFBD><EFBFBD><EFBFBD>ؿձ<EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܻ<EFBFBD><EFBFBD><EFBFBD>ʱ
2014-11-19 00:25:21 +08:00
*/
socket_stream* accept(int timeout = 0, bool* etimed = NULL);
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* @return {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>ǿ<EFBFBD>ָ<EFBFBD><EFBFBD>
*/
const char* get_addr(void) const
2014-11-19 00:25:21 +08:00
{
2018-02-13 18:03:24 +08:00
return addr_.c_str();
2014-11-19 00:25:21 +08:00
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽ӿ<EFBFBD>
* @return {int}
*/
#if defined(_WIN32) || defined(_WIN64)
SOCKET sock_handle(void) const
2014-11-19 00:25:21 +08:00
#else
int sock_handle(void) const
2014-11-19 00:25:21 +08:00
#endif
{
return fd_;
}
/**
* <EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>ӳٽ<EFBFBD><EFBFBD>չ<EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ž<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>Ӧ<EFBFBD>ã<EFBFBD>Ŀǰ<EFBFBD>ù<EFBFBD><EFBFBD>ܽ<EFBFBD>֧<EFBFBD><EFBFBD> Linux
* @param timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڹ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
* Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><EFBFBD>ظ<EFBFBD>Ӧ<EFBFBD><EFBFBD>
*/
void set_tcp_defer_accept(int timeout);
private:
int backlog_;
unsigned open_flag_;
bool unix_sock_;
2018-02-13 18:03:24 +08:00
string addr_;
2014-11-19 00:25:21 +08:00
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
SOCKET fd_;
SOCKET fd_local_;
2014-11-19 00:25:21 +08:00
#else
int fd_;
int fd_local_;
2014-11-19 00:25:21 +08:00
#endif
};
} // namespace acl