Tesseract
3.02
|
#include <svutil.h>
Public Member Functions | |
SVNetwork (const char *hostname, int port) | |
Set up a connection to hostname on port. | |
~SVNetwork () | |
Destructor. | |
void | Send (const char *msg) |
Put a message in the messagebuffer to the server and try to send it. | |
char * | Receive () |
void | Close () |
Close the connection to the server. | |
void | Flush () |
Flush the buffer. |
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.
SVNetwork::SVNetwork | ( | const char * | hostname, |
int | port | ||
) |
Set up a connection to hostname on port.
Definition at line 374 of file svutil.cpp.
{ mutex_send_ = new SVMutex(); msg_buffer_in_ = new char[kMaxMsgSize + 1]; msg_buffer_in_[0] = '\0'; has_content = false; buffer_ptr_ = NULL; struct addrinfo *addr_info = NULL; if (GetAddrInfo(hostname, port, &addr_info) != 0) { std::cerr << "Error resolving name for ScrollView host " << std::string(hostname) << ":" << port << std::endl; } stream_ = socket(addr_info->ai_family, addr_info->ai_socktype, addr_info->ai_protocol); // If server is not there, we will start a new server as local child process. if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) < 0) { const char* scrollview_path = getenv("SCROLLVIEW_PATH"); if (scrollview_path == NULL) { #ifdef SCROLLVIEW_PATH #define _STR(a) #a #define _XSTR(a) _STR(a) scrollview_path = _XSTR(SCROLLVIEW_PATH); #undef _XSTR #undef _STR #else scrollview_path = "."; #endif } const char *prog = ScrollViewProg(); std::string command = ScrollViewCommand(scrollview_path); SVSync::StartProcess(prog, command.c_str()); // Wait for server to show up. // Note: There is no exception handling in case the server never turns up. while (connect(stream_, (struct sockaddr *) addr_info->ai_addr, addr_info->ai_addrlen) < 0) { std::cout << "ScrollView: Waiting for server...\n"; #ifdef _WIN32 Sleep(1000); #else sleep(1); #endif } } FreeAddrInfo(addr_info); }
SVNetwork::~SVNetwork | ( | ) |
Destructor.
Definition at line 425 of file svutil.cpp.
{ delete[] msg_buffer_in_; delete mutex_send_; }
void SVNetwork::Close | ( | ) |
Close the connection to the server.
Definition at line 259 of file svutil.cpp.
{ #ifdef _WIN32 closesocket(stream_); #else close(stream_); #endif }
void SVNetwork::Flush | ( | ) |
Flush the buffer.
Definition at line 200 of file svutil.cpp.
char * SVNetwork::Receive | ( | ) |
Receive a message from the server. This will always return one line of char* (denoted by
).
Definition at line 211 of file svutil.cpp.
{ char* result = NULL; #ifdef _WIN32 if (has_content) { result = strtok (NULL, "\n"); } #else if (buffer_ptr_ != NULL) { result = strtok_r(NULL, "\n", &buffer_ptr_); } #endif // This means there is something left in the buffer and we return it. if (result != NULL) { return result; // Otherwise, we read from the stream_. } else { buffer_ptr_ = NULL; has_content = false; // The timeout length is not really important since we are looping anyway // until a new message is delivered. struct timeval tv; tv.tv_sec = 10; tv.tv_usec = 0; // Set the flags to return when the stream_ is ready to be read. fd_set readfds; FD_ZERO(&readfds); FD_SET(stream_, &readfds); int i = select(stream_+1, &readfds, NULL, NULL, &tv); // The stream_ died. if (i == 0) { return NULL; } // Read the message buffer. i = recv(stream_, msg_buffer_in_, kMaxMsgSize, 0); // Server quit (0) or error (-1). if (i <= 0) { return NULL; } msg_buffer_in_[i] = '\0'; has_content = true; #ifdef _WIN32 return strtok(msg_buffer_in_, "\n"); #else // Setup a new string tokenizer. return strtok_r(msg_buffer_in_, "\n", &buffer_ptr_); #endif } }
void SVNetwork::Send | ( | const char * | msg | ) |
Put a message in the messagebuffer to the server and try to send it.
Definition at line 193 of file svutil.cpp.