tesseract  5.0.0-alpha-619-ge9db
SVNetwork Class Reference

#include <svutil.h>

Public Member Functions

 SVNetwork (const char *hostname, int port)
 Set up a connection to hostname on port. More...
 
 ~SVNetwork ()
 Destructor. More...
 
void Send (const char *msg)
 Put a message in the messagebuffer to the server and try to send it. More...
 
char * Receive ()
 
void Close ()
 Close the connection to the server. More...
 
void Flush ()
 Flush the buffer. More...
 

Detailed Description

The SVNetwork class takes care of the remote connection for ScrollView This means setting up and maintaining a remote connection, sending and receiving messages and closing the connection. It is designed to work on both Linux and Windows.

Definition at line 66 of file svutil.h.

Constructor & Destructor Documentation

◆ SVNetwork()

SVNetwork::SVNetwork ( const char *  hostname,
int  port 
)

Set up a connection to hostname on port.

Definition at line 264 of file svutil.cpp.

264  {
265  msg_buffer_in_ = new char[kMaxMsgSize + 1];
266  msg_buffer_in_[0] = '\0';
267 
268  has_content = false;
269  buffer_ptr_ = nullptr;
270 
271  struct addrinfo *addr_info = nullptr;
272  char port_str[40];
273  snprintf(port_str, 40, "%d", port);
274 #ifdef _WIN32
275  // Initialize Winsock
276  WSADATA wsaData;
277  int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
278  if (iResult != 0) {
279  std::cerr << "WSAStartup failed: " << iResult << std::endl;
280  }
281 #endif // _WIN32
282 
283  if (getaddrinfo(hostname, port_str, nullptr, &addr_info) != 0) {
284  std::cerr << "Error resolving name for ScrollView host "
285  << std::string(hostname) << ":" << port << std::endl;
286 #ifdef _WIN32
287  WSACleanup();
288 #endif // _WIN32
289  }
290 
291  stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
292  addr_info->ai_protocol);
293 
294  if (stream_ < 0) {
295  std::cerr << "Failed to open socket" << std::endl;
296  } else if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) < 0) {
297  // If server is not there, we will start a new server as local child process.
298  const char* scrollview_path = getenv("SCROLLVIEW_PATH");
299  if (scrollview_path == nullptr) {
300 #ifdef SCROLLVIEW_PATH
301 #define _STR(a) #a
302 #define _XSTR(a) _STR(a)
303  scrollview_path = _XSTR(SCROLLVIEW_PATH);
304 #undef _XSTR
305 #undef _STR
306 #else
307  scrollview_path = ".";
308 #endif
309  }
310  const char *prog = ScrollViewProg();
311  std::string command = ScrollViewCommand(scrollview_path);
312  SVSync::StartProcess(prog, command.c_str());
313 
314  // Wait for server to show up.
315  // Note: There is no exception handling in case the server never turns up.
316 
317  Close();
318  for (;;) {
319  stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
320  addr_info->ai_protocol);
321  if (stream_ >= 0) {
322  if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) == 0) {
323  break;
324  }
325 
326  Close();
327 
328  std::cout << "ScrollView: Waiting for server...\n";
329  std::this_thread::sleep_for(std::chrono::seconds(1));
330  }
331  }
332  }
333 #ifdef _WIN32
334  // WSACleanup(); // This cause ScrollView windows is not displayed
335 #endif // _WIN32
336  freeaddrinfo(addr_info);
337 }

◆ ~SVNetwork()

SVNetwork::~SVNetwork ( )

Destructor.

Definition at line 339 of file svutil.cpp.

339  {
340  Close();
341  delete[] msg_buffer_in_;
342 }

Member Function Documentation

◆ Close()

void SVNetwork::Close ( )

Close the connection to the server.

Definition at line 213 of file svutil.cpp.

213  {
214 #ifdef _WIN32
215  closesocket(stream_);
216 #else
217  close(stream_);
218 #endif
219  // Mark stream_ as invalid.
220  stream_ = -1;
221 }

◆ Flush()

void SVNetwork::Flush ( )

Flush the buffer.

Definition at line 155 of file svutil.cpp.

155  {
156  std::lock_guard<std::mutex> guard(mutex_send_);
157  while (!msg_buffer_out_.empty()) {
158  int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
159  msg_buffer_out_.erase(0, i);
160  }
161 }

◆ Receive()

char * SVNetwork::Receive ( )

Receive a message from the server. This will always return one line of char* (denoted by \n).

Definition at line 165 of file svutil.cpp.

165  {
166  char* result = nullptr;
167 #if defined(_WIN32) || defined(__CYGWIN__)
168  if (has_content) { result = strtok (nullptr, "\n"); }
169 #else
170  if (buffer_ptr_ != nullptr) { result = strtok_r(nullptr, "\n", &buffer_ptr_); }
171 #endif
172 
173  // This means there is something left in the buffer and we return it.
174  if (result != nullptr) { return result;
175  // Otherwise, we read from the stream_.
176  } else {
177  buffer_ptr_ = nullptr;
178  has_content = false;
179 
180  // The timeout length is not really important since we are looping anyway
181  // until a new message is delivered.
182  struct timeval tv;
183  tv.tv_sec = 10;
184  tv.tv_usec = 0;
185 
186  // Set the flags to return when the stream_ is ready to be read.
187  fd_set readfds;
188  FD_ZERO(&readfds);
189  FD_SET(stream_, &readfds);
190 
191  int i = select(stream_+1, &readfds, nullptr, nullptr, &tv);
192 
193  // The stream_ died.
194  if (i == 0) { return nullptr; }
195 
196  // Read the message buffer.
197  i = recv(stream_, msg_buffer_in_, kMaxMsgSize, 0);
198 
199  // Server quit (0) or error (-1).
200  if (i <= 0) { return nullptr; }
201  msg_buffer_in_[i] = '\0';
202  has_content = true;
203 #ifdef _WIN32
204  return strtok(msg_buffer_in_, "\n");
205 #else
206  // Setup a new string tokenizer.
207  return strtok_r(msg_buffer_in_, "\n", &buffer_ptr_);
208 #endif
209  }
210 }

◆ Send()

void SVNetwork::Send ( const char *  msg)

Put a message in the messagebuffer to the server and try to send it.

Definition at line 149 of file svutil.cpp.

149  {
150  std::lock_guard<std::mutex> guard(mutex_send_);
151  msg_buffer_out_.append(msg);
152 }

The documentation for this class was generated from the following files:
string
std::string string
Definition: equationdetect_test.cc:21
kMaxMsgSize
const int kMaxMsgSize
Definition: svutil.cpp:57
SVSync::StartProcess
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:60
SVNetwork::Close
void Close()
Close the connection to the server.
Definition: svutil.cpp:213