fixed one bug in icmp_stream that should set read_ready flag to 0;

icmp_chat can be used in fiber mode;
This commit is contained in:
zhengshuxin 2017-08-15 11:01:58 +08:00
parent f1037f97ce
commit 267bd5db63
5 changed files with 69 additions and 40 deletions

View File

@ -1,6 +1,6 @@
--- ---
Language: Cpp Language: Cpp
DisableFormat: false DisableFormat: true
AccessModifierOffset: -8 AccessModifierOffset: -8
AlignAfterOpenBracket: DontAlign AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: true AlignConsecutiveAssignments: true

View File

@ -1,7 +1,13 @@
修改历史列表: 修改历史列表:
------------------------------------------------------------------------ ------------------------------------------------------------------------
261) 2017.8.15
261.1) bugfix: icmp_stream.c 中函数 icmp_read 应该设置 read_ready = 0以清除
在异步 IO 模式与 event 事件引擎设置的该标志位,否则会造成 IO 死循环
261.2) feature: icmp_chat.c 中将 ICMP_CHAT::tid 由原来的线程 ID 改为原子加值,
以支持协程模式下使用
260) 2016.5.10 260) 2016.5.10
260.1) featur: http_hdr_res.c 中的函数 http_hdr_res_parse 取消了对 http_status 260.1) feature: http_hdr_res.c 中的函数 http_hdr_res_parse 取消了对 http_status
的检查,以便于应用使用自定义状态码 的检查,以便于应用使用自定义状态码
------------------------------------------------------------------------ ------------------------------------------------------------------------

View File

