tesseract  4.00.00dev
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 121 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 389 of file svutil.cpp.

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

◆ ~SVNetwork()

SVNetwork::~SVNetwork ( )

Destructor.

Definition at line 448 of file svutil.cpp.

448  {
449  delete[] msg_buffer_in_;
450 }

Member Function Documentation

◆ Close()

void SVNetwork::Close ( )

Close the connection to the server.

Definition at line 276 of file svutil.cpp.

276  {
277 #ifdef _WIN32
278  closesocket(stream_);
279 #else
280  close(stream_);
281 #endif
282 }

◆ Flush()

void SVNetwork::Flush ( )

Flush the buffer.

Definition at line 217 of file svutil.cpp.

217  {
218  mutex_send_.Lock();
219  while (!msg_buffer_out_.empty()) {
220  int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
221  msg_buffer_out_.erase(0, i);
222  }
223  mutex_send_.Unlock();
224 }
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 228 of file svutil.cpp.

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

210  {
211  mutex_send_.Lock();
212  msg_buffer_out_.append(msg);
213  mutex_send_.Unlock();
214 }
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: