tesseract  4.0.0-1-g2a2b
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 109 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 392 of file svutil.cpp.

392  {
393  msg_buffer_in_ = new char[kMaxMsgSize + 1];
394  msg_buffer_in_[0] = '\0';
395 
396  has_content = false;
397  buffer_ptr_ = nullptr;
398 
399  struct addrinfo *addr_info = nullptr;
400 
401  if (GetAddrInfo(hostname, port, &addr_info) != 0) {
402  std::cerr << "Error resolving name for ScrollView host "
403  << std::string(hostname) << ":" << port << std::endl;
404  }
405 
406  stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
407  addr_info->ai_protocol);
408 
409  if (stream_ < 0) {
410  std::cerr << "Failed to open socket" << std::endl;
411  } else if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) < 0) {
412  // If server is not there, we will start a new server as local child process.
413  const char* scrollview_path = getenv("SCROLLVIEW_PATH");
414  if (scrollview_path == nullptr) {
415 #ifdef SCROLLVIEW_PATH
416 #define _STR(a) #a
417 #define _XSTR(a) _STR(a)
418  scrollview_path = _XSTR(SCROLLVIEW_PATH);
419 #undef _XSTR
420 #undef _STR
421 #else
422  scrollview_path = ".";
423 #endif
424  }
425  const char *prog = ScrollViewProg();
426  std::string command = ScrollViewCommand(scrollview_path);
427  SVSync::StartProcess(prog, command.c_str());
428 
429  // Wait for server to show up.
430  // Note: There is no exception handling in case the server never turns up.
431 
432  Close();
433  for (;;) {
434  stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
435  addr_info->ai_protocol);
436  if (stream_ >= 0) {
437  if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) == 0) {
438  break;
439  }
440 
441  Close();
442 
443  std::cout << "ScrollView: Waiting for server...\n";
444 #ifdef _WIN32
445  Sleep(1000);
446 #else
447  sleep(1);
448 #endif
449  }
450  }
451  }
452  FreeAddrInfo(addr_info);
453 }
void Close()
Close the connection to the server.
Definition: svutil.cpp:275
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:120
const int kMaxMsgSize
Definition: svutil.cpp:108

◆ ~SVNetwork()

SVNetwork::~SVNetwork ( )

Destructor.

Definition at line 455 of file svutil.cpp.

455  {
456  Close();
457  delete[] msg_buffer_in_;
458 }
void Close()
Close the connection to the server.
Definition: svutil.cpp:275

Member Function Documentation

◆ Close()

void SVNetwork::Close ( )

Close the connection to the server.

Definition at line 275 of file svutil.cpp.

275  {
276 #ifdef _WIN32
277  closesocket(stream_);
278 #else
279  close(stream_);
280 #endif
281  // Mark stream_ as invalid.
282  stream_ = -1;
283 }

◆ Flush()

void SVNetwork::Flush ( )

Flush the buffer.

Definition at line 216 of file svutil.cpp.

216  {
217  mutex_send_.Lock();
218  while (!msg_buffer_out_.empty()) {
219  int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
220  msg_buffer_out_.erase(0, i);
221  }
222  mutex_send_.Unlock();
223 }
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:78
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70

◆ Receive()

char * SVNetwork::Receive ( )

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

Definition at line 227 of file svutil.cpp.

227  {
228  char* result = nullptr;
229 #if defined(_WIN32) || defined(__CYGWIN__)
230  if (has_content) { result = strtok (nullptr, "\n"); }
231 #else
232  if (buffer_ptr_ != nullptr) { result = strtok_r(nullptr, "\n", &buffer_ptr_); }
233 #endif
234 
235  // This means there is something left in the buffer and we return it.
236  if (result != nullptr) { return result;
237  // Otherwise, we read from the stream_.
238  } else {
239  buffer_ptr_ = nullptr;
240  has_content = false;
241 
242  // The timeout length is not really important since we are looping anyway
243  // until a new message is delivered.
244  struct timeval tv;
245  tv.tv_sec = 10;
246  tv.tv_usec = 0;
247 
248  // Set the flags to return when the stream_ is ready to be read.
249  fd_set readfds;
250  FD_ZERO(&readfds);
251  FD_SET(stream_, &readfds);
252 
253  int i = select(stream_+1, &readfds, nullptr, nullptr, &tv);
254 
255  // The stream_ died.
256  if (i == 0) { return nullptr; }
257 
258  // Read the message buffer.
259  i = recv(stream_, msg_buffer_in_, kMaxMsgSize, 0);
260 
261  // Server quit (0) or error (-1).
262  if (i <= 0) { return nullptr; }
263  msg_buffer_in_[i] = '\0';
264  has_content = true;
265 #ifdef _WIN32
266  return strtok(msg_buffer_in_, "\n");
267 #else
268  // Setup a new string tokenizer.
269  return strtok_r(msg_buffer_in_, "\n", &buffer_ptr_);
270 #endif
271  }
272 }
const int kMaxMsgSize
Definition: svutil.cpp:108

◆ Send()

void SVNetwork::Send ( const char *  msg)

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

Definition at line 209 of file svutil.cpp.

209  {
210  mutex_send_.Lock();
211  msg_buffer_out_.append(msg);
212  mutex_send_.Unlock();
213 }
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:78
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70

The documentation for this class was generated from the following files: