#include <svutil.h>
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 66 of file svutil.h.
◆ SVNetwork()
SVNetwork::SVNetwork |
( |
const char * |
hostname, |
|
|
int |
port |
|
) |
| |
Set up a connection to hostname on port.
Definition at line 264 of file svutil.cpp.
266 msg_buffer_in_[0] =
'\0';
269 buffer_ptr_ =
nullptr;
271 struct addrinfo *addr_info =
nullptr;
273 snprintf(port_str, 40,
"%d", port);
277 int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
279 std::cerr <<
"WSAStartup failed: " << iResult << std::endl;
283 if (getaddrinfo(hostname, port_str,
nullptr, &addr_info) != 0) {
284 std::cerr <<
"Error resolving name for ScrollView host "
285 <<
std::string(hostname) <<
":" << port << std::endl;
291 stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
292 addr_info->ai_protocol);
295 std::cerr <<
"Failed to open socket" << std::endl;
296 }
else if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) < 0) {
298 const char* scrollview_path = getenv(
"SCROLLVIEW_PATH");
299 if (scrollview_path ==
nullptr) {
300 #ifdef SCROLLVIEW_PATH
302 #define _XSTR(a) _STR(a)
303 scrollview_path = _XSTR(SCROLLVIEW_PATH);
307 scrollview_path =
".";
310 const char *prog = ScrollViewProg();
311 std::string command = ScrollViewCommand(scrollview_path);
319 stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
320 addr_info->ai_protocol);
322 if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) == 0) {
328 std::cout <<
"ScrollView: Waiting for server...\n";
329 std::this_thread::sleep_for(std::chrono::seconds(1));
336 freeaddrinfo(addr_info);
◆ ~SVNetwork()
SVNetwork::~SVNetwork |
( |
| ) |
|
Destructor.
Definition at line 339 of file svutil.cpp.
341 delete[] msg_buffer_in_;
◆ Close()
void SVNetwork::Close |
( |
| ) |
|
Close the connection to the server.
Definition at line 213 of file svutil.cpp.
215 closesocket(stream_);
◆ Flush()
void SVNetwork::Flush |
( |
| ) |
|
Flush the buffer.
Definition at line 155 of file svutil.cpp.
156 std::lock_guard<std::mutex> guard(mutex_send_);
157 while (!msg_buffer_out_.empty()) {
158 int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
159 msg_buffer_out_.erase(0, i);
◆ Receive()
char * SVNetwork::Receive |
( |
| ) |
|
Receive a message from the server. This will always return one line of char* (denoted by \n).
Definition at line 165 of file svutil.cpp.
166 char* result =
nullptr;
167 #if defined(_WIN32) || defined(__CYGWIN__)
168 if (has_content) { result = strtok (
nullptr,
"\n"); }
170 if (buffer_ptr_ !=
nullptr) { result = strtok_r(
nullptr,
"\n", &buffer_ptr_); }
174 if (result !=
nullptr) {
return result;
177 buffer_ptr_ =
nullptr;
189 FD_SET(stream_, &readfds);
191 int i = select(stream_+1, &readfds,
nullptr,
nullptr, &tv);
194 if (i == 0) {
return nullptr; }
200 if (i <= 0) {
return nullptr; }
201 msg_buffer_in_[i] =
'\0';
204 return strtok(msg_buffer_in_,
"\n");
207 return strtok_r(msg_buffer_in_,
"\n", &buffer_ptr_);
◆ Send()
void SVNetwork::Send |
( |
const char * |
msg | ) |
|
Put a message in the messagebuffer to the server and try to send it.
Definition at line 149 of file svutil.cpp.
150 std::lock_guard<std::mutex> guard(mutex_send_);
151 msg_buffer_out_.append(msg);
The documentation for this class was generated from the following files: