27 struct sockaddr* ai_addr;
34 #include <arpa/inet.h> 36 #include <netinet/in.h> 38 #include <semaphore.h> 40 #include <sys/select.h> 41 #include <sys/socket.h> 43 #include <sys/prctl.h> 57 #include "config_auto.h" 64 mutex_ = CreateMutex(0,
FALSE, 0);
66 pthread_mutex_init(&mutex_,
nullptr);
72 WaitForSingleObject(mutex_, INFINITE);
74 pthread_mutex_lock(&mutex_);
82 pthread_mutex_unlock(&mutex_);
89 LPTHREAD_START_ROUTINE f = (LPTHREAD_START_ROUTINE)func;
91 HANDLE newthread = CreateThread(
nullptr,
100 pthread_attr_init(&attr);
101 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
102 pthread_create(&helper, &attr, func, arg);
106 #ifndef GRAPHICS_DISABLED 115 pthread_exit(
nullptr);
122 proc.append(executable);
125 std::cout <<
"Starting " << proc << std::endl;
127 STARTUPINFO start_info;
128 PROCESS_INFORMATION proc_info;
129 GetStartupInfo(&start_info);
130 if (!CreateProcess(
nullptr, const_cast<char*>(proc.c_str()),
nullptr,
nullptr,
FALSE,
131 CREATE_NO_WINDOW | DETACHED_PROCESS,
nullptr,
nullptr,
132 &start_info, &proc_info))
141 prctl(PR_SET_PDEATHSIG, 2, 0, 0, 0);
143 char* mutable_args = strdup(args);
145 for (
int i = 0; mutable_args[i]; ++i) {
146 if (mutable_args[i] ==
' ') {
150 std::unique_ptr<char*[]> argv(
new char*[argc + 2]);
151 argv[0] = strdup(executable);
152 argv[1] = mutable_args;
154 bool inquote =
false;
155 for (
int i = 0; mutable_args[i]; ++i) {
156 if (!inquote && mutable_args[i] ==
' ') {
157 mutable_args[i] =
'\0';
158 argv[argc++] = mutable_args + i + 1;
159 }
else if (mutable_args[i] ==
'"') {
161 mutable_args[i] =
' ';
164 argv[argc] =
nullptr;
165 execvp(executable, argv.get());
174 semaphore_ = CreateSemaphore(0, 0, 10, 0);
175 #elif defined(__APPLE__) 177 snprintf(name,
sizeof(name),
"%ld", random());
179 semaphore_ = sem_open(name, O_CREAT , S_IWUSR, 0);
180 if (semaphore_ == SEM_FAILED) {
184 sem_init(&semaphore_, 0, 0);
190 ReleaseSemaphore(semaphore_, 1,
nullptr);
191 #elif defined(__APPLE__) 192 sem_post(semaphore_);
194 sem_post(&semaphore_);
200 WaitForSingleObject(semaphore_, INFINITE);
201 #elif defined(__APPLE__) 202 sem_wait(semaphore_);
204 sem_wait(&semaphore_);
211 msg_buffer_out_.append(msg);
218 while (!msg_buffer_out_.empty()) {
219 int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
220 msg_buffer_out_.erase(0, i);
228 char* result =
nullptr;
229 #if defined(_WIN32) || defined(__CYGWIN__) 230 if (has_content) { result = strtok (
nullptr,
"\n"); }
232 if (buffer_ptr_ !=
nullptr) { result = strtok_r(
nullptr,
"\n", &buffer_ptr_); }
236 if (result !=
nullptr) {
return result;
239 buffer_ptr_ =
nullptr;
251 FD_SET(stream_, &readfds);
253 int i = select(stream_+1, &readfds,
nullptr,
nullptr, &tv);
256 if (i == 0) {
return nullptr; }
262 if (i <= 0) {
return nullptr; }
263 msg_buffer_in_[i] =
'\0';
266 return strtok(msg_buffer_in_,
"\n");
269 return strtok_r(msg_buffer_in_,
"\n", &buffer_ptr_);
277 closesocket(stream_);
287 static const char* ScrollViewProg() {
289 const char* prog =
"java -Xms512m -Xmx1024m";
291 const char* prog =
"sh";
298 static std::string ScrollViewCommand(std::string scrollview_path) {
305 const char cmd_template[] =
"-Djava.library.path=%s -jar %s/ScrollView.jar";
308 const char cmd_template[] =
309 "-c \"trap 'kill %%1' 0 1 2 ; java " 310 "-Xms1024m -Xmx2048m -jar %s/ScrollView.jar" 313 size_t cmdlen =
sizeof(cmd_template) + 2 * scrollview_path.size() + 1;
314 std::vector<char> cmd(cmdlen);
315 const char* sv_path = scrollview_path.c_str();
317 snprintf(&cmd[0], cmdlen, cmd_template, sv_path, sv_path);
319 snprintf(&cmd[0], cmdlen, cmd_template, sv_path);
321 std::string command(&cmd[0]);
327 static void FreeAddrInfo(
struct addrinfo* addr_info) {
328 #if defined(__linux__) 329 freeaddrinfo(addr_info);
331 delete addr_info->ai_addr;
338 #if !defined(__linux__) 339 static int GetAddrInfoNonLinux(
const char* hostname,
int port,
340 struct addrinfo** addr_info) {
342 struct sockaddr_in* address;
343 *addr_info =
new struct addrinfo;
344 memset(*addr_info, 0,
sizeof(
struct addrinfo));
345 address =
new struct sockaddr_in;
346 memset(address, 0,
sizeof(
struct sockaddr_in));
348 (*addr_info)->ai_addr = (
struct sockaddr*) address;
349 (*addr_info)->ai_addrlen =
sizeof(
struct sockaddr);
350 (*addr_info)->ai_family = AF_INET;
351 (*addr_info)->ai_socktype = SOCK_STREAM;
353 struct hostent *name;
356 WSAStartup(MAKEWORD(1, 1), &wsaData);
357 name = gethostbyname(hostname);
359 name = gethostbyname(hostname);
362 if (name ==
nullptr) {
363 FreeAddrInfo(*addr_info);
364 *addr_info =
nullptr;
369 address->sin_family = name->h_addrtype;
370 memcpy(&address->sin_addr.s_addr, name->h_addr_list[0], name->h_length);
371 address->sin_port = htons(port);
379 static int GetAddrInfo(
const char* hostname,
int port,
380 struct addrinfo** address) {
381 #if defined(__linux__) 383 snprintf(port_str, 40,
"%d", port);
384 return getaddrinfo(hostname, port_str,
nullptr, address);
386 return GetAddrInfoNonLinux(hostname, port, address);
394 msg_buffer_in_[0] =
'\0';
397 buffer_ptr_ =
nullptr;
399 struct addrinfo *addr_info =
nullptr;
401 if (GetAddrInfo(hostname, port, &addr_info) != 0) {
402 std::cerr <<
"Error resolving name for ScrollView host " 403 << std::string(hostname) <<
":" << port << std::endl;
406 stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
407 addr_info->ai_protocol);
410 std::cerr <<
"Failed to open socket" << std::endl;
411 }
else if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) < 0) {
413 const char* scrollview_path = getenv(
"SCROLLVIEW_PATH");
414 if (scrollview_path ==
nullptr) {
415 #ifdef SCROLLVIEW_PATH 417 #define _XSTR(a) _STR(a) 418 scrollview_path = _XSTR(SCROLLVIEW_PATH);
422 scrollview_path =
".";
425 const char *prog = ScrollViewProg();
426 std::string command = ScrollViewCommand(scrollview_path);
434 stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
435 addr_info->ai_protocol);
437 if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) == 0) {
443 std::cout <<
"ScrollView: Waiting for server...\n";
452 FreeAddrInfo(addr_info);
457 delete[] msg_buffer_in_;
460 #endif // GRAPHICS_DISABLED void Close()
Close the connection to the server.
static void StartProcess(const char *executable, const char *args)
Starts a new process.
void Unlock()
Unlocks on a mutex.
void Send(const char *msg)
Put a message in the messagebuffer to the server and try to send it.
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
void Lock()
Locks on a mutex.
void Wait()
Wait on a semaphore.
void Signal()
Signal a semaphore.
SVSemaphore()
Sets up a semaphore.
SVMutex()
Sets up a new mutex.
SVNetwork(const char *hostname, int port)
Set up a connection to hostname on port.
static void ExitThread()
Signals a thread to exit.
void Flush()
Flush the buffer.