From 351a0d1482c53fbc84d96b20b898d9ffb092cfff Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Fri, 11 Jan 2019 12:22:44 -0200 Subject: [PATCH] Adding background tasks support. --- src/include/lib3270.h | 27 ++++++++++++++++++++------- src/include/lib3270/session.h | 2 +- src/lib3270/connect.c | 4 ++-- src/lib3270/ft.c | 16 ++++++++-------- src/lib3270/iocalls.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++--------------- src/lib3270/kybd.c | 4 +++- src/lib3270/log.c | 3 ++- src/lib3270/properties.c | 18 ++++++++++++++---- src/lib3270/session.c | 2 +- 9 files changed, 98 insertions(+), 40 deletions(-) diff --git a/src/include/lib3270.h b/src/include/lib3270.h index 4398fd9..3d6bd4e 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -809,12 +809,12 @@ LIB3270_EXPORT void lib3270_update_poll_fd(H3270 *session, int fd, LIB3270_IO_FLAG flag); /** - * @brief Callback table + * @brief I/O Controller. * - * Structure with GUI unblocking I/O calls, used to replace the lib3270´s internal ones. + * GUI unblocking I/O calls, used to replace the lib3270´s internal ones. * */ - struct lib3270_callbacks + typedef struct lib3270_io_controller { unsigned short sz; @@ -827,8 +827,9 @@ int (*Wait)(H3270 *hSession, int seconds); int (*event_dispatcher)(H3270 *session, int wait); void (*ring_bell)(H3270 *session); + int (*run_task)(int(*callback)(H3270 *, void *), H3270 *session, void *parm); - }; + } LIB3270_IO_CONTROLLER; /** * Register application Handlers. @@ -838,7 +839,7 @@ * @return 0 if ok, error code if not. * */ - LIB3270_EXPORT int lib3270_register_handlers(const struct lib3270_callbacks *cbk); + LIB3270_EXPORT int lib3270_register_io_controller(const LIB3270_IO_CONTROLLER *cbk); /** * Register time handlers. @@ -1151,8 +1152,7 @@ * @param Delay in milliseconds. * */ - LIB3270_EXPORT void lib3270_set_unlock_delay(H3270 *session, int delay); - + LIB3270_EXPORT int lib3270_set_unlock_delay(H3270 *session, int delay); LIB3270_EXPORT int lib3270_get_unlock_delay(H3270 *session); /** @@ -1239,6 +1239,19 @@ LIB3270_EXPORT LIB3270_POINTER lib3270_get_pointer(H3270 *hSession, int baddr); /** + * @brief Run background task. + * + * Call task in a separate thread, keep gui main loop running until + * the function returns. + * + * @param hSession TN3270 session. + * @param callback Function to call. + * @param parm Parameter to callback function. + * + */ + LIB3270_EXPORT int lib3270_run_task(H3270 *hSession, int(*callback)(H3270 *h, void *), void *parm); + + /** * The host is TSO? * * @param hSession Session Handle diff --git a/src/include/lib3270/session.h b/src/include/lib3270/session.h index a0a22c5..dd1073c 100644 --- a/src/include/lib3270/session.h +++ b/src/include/lib3270/session.h @@ -97,7 +97,7 @@ * @return 0 if ok, error code if not. * */ - LIB3270_EXPORT int lib3270_set_session_callbacks(const struct lib3270_callbacks *cbk); + LIB3270_EXPORT int lib3270_set_session_io_handler(const LIB3270_IO_CONTROLLER *cbk); LIB3270_EXPORT int lib3270_getpeername(H3270 *hSession, struct sockaddr *addr, socklen_t *addrlen); LIB3270_EXPORT int lib3270_getsockname(H3270 *hSession, struct sockaddr *addr, socklen_t *addrlen); diff --git a/src/lib3270/connect.c b/src/lib3270/connect.c index 15e7470..c8a2fc6 100644 --- a/src/lib3270/connect.c +++ b/src/lib3270/connect.c @@ -79,7 +79,7 @@ //static void net_connected(H3270 *hSession) -static void net_connected(H3270 *hSession, int fd, LIB3270_IO_FLAG flag, void *dunno) +static void net_connected(H3270 *hSession, int fd unused, LIB3270_IO_FLAG flag unused, void *dunno unused) { int err; socklen_t len = sizeof(err); @@ -550,7 +550,7 @@ static void net_connected(H3270 *hSession, int fd, LIB3270_IO_FLAG flag, void *d } lib3270_disconnect(hSession); - lib3270_write_log(hSession,"connect", "%s",__FUNCTION__,strerror(ETIMEDOUT)); + lib3270_write_log(hSession,"connect", "%s: %s",__FUNCTION__,strerror(ETIMEDOUT)); return errno = ETIMEDOUT; } diff --git a/src/lib3270/ft.c b/src/lib3270/ft.c index f4e9896..6e22a03 100644 --- a/src/lib3270/ft.c +++ b/src/lib3270/ft.c @@ -159,7 +159,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); return 0; } - static void def_complete(H3270 *hSession, unsigned long length,double kbytes_sec,const char *msg, void *userdata) + static void def_complete(H3270 *hSession, unsigned long length unused, double kbytes_sec unused, const char *msg, void *userdata unused) { hSession->ft->cbk.message(hSession,msg,hSession->ft->user_data); } @@ -169,27 +169,27 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); hSession->ft->cbk.complete(hSession,length,kbytes_sec,msg,userdata); } - static void def_message(H3270 *hSession, const char *msg, void *userdata) + static void def_message(H3270 *hSession, const char *msg, void *userdata unused) { lib3270_write_log(hSession,"ft","%s",msg); } - static void def_update(H3270 *hSession, unsigned long current, unsigned long length, double kbytes_sec, void *userdata) + static void def_update(H3270 *hSession unused, unsigned long current unused, unsigned long length unused, double kbytes_sec unused, void *userdata unused) { } - static void def_running(H3270 *hSession, int is_cut, void *userdata) + static void def_running(H3270 *hSession unused, int is_cut unused, void *userdata unused) { } - static void def_aborting(H3270 *hSession, void *userdata) + static void def_aborting(H3270 *hSession unused, void *userdata unused) { } - static void def_state_changed(H3270 *hSession, LIB3270_FT_STATE state, const char *text, void *userdata) + static void def_state_changed(H3270 *hSession unused, LIB3270_FT_STATE state unused, const char *text unused, void *userdata unused) { } @@ -662,14 +662,14 @@ void ft_aborting(H3270FT *h) } /* Process a disconnect abort. */ -static void ft_connected(H3270 *hSession, int ignored, void *dunno) +static void ft_connected(H3270 *hSession, int ignored unused, void *dunno unused) { if (!CONNECTED && lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE) ft_failed(get_ft_handle(hSession),_("Host disconnected, transfer cancelled")); } /* Process an abort from no longer being in 3270 mode. */ -static void ft_in3270(H3270 *hSession, int ignored, void *dunno) +static void ft_in3270(H3270 *hSession, int ignored unused, void *dunno unused) { if (!IN_3270 && lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE) ft_failed(get_ft_handle(hSession),_("Not in 3270 mode, transfer cancelled")); diff --git a/src/lib3270/iocalls.c b/src/lib3270/iocalls.c index b48e6ad..40f71e8 100644 --- a/src/lib3270/iocalls.c +++ b/src/lib3270/iocalls.c @@ -45,15 +45,17 @@ /*---[ Standard calls ]-------------------------------------------------------------------------------------*/ // Timeout calls -static void internal_remove_timeout(H3270 *session, void *timer); -static void * internal_add_timeout(H3270 *session, unsigned long interval_ms, void (*proc)(H3270 *session)); + static void internal_remove_timeout(H3270 *session, void *timer); + static void * internal_add_timeout(H3270 *session, unsigned long interval_ms, void (*proc)(H3270 *session)); -static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata ); -static void internal_remove_poll(H3270 *session, void *id); + static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata ); + static void internal_remove_poll(H3270 *session, void *id); -static int internal_wait(H3270 *session, int seconds); + static int internal_wait(H3270 *session, int seconds); -static void internal_ring_bell(H3270 *session); + static void internal_ring_bell(H3270 *session); + + static int internal_run_task(int(*callback)(H3270 *, void *), H3270 *session, void *parm); /*---[ Active callbacks ]-----------------------------------------------------------------------------------*/ @@ -78,6 +80,9 @@ static void internal_ring_bell(H3270 *session); static void (*ring_bell)(H3270 *) = internal_ring_bell; + static int (*run_task)(H3270 *hSession, int(*callback)(H3270 *h, void *), void *parm) + = internal_run_task; + /*---[ Typedefs ]-------------------------------------------------------------------------------------------*/ #define TN (timeout_t *)NULL @@ -205,7 +210,7 @@ static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, vo ip->userdata = userdata; ip->call = call; - ip->next = session->inputs; + ip->next = (input_t *) session->inputs; session->inputs = ip; session->inputs_changed = 1; @@ -218,7 +223,7 @@ static void internal_remove_poll(H3270 *session, void *id) input_t *ip; input_t *prev = (input_t *)NULL; - for (ip = session->inputs; ip != (input_t *)NULL; ip = ip->next) + for (ip = session->inputs; ip != (input_t *) NULL; ip = (input_t *) ip->next) { if (ip == (input_t *)id) break; @@ -235,7 +240,7 @@ static void internal_remove_poll(H3270 *session, void *id) if (prev != (input_t *)NULL) prev->next = ip->next; else - session->inputs = ip->next; + session->inputs = (input_t *) ip->next; lib3270_free(ip); session->inputs_changed = 1; @@ -379,13 +384,10 @@ LIB3270_EXPORT void lib3270_register_fd_handlers(void * (*add)(H3270 *session, i remove_poll = rm; } -LIB3270_EXPORT int lib3270_register_handlers(const struct lib3270_callbacks *cbk) +LIB3270_EXPORT int lib3270_register_io_controller(const LIB3270_IO_CONTROLLER *cbk) { - if(!cbk) - return EINVAL; - - if(cbk->sz != sizeof(struct lib3270_callbacks)) - return EINVAL; + if(!cbk || cbk->sz != sizeof(LIB3270_IO_CONTROLLER)) + return errno = EINVAL; lib3270_register_time_handlers(cbk->AddTimeOut,cbk->RemoveTimeOut); lib3270_register_fd_handlers(cbk->add_poll,cbk->remove_poll); @@ -447,5 +449,35 @@ LIB3270_EXPORT void lib3270_ring_bell(H3270 *session) ring_bell(session); } +int internal_run_task(int(*callback)(H3270 *, void *), H3270 *session, void *parm) { + return callback(session,parm); +} + +/** + * @brief Run background task. + * + * Call task in a separate thread, keep gui main loop running until + * the function returns. + * + * @param hSession TN3270 session. + * @param callback Function to call. + * @param parm Parameter to callback function. + * + */ +LIB3270_EXPORT int lib3270_run_task(H3270 *hSession, int(*callback)(H3270 *h, void *), void *parm) +{ + int rc; + + CHECK_SESSION_HANDLE(hSession); + + hSession->cbk.set_timer(hSession,1); + rc = run_task(callback,hSession,parm); + hSession->cbk.set_timer(hSession,0); + + return rc; + +} + + diff --git a/src/lib3270/kybd.c b/src/lib3270/kybd.c index a7e47ed..9acc575 100644 --- a/src/lib3270/kybd.c +++ b/src/lib3270/kybd.c @@ -2922,13 +2922,15 @@ int kybd_prime(H3270 *hSession) } #endif /*]*/ -LIB3270_EXPORT void lib3270_set_unlock_delay(H3270 *session, int delay) +LIB3270_EXPORT int lib3270_set_unlock_delay(H3270 *session, int delay) { CHECK_SESSION_HANDLE(session); trace("%s(%d)",__FUNCTION__,(int) delay); session->unlock_delay_ms = (unsigned short) delay; + + return 0; } LIB3270_EXPORT int lib3270_get_unlock_delay(H3270 *session) diff --git a/src/lib3270/log.c b/src/lib3270/log.c index ebdd91b..a5ad816 100644 --- a/src/lib3270/log.c +++ b/src/lib3270/log.c @@ -35,6 +35,7 @@ #include #endif // WIN32 +#include "private.h" #include #include #include @@ -80,7 +81,7 @@ loghandler(session,module,0,fmt,arg); } - static void defaultlog(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr) + static void defaultlog(H3270 *session unused, const char *module, int rc unused, const char *fmt, va_list arg_ptr) { fprintf(stderr,"%s:\t",module); vfprintf(stderr,fmt,arg_ptr); diff --git a/src/lib3270/properties.c b/src/lib3270/properties.c index d4a39c1..1b2e6db 100644 --- a/src/lib3270/properties.c +++ b/src/lib3270/properties.c @@ -251,6 +251,16 @@ return properties; } + static const char * get_version(H3270 *hSession unused) + { + return lib3270_get_version(); + } + + static const char * get_revision(H3270 *hSession unused) + { + return lib3270_get_revision(); + } + LIB3270_EXPORT const LIB3270_STRING_PROPERTY * lib3270_get_string_properties_list(void) { static const LIB3270_STRING_PROPERTY properties[] = { @@ -300,14 +310,14 @@ { "version", // Property name. N_( "lib3270 version" ), // Property description. - lib3270_get_version, // Get value. + get_version, // Get value. NULL // Set value. }, { "revision", // Property name. N_( "lib3270 revision" ), // Property description. - lib3270_get_revision, // Get value. + get_revision, // Get value. NULL // Set value. }, @@ -360,7 +370,7 @@ int lib3270_get_int_property(H3270 *hSession, const char *name, int seconds) // Check for boolean properties properties = lib3270_get_boolean_properties_list(); - for(ix = 0; ix < (sizeof(properties)/sizeof(properties[0])); ix++) + for(ix = 0; properties[ix].name; ix++) { if(!strcasecmp(name,properties[ix].name)) { @@ -380,7 +390,7 @@ int lib3270_get_int_property(H3270 *hSession, const char *name, int seconds) // Check for int properties properties = lib3270_get_int_properties_list(); - for(ix = 0; ix < (sizeof(properties)/sizeof(properties[0])); ix++) + for(ix = 0; properties[ix].name; ix++) { if(!strcasecmp(name,properties[ix].name)) { diff --git a/src/lib3270/session.c b/src/lib3270/session.c index 1cfc42f..b126c29 100644 --- a/src/lib3270/session.c +++ b/src/lib3270/session.c @@ -132,7 +132,7 @@ void lib3270_session_free(H3270 *h) while(h->inputs) { input_t *ip = h->inputs; - h->inputs = ip->next; + h->inputs = (input_t *) ip->next; lib3270_free(ip); } -- libgit2 0.21.2