improve event source

This commit is contained in:
xianjimli 2019-10-01 08:46:37 +08:00
parent 4c3bfddfd9
commit 4a1a8d1cb4
11 changed files with 34 additions and 11 deletions

View File

@ -3,6 +3,8 @@
* 2019/09/30
* 修改文档的BUG感谢俊杰提供补丁
* edit/mledit的margin从style中获取从属性获取仍然保留但不支持在IDE中设置
* 使用sokol库中的函数获取时间。
* 增加event source。
* 2019/09/29
* 修改combo\_box\_ex获取type的问题感谢尧燊提供补丁

View File

@ -33,7 +33,10 @@ static int32_t tk_istream_tcp_read(tk_istream_t* stream, uint8_t* buff, uint32_t
ret = recv(istream_tcp->sock, buff, max_size, 0);
if (ret <= 0) {
istream_tcp->is_broken = TRUE;
if(errno != EAGAIN) {
perror("recv");
istream_tcp->is_broken = TRUE;
}
}
return ret;

View File

@ -23,6 +23,8 @@
#define TK_SERIAL_HELPER_H
#include "tkc/types_def.h"
#include <errno.h>
#include <string.h>
BEGIN_C_DECLS

View File

@ -23,6 +23,7 @@
#define TK_SOCKET_HELPER_H
#include "tkc/types_def.h"
#include <errno.h>
BEGIN_C_DECLS

View File

@ -26,13 +26,11 @@
BEGIN_C_DECLS
struct _event_source_t;
typedef struct _event_source_t event_source_t;
typedef int32_t (*event_source_get_fd_t)(event_source_t* source);
typedef uint32_t (*event_source_get_wakeup_time_t)(event_source_t* source);
typedef ret_t (*event_source_check_t)(event_source_t* source);
typedef ret_t (*event_source_dispatch_t)(event_source_t* source);
typedef ret_t (*event_source_on_event_t)(event_source_t* source);
/**
* @class event_source_t
@ -53,6 +51,8 @@ struct _event_source_t {
event_source_get_fd_t get_fd;
event_source_dispatch_t dispatch;
event_source_get_wakeup_time_t get_wakeup_time;
event_source_manager_t* manager;
};
/**

View File

@ -44,7 +44,7 @@ uint32_t event_source_fd_get_wakeup_time(event_source_t* source) {
return 0xffff;
}
event_source_t* event_source_fd_create(int fd, event_source_dispatch_t on_event, void* ctx) {
event_source_t* event_source_fd_create(int fd, event_source_on_event_t on_event, void* ctx) {
object_t* obj = NULL;
event_source_t* event_source = NULL;
event_source_fd_t* event_source_fd = NULL;

View File

@ -42,7 +42,7 @@ struct _event_source_fd_t {
int fd;
void* ctx;
void* ctx2;
event_source_dispatch_t on_event;
event_source_on_event_t on_event;
};
/**
@ -51,13 +51,13 @@ struct _event_source_fd_t {
* fd事件源
*
* @param {int} fd select等待的文件描述符
* @param {event_source_dispatch_t} on_event
* @param {event_source_on_event_t} on_event
* @param {void*} ctx on_event函数的上下文
*
* @return {event_source_t*}
*
*/
event_source_t* event_source_fd_create(int fd, event_source_dispatch_t on_event, void* ctx);
event_source_t* event_source_fd_create(int fd, event_source_on_event_t on_event, void* ctx);
#define EVENT_SOURCE_FD(obj) ((event_source_fd_t*)(obj))

View File

@ -69,6 +69,7 @@ ret_t event_source_manager_dispatch(event_source_manager_t* manager) {
ret_t event_source_manager_add(event_source_manager_t* manager, event_source_t* source) {
return_value_if_fail(manager != NULL && source != NULL, RET_BAD_PARAMS);
object_ref(OBJECT(source));
source->manager = manager;
return darray_push(&(manager->sources), source);
}
@ -82,6 +83,7 @@ bool_t event_source_manager_exist(event_source_manager_t* manager, event_source_
ret_t event_source_manager_remove(event_source_manager_t* manager, event_source_t* source) {
return_value_if_fail(manager != NULL && source != NULL, RET_BAD_PARAMS);
source->manager = NULL;
return darray_remove(&(manager->sources), source);
}

View File

@ -27,8 +27,6 @@
BEGIN_C_DECLS
struct _event_source_manager_t;
typedef struct _event_source_manager_t event_source_manager_t;
typedef ret_t (*event_source_manager_dispatch_t)(event_source_manager_t* manager);
typedef ret_t (*event_source_manager_destroy_t)(event_source_manager_t* manager);

View File

@ -19,6 +19,7 @@
*
*/
#include "tkc/platform.h"
#include "tkc/time_now.h"
#include "tkc/istream.h"
@ -67,10 +68,16 @@ int32_t tk_istream_read_len(tk_istream_t* stream, uint8_t* buff, uint32_t max_si
end = now + timeout_ms;
do {
errno = 0;
read_bytes = tk_istream_read(stream, buff + offset, remain_bytes);
if (read_bytes <= 0) {
break;
if(errno == EAGAIN) {
sleep_ms(10);
continue;
} else {
break;
}
}
offset += read_bytes;

View File

@ -25,6 +25,7 @@
#include <math.h>
#include <ctype.h>
#include <wchar.h>
#include <errno.h>
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
@ -354,4 +355,11 @@ typedef void (*tk_sleep_ms_t)(uint32_t ms);
#define WITH_SOCKET 1
#endif
struct _event_source_t;
typedef struct _event_source_t event_source_t;
struct _event_source_manager_t;
typedef struct _event_source_manager_t event_source_manager_t;
#endif /*TYPES_DEF_H*/