tesseract 4.1.1
Loading...
Searching...
No Matches
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::SVNetwork ( const char *  hostname,
int  port 
)

Set up a connection to hostname on port.

Definition at line 320 of file svutil.cpp.

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

◆ ~SVNetwork()

SVNetwork::~SVNetwork ( )

Destructor.

Definition at line 395 of file svutil.cpp.

395 {
396 Close();
397 delete[] msg_buffer_in_;
398}

Member Function Documentation

◆ Close()

void SVNetwork::Close ( )

Close the connection to the server.

Definition at line 269 of file svutil.cpp.

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

◆ Flush()

void SVNetwork::Flush ( )

Flush the buffer.

Definition at line 210 of file svutil.cpp.

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

◆ Receive()

char * SVNetwork::Receive ( )

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

Definition at line 221 of file svutil.cpp.

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

◆ Send()

void SVNetwork::Send ( const char *  msg)

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

Definition at line 203 of file svutil.cpp.

203 {
204 mutex_send_.Lock();
205 msg_buffer_out_.append(msg);
206 mutex_send_.Unlock();
207}

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