diff --git a/src/lib3270++/abstract.cc b/src/lib3270++/abstract.cc index 34018f0..4406e09 100644 --- a/src/lib3270++/abstract.cc +++ b/src/lib3270++/abstract.cc @@ -37,6 +37,7 @@ */ #include "private.h" + #include /*---[ Implement ]----------------------------------------------------------------------------------*/ @@ -68,6 +69,38 @@ } + /// @brief Setup charsets + void Abstract::Session::setCharSet(const char *remote, const char *local) { + +#ifdef HAVE_ICONV + + if(this->iconv.local != (iconv_t) (-1)) + iconv_close(iconv.local); + + if(this->iconv.host != (iconv_t) (-1)) + iconv_close(iconv.host); + + if(strcmp(local,remote)) { + + // Local and remote charsets aren't the same, setup conversion + iconv.local = iconv_open(local, remote); + iconv.host = iconv_open(remote,local); + + } else { + // Same charset, doesn't convert + iconv.local = iconv.host = (iconv_t)(-1); + } + +#else + + #error No ICONV Support + +#endif + + + } + + } diff --git a/src/lib3270++/local.cc b/src/lib3270++/local.cc index 8112143..1e99f9e 100644 --- a/src/lib3270++/local.cc +++ b/src/lib3270++/local.cc @@ -30,13 +30,14 @@ /** * @file src/lib3270++/local.cc * - * @brief + * @brief Implement lib3270 direct access layout (NO IPC). * * @author perry.werneck@gmail.com * */ #include "private.h" + #include /*---[ Implement ]----------------------------------------------------------------------------------*/ @@ -44,26 +45,38 @@ namespace TN3270 { LocalSession::LocalSession() : Abstract::Session() { + + std::lock_guard lock(sync); + this->hSession = lib3270_session_new(""); lib3270_set_user_data(this->hSession,(void *) this); + setCharSet(lib3270_get_display_charset(this->hSession)); + } LocalSession::~LocalSession() { + + std::lock_guard lock(sync); + lib3270_session_free(this->hSession); this->hSession = nullptr; } void LocalSession::connect(const char *url) { + std::lock_guard lock(sync); lib3270_connect_url(hSession,url,0); } void LocalSession::disconnect() { + std::lock_guard lock(sync); lib3270_disconnect(hSession); } // Wait for session state. void LocalSession::waitForReady(time_t timeout) throw() { + std::lock_guard lock(sync); + int rc = lib3270_wait_for_ready(this->hSession, timeout); if(rc) { @@ -74,56 +87,72 @@ // Gets std::string LocalSession::toString() const { + std::lock_guard lock(const_cast(this)->sync); } std::string LocalSession::toString(int baddr, size_t len, bool lf) { + std::lock_guard lock(sync); } std::string LocalSession::toString(int row, int col, size_t sz, bool lf) { + std::lock_guard lock(sync); } ProgramMessage LocalSession::getProgramMessage() const { + std::lock_guard lock(const_cast(this)->sync); return (ProgramMessage) lib3270_get_program_message(this->hSession); } ConnectionState LocalSession::getConnectionState() const { + std::lock_guard lock(const_cast(this)->sync); return (ConnectionState) lib3270_get_connection_state(this->hSession); } /// @brief Set field at current posicion, jumps to next writable field. TN3270::Session & LocalSession::push(const char *text) { + std::lock_guard lock(sync); return *this; } TN3270::Session & LocalSession::push(int baddr, const std::string &text) { + std::lock_guard lock(sync); return *this; } TN3270::Session & LocalSession::push(int row, int col, const std::string &text) { + std::lock_guard lock(sync); return *this; } TN3270::Session & LocalSession::push(const PFKey key) { + std::lock_guard lock(sync); + lib3270_pfkey(hSession,(int) key); return *this; } TN3270::Session & LocalSession::push(const PAKey key) { + std::lock_guard lock(sync); + lib3270_pakey(hSession,(int) key); return *this; } TN3270::Session & LocalSession::push(const Action action) { + std::lock_guard lock(sync); return *this; } TN3270::Session & LocalSession::pop(int baddr, std::string &text) { + std::lock_guard lock(sync); return *this; } TN3270::Session & LocalSession::pop(int row, int col, std::string &text) { + std::lock_guard lock(sync); return *this; } TN3270::Session & LocalSession::pop(std::string &text) { + std::lock_guard lock(sync); return *this; } diff --git a/src/lib3270++/private.h b/src/lib3270++/private.h index c66b587..bd3c1dd 100644 --- a/src/lib3270++/private.h +++ b/src/lib3270++/private.h @@ -41,6 +41,7 @@ #define PRIVATE_H_INCLUDED #include + #include #include #include @@ -49,6 +50,12 @@ #include #endif // HAVE_ICONV +#ifdef WIN32 + #define SYSTEM_CHARSET "CP1252" +#else + #define SYSTEM_CHARSET "UTF-8" +#endif // WIN32 + namespace TN3270 { namespace Abstract { @@ -76,6 +83,9 @@ Session(); virtual ~Session(); + /// @brief Setup charsets + void setCharSet(const char *remote, const char *local = SYSTEM_CHARSET); + }; } @@ -86,6 +96,9 @@ /// @brief Handle of the related instance of lib3270 H3270 * hSession; + /// @brief Mutex to serialize access to lib3270 + std::mutex sync; + public: LocalSession(); virtual ~LocalSession(); -- libgit2 0.21.2