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

249 lines
7.4 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
#include <WinSock2.h>
#endif
2017-06-02 14:47:24 +08:00
#include "istream.hpp"
#include "ostream.hpp"
2014-11-19 00:25:21 +08:00
struct ACL_VSTREAM;
namespace acl {
class ACL_CPP_API socket_stream
: public istream
, public ostream
{
public:
2018-09-16 21:49:58 +08:00
socket_stream(void);
virtual ~socket_stream(void);
2014-11-19 00:25:21 +08:00
/**
*
* @param fd
* @param udp_mode {bool} UDP
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
#if defined(_WIN32) || defined(_WIN64)
2017-06-26 18:10:47 +08:00
bool open(SOCKET fd, bool udp_mode = false);
2014-11-19 00:25:21 +08:00
#else
2017-06-26 18:10:47 +08:00
bool open(int fd, bool udp_mode = false);
2014-11-19 00:25:21 +08:00
#endif
/**
* ACL_VSTREAM
2014-11-19 00:25:21 +08:00
* @param vstream {ACL_VSTREAM*}
* @param udp_mode {bool} UDP
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
2017-06-26 18:10:47 +08:00
bool open(ACL_VSTREAM* vstream, bool udp_mode = false);
2014-11-19 00:25:21 +08:00
/**
*
* @param addr {const char*} , (UNIX平台),
* /tmp/test.sock; TCP服务器:
* [${local_ip}@]${remote_addr}, : 60.28.250.199@www.sina.com:80,
* : 60.28.250.199, www.sina.com 80,
* OS自动绑定本地 IP www.sina.com:80
* @param conn_timeout {int} ()
* @param rw_timeout {int} ()
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool open(const char* addr, int conn_timeout, int rw_timeout);
/**
* UDP UDP
* @param addr {const char*} ip:port
* @param rw_timeout {int} ()
* @param flag {unsigned}
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool bind_udp(const char* addr, int rw_timeout = 0, unsigned flag = 0);
2014-11-19 00:25:21 +08:00
2017-04-07 20:13:08 +08:00
/**
*
2017-04-07 20:13:08 +08:00
* @return {bool}
*/
bool shutdown_read(void);
/**
*
2017-04-07 20:13:08 +08:00
* @return {bool}
*/
bool shutdown_write(void);
/**
*
2017-04-07 20:13:08 +08:00
* @return {bool}
*/
bool shutdown_readwrite(void);
2014-11-19 00:25:21 +08:00
/**
*
* @return {ACL_SOCKET} - 1(UNIX )
* INVALID_SOCKET(win32平台)
2014-11-19 00:25:21 +08:00
*/
#if defined(_WIN32) || defined(_WIN64)
2018-09-16 21:49:58 +08:00
SOCKET sock_handle(void) const;
2014-11-19 00:25:21 +08:00
#else
2018-09-16 21:49:58 +08:00
int sock_handle(void) const;
2014-11-19 00:25:21 +08:00
#endif
/**
*
*
*
* close/open (
* )
* @return {ACL_SOCKET} ACL_SOCKET_INVALID
*
2014-11-19 00:25:21 +08:00
*/
#if defined(_WIN32) || defined(_WIN64)
2018-09-16 21:49:58 +08:00
SOCKET unbind_sock(void);
2014-11-19 00:25:21 +08:00
#else
2018-09-16 21:49:58 +08:00
int unbind_sock(void);
2014-11-19 00:25:21 +08:00
#endif
2018-09-16 21:49:58 +08:00
/**
* socket
* @return {int} AF_INET, AF_INT6, AF_UNIX -1
2018-09-16 21:49:58 +08:00
*/
int sock_type(void) const;
2014-11-19 00:25:21 +08:00
/**
*
* @param full {bool} IP:PORT
* false IP IP:PORT
* @return {const char*} == '\0'
*
2014-11-19 00:25:21 +08:00
*/
const char* get_peer(bool full = false) const;
/**
* IP
* @return {const char*} == '\0'
*
2014-11-19 00:25:21 +08:00
*/
2018-09-16 21:49:58 +08:00
const char* get_peer_ip(void) const;
2014-11-19 00:25:21 +08:00
/**
* TCP
* UDP
*
* @param addr {const char*} ip:port
* @return {bool} false
2014-11-19 00:25:21 +08:00
*/
bool set_peer(const char* addr);
/**
*
* @param full {bool} IP:PORT
* false IP IP:PORT
* @return {const char*} == ""
*
2014-11-19 00:25:21 +08:00
*/
const char* get_local(bool full = false) const;
/**
* IP
* @return {const char*} == ""
*
2014-11-19 00:25:21 +08:00
*/
2018-09-16 21:49:58 +08:00
const char* get_local_ip(void) const;
2014-11-19 00:25:21 +08:00
/**
*
* @param addr {const char*} ip:port
* @return {bool} false
2014-11-19 00:25:21 +08:00
*/
bool set_local(const char* addr);
/**
* (使)
* @return {bool} false
* true
2014-11-19 00:25:21 +08:00
*/
2018-09-16 21:49:58 +08:00
bool alive(void) const;
2014-11-19 00:25:21 +08:00
/**
* TCP nodelay
* @param on {bool} true false
2014-11-19 00:25:21 +08:00
* @return {socket_stream&}
*/
socket_stream& set_tcp_nodelay(bool on);
/**
* TCP SO_LINGER
* @param on {bool} SO_LINGER
* @param linger {int} SO_LINGER打开时 timed_wait
2014-11-19 00:25:21 +08:00
* @return {socket_stream&}
*/
socket_stream& set_tcp_solinger(bool on, int linger);
/**
* TCP
* @param size {int}
2014-11-19 00:25:21 +08:00
* @return {socket_stream&}
*/
socket_stream& set_tcp_sendbuf(int size);
/**
* TCP
* @param size {int}
2014-11-19 00:25:21 +08:00
* @return {socket_stream&}
*/
socket_stream& set_tcp_recvbuf(int size);
2015-01-14 23:35:23 +08:00
/**
* TCP
* @param on {bool} true
*
2015-01-14 23:35:23 +08:00
* @return {socket_stream&}
*/
socket_stream& set_tcp_non_blocking(bool on);
2014-11-19 00:25:21 +08:00
/**
* TCP nodelay
* @return {bool} true false
2014-11-19 00:25:21 +08:00
*/
2018-09-16 21:49:58 +08:00
bool get_tcp_nodelay(void);
2014-11-19 00:25:21 +08:00
/**
* TCP linger
* @param fd {ACL_SOCKET}
* @return {int} -1 linger >= 0
* linger TCP
* TIME_WAIT ()
2014-11-19 00:25:21 +08:00
*/
2018-09-16 21:49:58 +08:00
int get_tcp_solinger(void);
2014-11-19 00:25:21 +08:00
/**
* TCP
* @param fd {ACL_SOCKET}
* @return {int}
2014-11-19 00:25:21 +08:00
*/
2018-09-16 21:49:58 +08:00
int get_tcp_sendbuf(void);
2014-11-19 00:25:21 +08:00
/**
* TCP
* @param fd {ACL_SOCKET}
* @return {int}
2014-11-19 00:25:21 +08:00
*/
2018-09-16 21:49:58 +08:00
int get_tcp_recvbuf(void);
2014-11-19 00:25:21 +08:00
2015-01-14 23:35:23 +08:00
/**
*
2015-01-14 23:35:23 +08:00
* @return {bool}
* UNIX
2015-01-14 23:35:23 +08:00
*/
2018-09-16 21:49:58 +08:00
bool get_tcp_non_blocking(void);
2015-01-14 23:35:23 +08:00
2014-11-19 00:25:21 +08:00
private:
char dummy_[1];
2017-04-11 23:14:39 +08:00
char peer_ip_[256];
char local_ip_[256];
2014-11-19 00:25:21 +08:00
const char* get_ip(const char* addr, char* buf, size_t size);
};
} // namespace acl