#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 99 of file svutil.h.
◆ SVNetwork()
SVNetwork::SVNetwork |
( |
const char * |
hostname, |
|
|
int |
port |
|
) |
| |
Set up a connection to hostname on port.
Definition at line 320 of file svutil.cpp.
322 msg_buffer_in_[0] =
'\0';
325 buffer_ptr_ =
nullptr;
327 struct addrinfo *addr_info =
nullptr;
329 snprintf(port_str, 40,
"%d", port);
333 int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
335 std::cerr <<
"WSAStartup failed: " << iResult << std::endl;
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;
347 stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
348 addr_info->ai_protocol);
351 std::cerr <<
"Failed to open socket" << std::endl;
352 }
else if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) < 0) {
354 const char* scrollview_path = getenv(
"SCROLLVIEW_PATH");
355 if (scrollview_path ==
nullptr) {
356 #ifdef SCROLLVIEW_PATH
358 #define _XSTR(a) _STR(a)
359 scrollview_path = _XSTR(SCROLLVIEW_PATH);
363 scrollview_path =
".";
366 const char *prog = ScrollViewProg();
367 std::string command = ScrollViewCommand(scrollview_path);
375 stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
376 addr_info->ai_protocol);
378 if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) == 0) {
384 std::cout <<
"ScrollView: Waiting for server...\n";
385 std::this_thread::sleep_for(std::chrono::seconds(1));
392 freeaddrinfo(addr_info);
◆ ~SVNetwork()
SVNetwork::~SVNetwork |
( |
| ) |
|
Destructor.
Definition at line 395 of file svutil.cpp.
397 delete[] msg_buffer_in_;
◆ Close()
void SVNetwork::Close |
( |
| ) |
|
Close the connection to the server.
Definition at line 269 of file svutil.cpp.
271 closesocket(stream_);
◆ Flush()
void SVNetwork::Flush |
( |
| ) |
|
Flush the buffer.
Definition at line 210 of file svutil.cpp.
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);
◆ 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.
222 char* result =
nullptr;
223 #if defined(_WIN32) || defined(__CYGWIN__)
224 if (has_content) { result = strtok (
nullptr,
"\n"); }
226 if (buffer_ptr_ !=
nullptr) { result = strtok_r(
nullptr,
"\n", &buffer_ptr_); }
230 if (result !=
nullptr) {
return result;
233 buffer_ptr_ =
nullptr;
245 FD_SET(stream_, &readfds);
247 int i = select(stream_+1, &readfds,
nullptr,
nullptr, &tv);
250 if (i == 0) {
return nullptr; }
256 if (i <= 0) {
return nullptr; }
257 msg_buffer_in_[i] =
'\0';
260 return strtok(msg_buffer_in_,
"\n");
263 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 203 of file svutil.cpp.
205 msg_buffer_out_.append(msg);
The documentation for this class was generated from the following files: