diff options
author | ho.namkoong <ho.namkoong@samsung.com> | 2013-10-29 20:54:56 +0900 |
---|---|---|
committer | ho.namkoong <ho.namkoong@samsung.com> | 2013-10-29 20:57:33 +0900 |
commit | 72cc6e0d134b8c40318fd03d1e3d94aadec923f2 (patch) | |
tree | 7214c8388b8e44d5ba88d985cf495ffddc81d04a | |
parent | aa9484d6cc647816b53d263be667dd6425386b41 (diff) | |
download | sdb-72cc6e0d134b8c40318fd03d1e3d94aadec923f2.tar.gz sdb-72cc6e0d134b8c40318fd03d1e3d94aadec923f2.tar.bz2 sdb-72cc6e0d134b8c40318fd03d1e3d94aadec923f2.zip |
make fd in Windows unique. If one fd is closed and other fds are created, same fd is never be used
Change-Id: Iaf7ce7a08117faf3b557acb35f1c4e9230e2f4de
-rwxr-xr-x | src/fdevent.c | 4 | ||||
-rwxr-xr-x | src/fdevent_windows.c | 9 | ||||
-rwxr-xr-x | src/utils_backend.h | 4 | ||||
-rwxr-xr-x | src/utils_windows.c | 71 |
4 files changed, 37 insertions, 51 deletions
diff --git a/src/fdevent.c b/src/fdevent.c index 826cb8f..1b23844 100755 --- a/src/fdevent.c +++ b/src/fdevent.c @@ -44,8 +44,8 @@ MAP event_map; #if defined(OS_WINDOWS) MAP sdb_handle_map; -HANDLE socket_event_handle[WIN32_MAX_FHS]; -int event_location_to_fd[WIN32_MAX_FHS]; +HANDLE socket_event_handle[MAXIMUM_WAIT_OBJECTS]; +int event_location_to_fd[MAXIMUM_WAIT_OBJECTS]; int current_socket_location = 0; #endif diff --git a/src/fdevent_windows.c b/src/fdevent_windows.c index 202a79b..aed1f5c 100755 --- a/src/fdevent_windows.c +++ b/src/fdevent_windows.c @@ -35,6 +35,11 @@ static void alloc_event(SDB_SOCK_HANDLE* h) { LOG_INFO("FD(%d), LOCATION(%d)\n", h->handle.fd, current_socket_location); + + if(current_socket_location >= MAXIMUM_WAIT_OBJECTS) { + LOG_FATAL("event handle %d exceeds MAXIMUM_WAIT_OBJECTS, aborting!\n", current_socket_location); + } + HANDLE event = WSACreateEvent(); socket_event_handle[current_socket_location] = event; event_location_to_fd[current_socket_location] = h->handle.fd; @@ -150,10 +155,6 @@ void _fdevent_loop() continue; } LOG_INFO( "sdb_win32: fdevevnt loop for %d events start\n", current_socket_location ); - if (current_socket_location > MAXIMUM_WAIT_OBJECTS) { - LOG_ERROR("handle count %d exceeds MAXIMUM_WAIT_OBJECTS, aborting!\n", current_socket_location); - abort(); - } int wait_ret = WaitForMultipleObjects( current_socket_location, socket_event_handle, FALSE, INFINITE ); if(wait_ret == (int)WAIT_FAILED) { diff --git a/src/utils_backend.h b/src/utils_backend.h index f50b298..c0f9ef9 100755 --- a/src/utils_backend.h +++ b/src/utils_backend.h @@ -29,6 +29,7 @@ struct sdb_handle { SOCKET socket; } u; int fd; + int is_socket; }; typedef struct sdb_handle SDB_HANDLE; @@ -42,8 +43,7 @@ typedef struct sdb_socket_handle SDB_SOCK_HANDLE; #define BIP_BUFFER_SIZE 4096 #define MAX_LOOPER_HANDLES WIN32_MAX_FHS -#define IS_SOCKET_HANDLE(handle) (handle->fd < WIN32_MAX_FHS) -#define IS_SOCKET_FD(fd) (fd < WIN32_MAX_FHS) +#define IS_SOCKET_HANDLE(handle) (handle->is_socket == 1) SDB_HANDLE* sdb_handle_map_get(int _key); void sdb_handle_map_put(int _key, SDB_HANDLE* value); diff --git a/src/utils_windows.c b/src/utils_windows.c index f19c4b7..9fb3b07 100755 --- a/src/utils_windows.c +++ b/src/utils_windows.c @@ -34,11 +34,8 @@ void setBaseErrno(DWORD dwLastErrorCode); int getBaseErrno(DWORD dwLastErrorCode); static int total_handle_number = 0; -static LIST_NODE* free_socket_handle_list = NULL; -//this increases 1 when one file fd is created. -static int file_handle_count = WIN32_MAX_FHS; -//this indicates total number of socket fds. -static int socket_handle_number = 0; +//this increases 1 when one file or socket is created. +static int windows_handle_fd_count = 0; struct errno_lists { unsigned long wincode; @@ -237,7 +234,6 @@ static void _start_logging(void) static sdb_mutex_t _win32_lock; static sdb_mutex_t sdb_handle_map_lock; -static sdb_mutex_t free_socket_handle_list_lock; static SDB_HANDLE* alloc_handle(int socket) { @@ -246,29 +242,23 @@ static SDB_HANDLE* alloc_handle(int socket) { if(total_handle_number < WIN32_MAX_FHS) { total_handle_number++; + windows_handle_fd_count++; if(socket) { - if(free_socket_handle_list == NULL) { - SDB_SOCK_HANDLE* __h = malloc(sizeof(SDB_SOCK_HANDLE)); - __h->event_location = -1; - _h = (SDB_HANDLE*)__h; - _h->fd = socket_handle_number++; - LOG_INFO("no free socket. assign socket fd FD(%d)\n", _h->fd); - } - else { - sdb_mutex_lock(&free_socket_handle_list_lock, "_fh_alloc free_socket_handle_list_lock"); - _h = free_socket_handle_list->data; - remove_first(&free_socket_handle_list, no_free); - sdb_mutex_unlock(&free_socket_handle_list_lock, "_fh_alloc free_socket_handle_list_lock"); - LOG_INFO("reuse socket fd FD(%d)\n", _h->fd); - } + SDB_SOCK_HANDLE* __h = malloc(sizeof(SDB_SOCK_HANDLE)); + __h->event_location = -1; + _h = (SDB_HANDLE*)__h; + _h->is_socket = 1; _h->u.socket = INVALID_SOCKET; + LOG_INFO("assign socket fd FD(%d)\n", _h->fd); } else { _h = malloc(sizeof(SDB_HANDLE)); - _h->fd = file_handle_count++; _h->u.file_handle = INVALID_HANDLE_VALUE; + _h->is_socket = 0; + LOG_INFO("assign file fd FD(%d)\n", _h->fd); } + _h->fd = windows_handle_fd_count; sdb_handle_map_put(_h->fd, _h); } else { @@ -310,16 +300,13 @@ static int _fh_close(SDB_HANDLE* _h) { shutdown(_h->u.socket, SD_BOTH); closesocket(_h->u.socket); _h->u.socket = INVALID_SOCKET; - sdb_handle_map_remove(_h->fd); - sdb_mutex_lock(&free_socket_handle_list_lock, "_fh_close"); - prepend(&free_socket_handle_list, _h); - sdb_mutex_unlock(&free_socket_handle_list_lock, "_fh_close"); } else { CloseHandle(_h->u.file_handle); _h->u.file_handle = INVALID_HANDLE_VALUE; - free(_h); } + sdb_handle_map_remove(_h->fd); + free(_h); sdb_mutex_lock(&_win32_lock, "_fh_close"); total_handle_number--; sdb_mutex_unlock(&_win32_lock, "_fh_close"); @@ -475,12 +462,6 @@ static int _sdb_write(int fd, const void* buffer, size_t w_length) { } static int _sdb_shutdown(int fd) { - - if(!IS_SOCKET_FD(fd)) { - LOG_ERROR("FD(%d) is file fd\n", fd); - return -1; - } - SDB_HANDLE* _h = sdb_handle_map_get(fd); if (_h == NULL) { @@ -488,6 +469,11 @@ static int _sdb_shutdown(int fd) { return -1; } + if(!IS_SOCKET_HANDLE(_h)) { + LOG_ERROR("FD(%d) is file fd\n", _h->fd); + return -1; + } + D( "sdb_shutdown: FD(%d)\n", fd); shutdown(_h->u.socket, SD_BOTH); return 0; @@ -680,11 +666,6 @@ static int _sdb_host_connect(const char *host, int port, int type) { static int _sdb_socket_accept(int serverfd) { - if(!IS_SOCKET_FD(serverfd)) { - LOG_ERROR("FD(%d) is file fd\n", serverfd); - return -1; - } - SDB_HANDLE* server_h = sdb_handle_map_get(serverfd); struct sockaddr addr; socklen_t alen = sizeof(addr); @@ -694,6 +675,11 @@ static int _sdb_socket_accept(int serverfd) { return -1; } + if(!IS_SOCKET_HANDLE(server_h)) { + LOG_ERROR("FD(%d) is file fd\n", serverfd); + return -1; + } + SDB_HANDLE* _h = alloc_handle(1); if (!_h) { return -1; @@ -712,16 +698,16 @@ static int _sdb_socket_accept(int serverfd) { static void _disable_tcp_nagle(int fd) { - if(!IS_SOCKET_FD(fd)) { - LOG_ERROR("FD(%d) is file fd\n", fd); - return; - } - SDB_HANDLE* _h = sdb_handle_map_get(fd); if (!_h) { return; } + if(!IS_SOCKET_HANDLE(_h)) { + LOG_ERROR("FD(%d) is file fd\n", fd); + return; + } + int on; setsockopt(_h->u.socket, IPPROTO_TCP, TCP_NODELAY, (const char*) &on, sizeof(on)); } @@ -844,7 +830,6 @@ static void _sdb_sysdeps_init(void) { SDB_MUTEX(D_lock) SDB_MUTEX(_win32_lock); SDB_MUTEX(sdb_handle_map_lock); - SDB_MUTEX(free_socket_handle_list_lock); } typedef void (*win_thread_func_t)(void* arg); |