@ -9,10 +9,32 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
static unsigned long __unique_id = 0;
static ACL_ATOMIC *__unique_lock = NULL;
static acl_pthread_once_t once_control = ACL_PTHREAD_ONCE_INIT;
static void proc_on_exit(void)
{
if (__unique_lock) {
acl_atomic_free(__unique_lock);
__unique_lock = NULL;
}
}
static void icmp_once(void)
{
__unique_lock = acl_atomic_new();
acl_atomic_set(__unique_lock, &__unique_id);
atexit(proc_on_exit);
}
ICMP_CHAT *icmp_chat_create(ACL_AIO* aio, int check_tid) ICMP_CHAT *icmp_chat_create(ACL_AIO* aio, int check_tid)
{ {
ICMP_CHAT *chat; ICMP_CHAT *chat;
if (acl_pthread_once(&once_control, icmp_once) != 0)
acl_msg_fatal("acl_pthread_once failed %s", acl_last_serror());
chat = (ICMP_CHAT*) acl_mycalloc(1, sizeof(ICMP_CHAT)); chat = (ICMP_CHAT*) acl_mycalloc(1, sizeof(ICMP_CHAT));
chat->aio = aio; chat->aio = aio;
acl_ring_init(&chat->host_head); acl_ring_init(&chat->host_head);
@ -24,7 +46,7 @@ ICMP_CHAT *icmp_chat_create(ACL_AIO* aio, int check_tid)
#elif defined(ACL_WINDOWS) #elif defined(ACL_WINDOWS)
chat->pid = _getpid(); chat->pid = _getpid();
#endif #endif
chat->tid = (unsigned long) acl_pthread_self(); chat->tid = (unsigned long) acl_atomic_int64_fetch_add(__unique_lock, 1);
chat->check_tid = check_tid; chat->check_tid = check_tid;
if (aio != NULL) if (aio != NULL)

View File

@ -113,8 +113,7 @@ int icmp_pkt_unpack(const ICMP_CHAT *chat, const char *buf, int bytes, ICMP_PKT
iphdrlen = iphdr->h_len * 4 ; /* number of 32-bit words *4 = bytes */ iphdrlen = iphdr->h_len * 4 ; /* number of 32-bit words *4 = bytes */
if (bytes < iphdrlen + ICMP_MIN) { if (bytes < iphdrlen + ICMP_MIN) {
acl_msg_error("Too few bytes from %s", acl_msg_error("Too few bytes from %s", inet_ntoa(chat->is->from.sin_addr));
inet_ntoa(chat->is->from.sin_addr));
return (-1); return (-1);
} }

View File

@ -2,42 +2,46 @@
#include "icmp_struct.h" #include "icmp_struct.h"
#include "icmp_private.h" #include "icmp_private.h"
static int icmp_read(ACL_SOCKET fd, void *buf, size_t size, static int icmp_read(ACL_SOCKET fd, void* buf, size_t size,
int timeout acl_unused, ACL_VSTREAM *stream acl_unused, void *arg) int timeout acl_unused, ACL_VSTREAM* stream, void* arg)
{ {
ICMP_STREAM *is = (ICMP_STREAM*) arg; ICMP_STREAM* is = (ICMP_STREAM*) arg;
int ret; int ret;
/* must reset read_ready flag in aio mode -- zsx, 2017.8.15 */
stream->read_ready = 0;
#ifdef ACL_UNIX #ifdef ACL_UNIX
ret = (int) recvfrom(fd, buf, size, 0, (struct sockaddr*) &is->from, ret = (int) recvfrom(fd, buf, size, 0, (struct sockaddr*) &is->from,
(socklen_t*) &is->from_len); (socklen_t*) &is->from_len);
#elif defined(ACL_WINDOWS) #elif defined(ACL_WINDOWS)
ret = recvfrom(fd, (char*) buf, (int) size, 0, ret = recvfrom(fd, (char*) buf, (int) size, 0,
(struct sockaddr*) &is->from, &is->from_len); (struct sockaddr*) &is->from, &is->from_len);
#else #else
#error "unknown OS" #error "unknown OS"
#endif #endif
if (ret < 0) if (ret < 0)
acl_msg_error("%s(%d): recvfrom error(%s)", acl_msg_error("%s(%d): recvfrom error(%s)", __FILE__,
__FILE__, __LINE__, acl_last_serror()); __LINE__, acl_last_serror());
return (ret); return (ret);
} }
static int icmp_write(ACL_SOCKET fd, const void *buf, size_t size, static int icmp_write(ACL_SOCKET fd, const void* buf, size_t size,
int timeout acl_unused, ACL_VSTREAM *stream acl_unused, void *arg) int timeout acl_unused, ACL_VSTREAM* stream acl_unused, void* arg)
{ {
ICMP_STREAM *is = (ICMP_STREAM*) arg; ICMP_STREAM* is = (ICMP_STREAM*) arg;
int ret; int ret;
#ifdef ACL_UNIX #ifdef ACL_UNIX
ret = (int) sendto(fd, buf, size, 0, (struct sockaddr*) &is->curr_host->dest, ret = (int) sendto(fd, buf, size, 0,
(struct sockaddr*) &is->curr_host->dest,
sizeof(is->curr_host->dest)); sizeof(is->curr_host->dest));
#elif defined(ACL_WINDOWS) #elif defined(ACL_WINDOWS)
ret = sendto(fd, (const char*) buf, (int) size, 0, ret = sendto(fd, (const char*) buf, (int) size, 0,
(struct sockaddr*) &is->curr_host->dest, (struct sockaddr*) &is->curr_host->dest,
sizeof(is->curr_host->dest)); sizeof(is->curr_host->dest));
#else #else
#error "unknown OS" #error "unknown OS"
#endif #endif
return (ret); return (ret);
} }
@ -51,11 +55,11 @@ void icmp_stream_close(ICMP_STREAM* is)
acl_myfree(is); acl_myfree(is);
} }
ICMP_STREAM* icmp_stream_open(ACL_AIO *aio) ICMP_STREAM* icmp_stream_open(ACL_AIO* aio)
{ {
const char* myname = "icmp_stream_open"; const char* myname = "icmp_stream_open";
ACL_SOCKET fd; ACL_SOCKET fd;
ICMP_STREAM *is = (ICMP_STREAM*) acl_mycalloc(1, sizeof(ICMP_STREAM)); ICMP_STREAM* is = (ICMP_STREAM*) acl_mycalloc(1, sizeof(ICMP_STREAM));
is->from_len = sizeof(is->from); is->from_len = sizeof(is->from);
@ -63,34 +67,32 @@ ICMP_STREAM* icmp_stream_open(ACL_AIO *aio)
fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (fd == ACL_SOCKET_INVALID) if (fd == ACL_SOCKET_INVALID)
acl_msg_fatal("%s(%d): socket create error", myname, __LINE__); acl_msg_fatal(
"%s(%d): socket create error", myname, __LINE__);
is->vstream = acl_vstream_fdopen(fd, O_RDWR, 1024, 0, ACL_VSTREAM_TYPE_SOCK); is->vstream = acl_vstream_fdopen(
acl_vstream_ctl(is->vstream, fd, O_RDWR, 1024, 0, ACL_VSTREAM_TYPE_SOCK);
ACL_VSTREAM_CTL_READ_FN, icmp_read, acl_vstream_ctl(is->vstream, ACL_VSTREAM_CTL_READ_FN, icmp_read,
ACL_VSTREAM_CTL_WRITE_FN, icmp_write, ACL_VSTREAM_CTL_WRITE_FN, icmp_write, ACL_VSTREAM_CTL_CONTEXT,
ACL_VSTREAM_CTL_CONTEXT, is, is, ACL_VSTREAM_CTL_END);
ACL_VSTREAM_CTL_END);
/* 如果采用异步方式,则打开异步流 */ /* 如果采用异步方式,则打开异步流 */
if (aio) if (aio) is->astream = acl_aio_open(aio, is->vstream);
is->astream = acl_aio_open(aio, is->vstream);
return (is); return (is);
} }
void icmp_stream_reopen(ACL_AIO *aio, ICMP_STREAM *is) void icmp_stream_reopen(ACL_AIO* aio, ICMP_STREAM* is)
{ {
ACL_SOCKET fd = ACL_VSTREAM_SOCK(is->vstream); ACL_SOCKET fd = ACL_VSTREAM_SOCK(is->vstream);
ACL_VSTREAM_SOCK(is->vstream) = ACL_SOCKET_INVALID; /* ˇŔÖšÔ­SOCKETąťšŘąŐ:) */ ACL_VSTREAM_SOCK(is->vstream) =
ACL_SOCKET_INVALID; /* ·ÀÖ¹Ô­SOCKET±»¹Ø±Õ:) */
is->vstream = acl_vstream_fdopen(fd, O_RDWR, 1024, 0, ACL_VSTREAM_TYPE_SOCK); is->vstream = acl_vstream_fdopen(
acl_vstream_ctl(is->vstream, fd, O_RDWR, 1024, 0, ACL_VSTREAM_TYPE_SOCK);
ACL_VSTREAM_CTL_READ_FN, icmp_read, acl_vstream_ctl(is->vstream, ACL_VSTREAM_CTL_READ_FN, icmp_read,
ACL_VSTREAM_CTL_WRITE_FN, icmp_write, ACL_VSTREAM_CTL_WRITE_FN, icmp_write, ACL_VSTREAM_CTL_CONTEXT,
ACL_VSTREAM_CTL_CONTEXT, is, is, ACL_VSTREAM_CTL_END);
ACL_VSTREAM_CTL_END);
if (aio) if (aio) is->astream = acl_aio_open(aio, is->vstream);
is->astream = acl_aio_open(aio, is->vstream);
} }