tesseract  3.05.02
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 392 of file svutil.cpp.

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

◆ ~SVNetwork()

SVNetwork::~SVNetwork ( )

Destructor.

Definition at line 452 of file svutil.cpp.

452  {
453  delete[] msg_buffer_in_;
454  delete mutex_send_;
455 }

Member Function Documentation

◆ Close()

void SVNetwork::Close ( )

Close the connection to the server.

Definition at line 279 of file svutil.cpp.

279  {
280 #ifdef _WIN32
281  closesocket(stream_);
282 #else
283  close(stream_);
284 #endif
285 }

◆ Flush()

void SVNetwork::Flush ( )

Flush the buffer.

Definition at line 220 of file svutil.cpp.

220  {
221  mutex_send_->Lock();
222  while (!msg_buffer_out_.empty()) {
223  int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
224  msg_buffer_out_.erase(0, i);
225  }
226  mutex_send_->Unlock();
227 }
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:78

◆ Receive()

char * SVNetwork::Receive ( )

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

Definition at line 231 of file svutil.cpp.

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

◆ Send()

void SVNetwork::Send ( const char *  msg)

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

Definition at line 213 of file svutil.cpp.

213  {
214  mutex_send_->Lock();
215  msg_buffer_out_.append(msg);
216  mutex_send_->Unlock();
217 }
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:78

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