Skip to content

Commit

Permalink
support IPV6.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuxin committed Apr 7, 2017
1 parent d167d8f commit b73d188
Show file tree
Hide file tree
Showing 50 changed files with 1,547 additions and 339 deletions.
8 changes: 8 additions & 0 deletions lib_acl/changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
�޸���ʷ�б���

------------------------------------------------------------------------

579) 2017.4.7
579.1) feature: �����������������ģ��֧�� IPV6

578) 2017.4.5
578.1) bugfix: acl_atomic.c/acl_mbox.c/acl_pthread_mutext.c �����ڴ�й¶����

577) 2017.3.30
577.1) feature: json ������ڴ��� json �ַ���ʱĬ�ϲ��Զ����ӿո񣬵�����������
���ò����Ա����Զ����ӿո�Ӷ�ʹ json �ַ������ɶ�
Expand Down
27 changes: 14 additions & 13 deletions lib_acl/include/net/acl_connect.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,26 @@ ACL_API int acl_timed_connect(ACL_SOCKET fd, const struct sockaddr * sa,

/**
* 远程连接网络服务器地址
* @param addr {const char*} 远程服务器的监听地址,如:192.168.0.1:80, 如果需要绑定本地
* 的地址,则格式为: {local_ip}@{remote_addr}, 如: 60.28.250.199@www.sina.com:80
* @param block_mode {int} 阻塞模式还是非阻塞模式, ACL_BLOCKING 或 ACL_NON_BLOCKING
* @param timeout {int} 连接超时时间,如果 block_mode 为 ACL_NON_BLOCKING 则该值将被忽略
* @param addr {const char*} 远程服务器的监听地址,如:192.168.0.1:80, 如果需
* 要绑定本地的地址,则格式为: {local_ip}@{remote_addr},
* 如: 60.28.250.199@www.sina.com:80
* @param blocking {int} 阻塞模式还是非阻塞模式, ACL_BLOCKING 或 ACL_NON_BLOCKING
* @param timeout {int} 连接超时时间,如果 blocking 为 ACL_NON_BLOCKING 则该值将被忽略
* @return {ACL_SOCKET} 如果返回 ACL_SOCKET_INVALID 表示连接失败
*/
ACL_API ACL_SOCKET acl_inet_connect(const char *addr, int block_mode, int timeout);
ACL_API ACL_SOCKET acl_inet_connect(const char *addr, int blocking, int timeout);

/**
* 远程连接网络服务器地址
* @param addr {const char*} 远程服务器的监听地址,如:192.168.0.1:80,
* 当本机有多个网卡地址且想通过某个指定网卡连接服务器时的地址格式:
local_ip@remote_ip:remote_port,如:192.168.1.1@211.150.111.12:80
* @param block_mode {int} 阻塞模式还是非阻塞模式, ACL_BLOCKING 或 ACL_NON_BLOCKING
* @param timeout {int} 连接超时时间,如果 block_mode 为 ACL_NON_BLOCKING 则该值将被忽略
* local_ip@remote_ip:remote_port,如:192.168.1.1@211.150.111.12:80
* @param blocking {int} 阻塞模式还是非阻塞模式, ACL_BLOCKING 或 ACL_NON_BLOCKING
* @param timeout {int} 连接超时时间,如果 blocking 为 ACL_NON_BLOCKING 则该值将被忽略
* @param h_error {int*} 当连接失败时存储失败原因错误号
* @return {ACL_SOCKET} 如果返回 ACL_SOCKET_INVALID 表示连接失败
*/
ACL_API ACL_SOCKET acl_inet_connect_ex(const char *addr, int block_mode,
ACL_API ACL_SOCKET acl_inet_connect_ex(const char *addr, int blocking,
int timeout, int *h_error);

#ifdef ACL_UNIX
Expand All @@ -69,15 +70,15 @@ ACL_API ACL_SOCKET acl_inet_connect_ex(const char *addr, int block_mode,
/**
* 连接监听域套接字服务器
* @param addr {const char*} 服务器监听的域套接字全路径, 如: /tmp/test.sock
* @param block_mode {int} 阻塞模式还是非阻塞模式, ACL_BLOCKING 或 ACL_NON_BLOCKING
* @param timeout {int} 连接超时时间,如果 block_mode 为 ACL_NON_BLOCKING 则该值将被忽略
* @param blocking {int} 阻塞模式还是非阻塞模式, ACL_BLOCKING 或 ACL_NON_BLOCKING
* @param timeout {int} 连接超时时间,如果 blocking 为 ACL_NON_BLOCKING 则该值将被忽略
* @return {ACL_SOCKET} 如果返回 ACL_SOCKET_INVALID 表示连接失败
*/
ACL_API ACL_SOCKET acl_unix_connect(const char *addr, int block_mode, int timeout);
ACL_API ACL_SOCKET acl_unix_connect(const char *addr, int blocking, int timeout);

/* in acl_stream_connect.c */
#ifdef SUNOS5
ACL_API int acl_stream_connect(const char *path, int block_mode, int unused_timeout);
ACL_API int acl_stream_connect(const char *path, int blocking, int timeout);
#endif

#endif
Expand Down
2 changes: 2 additions & 0 deletions lib_acl/include/net/acl_sane_inet.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ ACL_API const char *acl_inet_ntoa(struct in_addr in, char *dst, size_t size);
* @return {int} 0: 是; -1: 否
*/
ACL_API int acl_is_ip(const char *ip);
ACL_API int acl_is_ipv4(const char *ip);
ACL_API int acl_is_ipv6(const char *ip);

/**
* 判断所给的 ip 地址是否符合 xxx.xxx.xxx.xxx 格式
Expand Down
1 change: 1 addition & 0 deletions lib_acl/include/stdlib/acl_define_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

# include <assert.h>
# include <sys/types.h>
# include <netdb.h>

# ifndef acl_assert
# define acl_assert assert
Expand Down
2 changes: 2 additions & 0 deletions lib_acl/include/stdlib/acl_define_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
# include <winsock.h>
# endif

# include <ws2tcpip.h> /* for getaddrinfo */

# ifdef ACL_BCB_COMPILER
# pragma hdrstop
# endif
Expand Down
38 changes: 38 additions & 0 deletions lib_acl/include/stdlib/acl_mbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,49 @@ extern "C" {

typedef struct ACL_MBOX ACL_MBOX;

/**
* 创建无锁消息队列对象
* @return {ACL_MBOX}
*/
ACL_API ACL_MBOX *acl_mbox_create(void);

/**
* 释放无锁消息队列对象
* @param mbox {ACL_MBOX*} 消息队列对象
* @param free_fn {void (*)(void*)} 非空时用来释放当前存在于消息队列中的对象
*/
ACL_API void acl_mbox_free(ACL_MBOX *mbox, void (*free_fn)(void*));

/**
* 向消息队列中添加动态消息对象
* @param mbox {ACL_MBOX*} 消息队列对象
* @param msg {void*}
* @return {int} 发送成功返回 0,否则返回 -1
*/
ACL_API int acl_mbox_send(ACL_MBOX *mbox, void *msg);

/**
* 从消息队列中读取消息
* @param mbox {ACL_MBOX*} 消息队列对象
* @param timeout {int} 等待超时时间(秒)
* @param success {int*} 存储操作是否成功的结果, 0 表示出错,非 0 表示成功
* @return {void*} 返回读到的消息对象,如果返回 NULL 时还需判断 success 的值,以此来
* 判断读操作是否成功,如果返回非 NULL 表示成功读到一条消息
*/
ACL_API void *acl_mbox_read(ACL_MBOX *mbox, int timeout, int *success);

/**
* 获得当前消息队列已经成功发送的消息数
* @param mbox {ACL_MBOX*} 消息队列对象
* @return {size_t}
*/
ACL_API size_t acl_mbox_nsend(ACL_MBOX *mbox);

/**
* 获得当前消息队列已经成功接收到的消息数
* @param mbox {ACL_MBOX*} 消息队列对象
* @return {size_t}
*/
ACL_API size_t acl_mbox_nread(ACL_MBOX *mbox);

#ifdef __cplusplus
Expand Down
24 changes: 7 additions & 17 deletions lib_acl/include/stdlib/acl_mystring.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,15 @@ extern "C" {
* @param _size {int} 目的内存区的空间大小
*/
#ifndef ACL_SAFE_STRNCPY
#if defined(_WIN32) || defined(_WIN64)
#define ACL_SAFE_STRNCPY(_obj, _src, _size) do { \
size_t _n = strlen(_src); \
_n = _n > (size_t ) _size - 1? (size_t) _size - 1 : _n; \
memcpy(_obj, _src, _n); \
_obj[_n] = 0; \
} while (0)
#else
#define ACL_SAFE_STRNCPY(_obj, _src, _size) do { \
if (_size > 0) { \
strncpy(_obj, _src, _size); \
if ((int)_size > 0) \
_obj[_size - 1] = 0; \
else \
_obj[_size] = 0; \
} \
#define ACL_SAFE_STRNCPY(_obj, _src, _size) do { \
if (_size > 0) { \
size_t _n = strlen(_src); \
_n = _n > (size_t ) _size - 1? (size_t) _size - 1 : _n; \
memcpy(_obj, _src, _n); \
_obj[_n] = 0; \
} \
} while (0)
#endif
#endif

/**
* 将字符串转换为小写,直接在原内存空间进行操作
Expand Down
19 changes: 19 additions & 0 deletions lib_acl/include/stdlib/acl_sys_patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ ACL_API int acl_socket_end(void);
*/
ACL_API int acl_socket_close(ACL_SOCKET fd);

/**
* 禁止套接口的发送与接收
* @param fd {ACL_SOCKET} 套接字
* @param how {int}
* @return {int} 返回 0 表示操作成功,否则表示出错
*/
#if defined(_WIN32) || defined(_WIN64)
# ifndef SHUT_RD
# define SHUT_RD SD_RECEIVE
# endif
# ifndef SHUT_WR
# define SHUT_WR SD_SEND
# endif
# ifndef SHUT_RDWR
# define SHUT_RDWR SD_BOTH
# endif
#endif
ACL_API int acl_socket_shutdown(ACL_SOCKET fd, int how);

/**
* 从套接字读数据
* @param fd {ACL_SOCKET} 网络套接字
Expand Down
23 changes: 13 additions & 10 deletions lib_acl/include/thread/acl_pthread.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef ACL_PTHREAD_WIN32_INCLUDE_H
#define ACL_PTHREAD_WIN32_INCLUDE_H
#ifndef __ACL_PTHREAD_INCLUDE_H__
#define __ACL_PTHREAD_INCLUDE_H__

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -35,13 +35,11 @@ typedef pthread_once_t acl_pthread_once_t;
#define acl_pthread_detach pthread_detach
#define acl_pthread_once pthread_once
#define acl_pthread_join pthread_join
#define acl_pthread_mutex_destroy pthread_mutex_destroy
#define acl_pthread_mutex_init pthread_mutex_init
#define acl_pthread_mutex_lock pthread_mutex_lock
#define acl_pthread_mutex_unlock pthread_mutex_unlock
#define acl_pthread_mutex_trylock pthread_mutex_trylock
#define acl_pthread_cond_init pthread_cond_init
/* #define acl_pthread_cond_create pthread_cond_create */
#define acl_pthread_cond_destroy pthread_cond_destroy
#define acl_pthread_cond_signal pthread_cond_signal
#define acl_pthread_cond_broadcast pthread_cond_broadcast
Expand Down Expand Up @@ -147,7 +145,6 @@ ACL_API int acl_pthread_detach(acl_pthread_t thread);
ACL_API int acl_pthread_join(acl_pthread_t thread, void **thread_return);

/* in acl_pthread_mutex.c */
ACL_API int acl_pthread_mutex_destroy(acl_pthread_mutex_t *mutex);
ACL_API int acl_pthread_mutex_init(acl_pthread_mutex_t *mutex,
const acl_pthread_mutexattr_t *mattr);
ACL_API int acl_pthread_mutex_lock(acl_pthread_mutex_t *mutex);
Expand All @@ -158,7 +155,11 @@ ACL_API int acl_pthread_mutex_unlock(acl_pthread_mutex_t *mutex);
/* in acl_pthread_cond.c */
ACL_API int acl_pthread_cond_init(acl_pthread_cond_t *cond,
acl_pthread_condattr_t *cond_attr);
ACL_API acl_pthread_cond_t * acl_pthread_cond_create(void);
ACL_API acl_pthread_cond_t *acl_thread_cond_create(void);
#ifndef acl_pthread_cond_create
#define acl_pthread_cond_create acl_thread_cond_create
#endif

ACL_API int acl_pthread_cond_destroy(acl_pthread_cond_t *cond);
ACL_API int acl_pthread_cond_signal(acl_pthread_cond_t *cond);
ACL_API int acl_pthread_cond_broadcast(acl_pthread_cond_t *cond);
Expand All @@ -177,7 +178,12 @@ ACL_API int acl_thread_mutex_unlock(acl_pthread_mutex_t *mutex);
ACL_API int acl_thread_mutex_nested(acl_pthread_mutex_t *mutex);

/* in acl_pthread_mutex.c */
ACL_API acl_pthread_mutex_t *acl_pthread_mutex_create(void);
ACL_API acl_pthread_mutex_t *acl_thread_mutex_create(void);
#ifndef acl_pthread_mutex_create
#define acl_pthread_mutex_create acl_thread_mutex_create
#endif

ACL_API int acl_pthread_mutex_destroy(acl_pthread_mutex_t *mutex);

/* in acl_pthread.c */
ACL_API int acl_pthread_atexit_add(void *arg, void (*free_callback)(void*));
Expand All @@ -194,9 +200,6 @@ ACL_API void acl_pthread_tls_once_set(acl_pthread_once_t control_once);
ACL_API acl_pthread_key_t acl_pthread_tls_key_get(void);
ACL_API void acl_pthread_tls_key_set(acl_pthread_key_t key);

/* in acl_pthread_cond.c */
ACL_API acl_pthread_cond_t * acl_pthread_cond_create(void);

#ifdef __cplusplus
}
#endif
Expand Down
31 changes: 29 additions & 2 deletions lib_acl/samples/connect/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "lib_acl.h"
#include <netdb.h>
#include <list>

static void test(const char* addr)
{
std::list<ACL_VSTREAM*> conns;

for (int i = 0; i < 10000; i++)
for (int i = 0; i < 1; i++)
{
ACL_VSTREAM* client = acl_vstream_connect(addr, ACL_BLOCKING, 10, 10, 4096);
if (client == NULL)
Expand All @@ -26,9 +27,35 @@ static void test(const char* addr)
printf("Exit now ok\r\n");
}

static void get_name(void)
{
const char *name = "localhost";
struct addrinfo hints, *res, *res0;

memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ALL;

int error = getaddrinfo(name, "8089", &hints, &res0);
if (error) {
printf("getaddrinfo error %s\r\n", acl_last_serror());
return;
}

for (res = res0; res; res = res->ai_next) {
printf("ai_family: %d, len: %d\r\n",
res->ai_family, (int) res->ai_addrlen);
}

freeaddrinfo(res0);
}

int main(int argc, char *argv[])
{
test(argv[1]);
acl_msg_stdout_enable(1);
get_name();
test(argc >= 2 ? argv[1] : "localhost:8809");
return 0;

ACL_VSTREAM *client;
Expand Down
15 changes: 10 additions & 5 deletions lib_acl/samples/server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
static void init(void)
{
acl_lib_init();
acl_msg_stdout_enable(1);
}

static void end(void)
Expand All @@ -26,13 +27,12 @@ static void thread_run(void *arg)
}

buf[ret] = 0;
printf("(%s)\n", buf);
(void) acl_vstream_write(client, buf, ret);
acl_vstream_close(client);
}

static void run(void)
static void run(const char *addr)
{
const char *addr = "0.0.0.0:8089";
ACL_VSTREAM *sstream = acl_vstream_listen(addr, 128);
acl_pthread_pool_t *pool;

Expand All @@ -49,16 +49,21 @@ static void run(void)
acl_msg_error("accept error(%s)", acl_last_serror());
break;
}
printf("accept one client\r\n");
acl_pthread_pool_add(pool, thread_run, client);
}

acl_vstream_close(sstream);
}

int main(int argc acl_unused, char *argv[] acl_unused)
int main(int argc, char *argv[])
{
const char *addr = "0:8809";

if (argc >= 2)
addr = argv[1];
init();
run();
run(addr);
end();
return (0);
}
2 changes: 1 addition & 1 deletion lib_acl/src/event/events_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extern "C" {
#elif defined(SUNOS5)
# define ACL_EVENTS_KERNEL_STYLE ACL_EVENTS_STYLE_DEVPOLL
# define USE_FDMAP
#elif defined(FREEBSD) || defined(MACOSX)
#elif defined(ACL_FREEBSD) || defined(ACL_MACOSX)
# define ACL_EVENTS_KERNEL_STYLE ACL_EVENTS_STYLE_KQUEUE
#else
# undef ACL_EVENTS_KERNEL_STYLE
Expand Down
Loading

0 comments on commit b73d188

Please sign in to comment.