tesseract  4.1.0
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 99 of file svutil.h.

Constructor & Destructor Documentation

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

Set up a connection to hostname on port.

Definition at line 319 of file svutil.cpp.

319  {
320  msg_buffer_in_ = new char[kMaxMsgSize + 1];
321  msg_buffer_in_[0] = '\0';
322 
323  has_content = false;
324  buffer_ptr_ = nullptr;
325 
326  struct addrinfo *addr_info = nullptr;
327  char port_str[40];
328  snprintf(port_str, 40, "%d", port);
329 #ifdef _WIN32
330  // Initialize Winsock
331  WSADATA wsaData;
332  int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
333  if (iResult != 0) {
334  std::cerr << "WSAStartup failed: " << iResult << std::endl;
335  }
336 #endif // _WIN32
337 
338  if (getaddrinfo(hostname, port_str, nullptr, &addr_info) != 0) {
339  std::cerr << "Error resolving name for ScrollView host "
340  << std::string(hostname) << ":" << port << std::endl;
341 #ifdef _WIN32
342  WSACleanup();
343 #endif // _WIN32
344  }
345 
346  stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
347  addr_info->ai_protocol);
348 
349  if (stream_ < 0) {
350  std::cerr << "Failed to open socket" << std::endl;
351  } else if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) < 0) {
352  // If server is not there, we will start a new server as local child process.
353  const char* scrollview_path = getenv("SCROLLVIEW_PATH");
354  if (scrollview_path == nullptr) {
355 #ifdef SCROLLVIEW_PATH
356 #define _STR(a) #a
357 #define _XSTR(a) _STR(a)
358  scrollview_path = _XSTR(SCROLLVIEW_PATH);
359 #undef _XSTR
360 #undef _STR
361 #else
362  scrollview_path = ".";
363 #endif
364  }
365  const char *prog = ScrollViewProg();
366  std::string command = ScrollViewCommand(scrollview_path);
367  SVSync::StartProcess(prog, command.c_str());
368 
369  // Wait for server to show up.
370  // Note: There is no exception handling in case the server never turns up.
371 
372  Close();
373  for (;;) {
374  stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
375  addr_info->ai_protocol);
376  if (stream_ >= 0) {
377  if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) == 0) {
378  break;
379  }
380 
381  Close();
382 
383  std::cout << "ScrollView: Waiting for server...\n";
384 #ifdef _WIN32
385  Sleep(1000);
386 #else
387  sleep(1);
388 #endif
389  }
390  }
391  }
392 #ifdef _WIN32
393  // WSACleanup(); // This cause ScrollView windows is not displayed
394 #endif // _WIN32
395  freeaddrinfo(addr_info);
396 }
void Close()
Close the connection to the server.
Definition: svutil.cpp:268
const int kMaxMsgSize
Definition: svutil.cpp:101
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:113
SVNetwork::~SVNetwork ( )

Destructor.

Definition at line 398 of file svutil.cpp.

398  {
399  Close();
400  delete[] msg_buffer_in_;
401 }
void Close()
Close the connection to the server.
Definition: svutil.cpp:268

Member Function Documentation

void SVNetwork::Close ( )

Close the connection to the server.

Definition at line 268 of file svutil.cpp.

268  {
269 #ifdef _WIN32
270  closesocket(stream_);
271 #else
272  close(stream_);
273 #endif
274  // Mark stream_ as invalid.
275  stream_ = -1;
276 }
void SVNetwork::Flush ( )

Flush the buffer.

Definition at line 209 of file svutil.cpp.

209  {
210  mutex_send_.Lock();
211  while (!msg_buffer_out_.empty()) {
212  int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
213  msg_buffer_out_.erase(0, i);
214  }
215  mutex_send_.Unlock();
216 }
void Lock()
Locks on a mutex.
Definition: svutil.cpp:63
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:71
char * SVNetwork::Receive ( )

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

Definition at line 220 of file svutil.cpp.

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

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

Definition at line 202 of file svutil.cpp.

202  {
203  mutex_send_.Lock();
204  msg_buffer_out_.append(msg);
205  mutex_send_.Unlock();
206 }
void Lock()
Locks on a mutex.
Definition: svutil.cpp:63
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:71

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