Commit 64bf19bb6e30ce7bffe24e763fac9e0d7c564d73

Authored by Perry Werneck
1 parent 245584be
Exists in master and in 1 other branch develop

Fixing memory leaks, working on C++ API.

@@ -55,8 +55,15 @@ @@ -55,8 +55,15 @@
55 this->session = nullptr; 55 this->session = nullptr;
56 } 56 }
57 57
  58 + void Host::connect(const char *url) {
  59 + this->session->connect(url);
  60 + sync();
  61 + }
  62 +
  63 +
58 /// @brief Writes characters to the associated file from the put area 64 /// @brief Writes characters to the associated file from the put area
59 int Host::sync() { 65 int Host::sync() {
  66 + this->session->waitForReady();
60 return 0; 67 return 0;
61 } 68 }
62 69
@@ -74,5 +81,20 @@ @@ -74,5 +81,20 @@
74 81
75 } 82 }
76 83
  84 + Host & Host::push(const Action action) {
  85 + session->push(action);
  86 + sync();
  87 + return *this;
  88 + }
  89 +
  90 + std::string Host::toString() const {
  91 +
  92 + if(this->session->getConnectionState() == TN3270::DISCONNECTED) {
  93 + throw std::system_error(ENOTCONN, std::system_category());
  94 + }
  95 +
  96 + return this->session->toString();
  97 + }
  98 +
77 } 99 }
78 100
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 <Option compiler="gcc" /> 8 <Option compiler="gcc" />
9 <Build> 9 <Build>
10 <Target title="Debug"> 10 <Target title="Debug">
11 - <Option output=".bin/Debug/C++ Bindings for lib3270" prefix_auto="1" extension_auto="1" /> 11 + <Option output="../../.bin/Debug/lib3270++" prefix_auto="1" extension_auto="1" />
12 <Option object_output=".obj/Debug/" /> 12 <Option object_output=".obj/Debug/" />
13 <Option type="1" /> 13 <Option type="1" />
14 <Option compiler="gcc" /> 14 <Option compiler="gcc" />
@@ -42,6 +42,8 @@ @@ -42,6 +42,8 @@
42 </Linker> 42 </Linker>
43 <Unit filename="../include/lib3270++.h" /> 43 <Unit filename="../include/lib3270++.h" />
44 <Unit filename="../include/lib3270.h" /> 44 <Unit filename="../include/lib3270.h" />
  45 + <Unit filename="../include/lib3270/action_table.h" />
  46 + <Unit filename="../include/lib3270/actions.h" />
45 <Unit filename="../include/lib3270/popup.h" /> 47 <Unit filename="../include/lib3270/popup.h" />
46 <Unit filename="../include/lib3270/session.h" /> 48 <Unit filename="../include/lib3270/session.h" />
47 <Unit filename="abstract.cc" /> 49 <Unit filename="abstract.cc" />
local/session.cc
@@ -37,6 +37,7 @@ @@ -37,6 +37,7 @@
37 */ 37 */
38 38
39 #include "../private.h" 39 #include "../private.h"
  40 + #include <lib3270/actions.h>
40 41
41 extern "C" { 42 extern "C" {
42 #include <lib3270/actions.h> 43 #include <lib3270/actions.h>
@@ -82,6 +83,8 @@ @@ -82,6 +83,8 @@
82 83
83 void Local::Session::wait(time_t timeout) { 84 void Local::Session::wait(time_t timeout) {
84 85
  86 + std::lock_guard<std::mutex> lock(sync);
  87 +
85 int rc = lib3270_wait_for_ready(this->hSession, timeout); 88 int rc = lib3270_wait_for_ready(this->hSession, timeout);
86 89
87 if(rc) { 90 if(rc) {
@@ -98,9 +101,7 @@ @@ -98,9 +101,7 @@
98 throw std::system_error(rc, std::system_category()); 101 throw std::system_error(rc, std::system_category());
99 } 102 }
100 103
101 - wait();  
102 -  
103 - } 104 + }
104 105
105 void Local::Session::disconnect() { 106 void Local::Session::disconnect() {
106 std::lock_guard<std::mutex> lock(sync); 107 std::lock_guard<std::mutex> lock(sync);
@@ -109,23 +110,42 @@ @@ -109,23 +110,42 @@
109 110
110 // Wait for session state. 111 // Wait for session state.
111 void Local::Session::waitForReady(time_t timeout) throw() { 112 void Local::Session::waitForReady(time_t timeout) throw() {
  113 + this->wait(timeout);
  114 + }
112 115
113 - std::lock_guard<std::mutex> lock(sync);  
114 - wait(timeout); 116 + std::string Local::Session::toString(int baddr, size_t len, char lf) const {
  117 +
  118 + std::lock_guard<std::mutex> lock(const_cast<Local::Session *>(this)->sync);
  119 +
  120 + char * text = lib3270_get_text(hSession, baddr, len, lf);
  121 +
  122 + if(!text) {
  123 + throw std::runtime_error("Can't get screen contents");
  124 + }
  125 +
  126 + string rc = convertFromHost(text);
  127 +
  128 + lib3270_free(text);
  129 +
  130 + return rc;
115 131
116 } 132 }
117 133
118 - // Gets  
119 - std::string Local::Session::toString() const { 134 + std::string Local::Session::toString(int row, int col, size_t sz, char lf) const {
  135 +
120 std::lock_guard<std::mutex> lock(const_cast<Local::Session *>(this)->sync); 136 std::lock_guard<std::mutex> lock(const_cast<Local::Session *>(this)->sync);
121 - }  
122 137
123 - std::string Local::Session::toString(int baddr, size_t len, bool lf) {  
124 - std::lock_guard<std::mutex> lock(sync);  
125 - } 138 + char * text = lib3270_get_text_at(hSession, row, col, sz, lf);
126 139
127 - std::string Local::Session::toString(int row, int col, size_t sz, bool lf) {  
128 - std::lock_guard<std::mutex> lock(sync); 140 + if(!text) {
  141 + throw std::runtime_error("Can't get screen contents");
  142 + }
  143 +
  144 + string rc = convertFromHost(text);
  145 +
  146 + lib3270_free(text);
  147 +
  148 + return rc;
129 } 149 }
130 150
131 ProgramMessage Local::Session::getProgramMessage() const { 151 ProgramMessage Local::Session::getProgramMessage() const {
@@ -168,6 +188,30 @@ @@ -168,6 +188,30 @@
168 188
169 TN3270::Session & Local::Session::push(const Action action) { 189 TN3270::Session & Local::Session::push(const Action action) {
170 std::lock_guard<std::mutex> lock(sync); 190 std::lock_guard<std::mutex> lock(sync);
  191 +
  192 + switch(action) {
  193 + case ENTER:
  194 + lib3270_enter(hSession);
  195 + break;
  196 +
  197 + case ERASE:
  198 + lib3270_erase(hSession);
  199 + break;
  200 +
  201 + case ERASE_EOF:
  202 + lib3270_eraseeof(hSession);
  203 + break;
  204 +
  205 + case ERASE_EOL:
  206 + lib3270_eraseeol(hSession);
  207 + break;
  208 +
  209 + case ERASE_INPUT:
  210 + lib3270_eraseinput(hSession);
  211 + break;
  212 +
  213 + }
  214 +
171 return *this; 215 return *this;
172 } 216 }
173 217
@@ -45,6 +45,7 @@ @@ -45,6 +45,7 @@
45 #include <lib3270++.h> 45 #include <lib3270++.h>
46 #include <lib3270/popup.h> 46 #include <lib3270/popup.h>
47 #include <system_error> 47 #include <system_error>
  48 + #include <stdexcept>
48 49
49 50
50 #ifdef HAVE_ICONV 51 #ifdef HAVE_ICONV
@@ -133,9 +134,8 @@ @@ -133,9 +134,8 @@
133 void waitForReady(time_t timeout = 5) throw() override; 134 void waitForReady(time_t timeout = 5) throw() override;
134 135
135 // Gets 136 // Gets
136 - std::string toString() const override;  
137 - std::string toString(int baddr = 0, size_t len = -1, bool lf = false) override;  
138 - std::string toString(int row, int col, size_t sz, bool lf = false) override; 137 + std::string toString(int baddr, size_t len, char lf) const override;
  138 + std::string toString(int row, int col, size_t sz, char lf) const override;
139 139
140 ProgramMessage getProgramMessage() const override; 140 ProgramMessage getProgramMessage() const override;
141 141
testprogram/testprogram.cc
@@ -39,6 +39,8 @@ @@ -39,6 +39,8 @@
39 #include <cstdlib> 39 #include <cstdlib>
40 #include <lib3270++.h> 40 #include <lib3270++.h>
41 41
  42 + using namespace std;
  43 +
42 /*---[ Implement ]----------------------------------------------------------------------------------*/ 44 /*---[ Implement ]----------------------------------------------------------------------------------*/
43 45
44 int main(int argc, const char *argv[]) { 46 int main(int argc, const char *argv[]) {
@@ -46,6 +48,11 @@ @@ -46,6 +48,11 @@
46 TN3270::Host host; 48 TN3270::Host host;
47 49
48 host.connect(getenv("TN3270URL")); 50 host.connect(getenv("TN3270URL"));
  51 + cout << host << endl;
  52 +
  53 + host << TN3270::ENTER;
  54 +
  55 +
49 56
50 return 0; 57 return 0;
51 } 58 }