27 struct sockaddr* ai_addr;
34 #include <arpa/inet.h> 36 #include <netinet/in.h> 38 #include <semaphore.h> 42 #include <sys/select.h> 43 #include <sys/socket.h> 45 #include <sys/prctl.h> 64 mutex_ = CreateMutex(0,
FALSE, 0);
66 pthread_mutex_init(&mutex_, NULL);
72 WaitForSingleObject(mutex_, INFINITE);
74 pthread_mutex_lock(&mutex_);
82 pthread_mutex_unlock(&mutex_);
86 #ifndef GRAPHICS_DISABLED 102 proc.append(executable);
105 std::cout <<
"Starting " << proc << std::endl;
107 STARTUPINFO start_info;
108 PROCESS_INFORMATION proc_info;
109 GetStartupInfo(&start_info);
110 if (!CreateProcess(NULL, const_cast<char*>(proc.c_str()), NULL, NULL,
FALSE,
111 CREATE_NO_WINDOW | DETACHED_PROCESS, NULL, NULL,
112 &start_info, &proc_info))
121 prctl(PR_SET_PDEATHSIG, 2, 0, 0, 0);
123 char* mutable_args = strdup(args);
125 for (
int i = 0; mutable_args[i]; ++i) {
126 if (mutable_args[i] ==
' ') {
130 char** argv =
new char*[argc + 2];
131 argv[0] = strdup(executable);
132 argv[1] = mutable_args;
134 bool inquote =
false;
135 for (
int i = 0; mutable_args[i]; ++i) {
136 if (!inquote && mutable_args[i] ==
' ') {
137 mutable_args[i] =
'\0';
138 argv[argc++] = mutable_args + i + 1;
139 }
else if (mutable_args[i] ==
'"') {
141 mutable_args[i] =
' ';
145 execvp(executable, argv);
155 semaphore_ = CreateSemaphore(0, 0, 10, 0);
156 #elif defined(__APPLE__) 158 snprintf(name,
sizeof(name),
"%ld", random());
160 semaphore_ = sem_open(name, O_CREAT , S_IWUSR, 0);
161 if (semaphore_ == SEM_FAILED) {
165 sem_init(&semaphore_, 0, 0);
171 ReleaseSemaphore(semaphore_, 1, NULL);
172 #elif defined(__APPLE__) 173 sem_post(semaphore_);
175 sem_post(&semaphore_);
181 WaitForSingleObject(semaphore_, INFINITE);
182 #elif defined(__APPLE__) 183 sem_wait(semaphore_);
185 sem_wait(&semaphore_);
194 LPTHREAD_START_ROUTINE f = (LPTHREAD_START_ROUTINE) func;
196 HANDLE newthread = CreateThread(
206 pthread_attr_init(&attr);
207 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
208 pthread_create(&helper, &attr, func, arg);
215 msg_buffer_out_.append(msg);
222 while (!msg_buffer_out_.empty()) {
223 int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
224 msg_buffer_out_.erase(0, i);
233 #if defined(_WIN32) || defined(__CYGWIN__) 234 if (has_content) { result = strtok (NULL,
"\n"); }
236 if (buffer_ptr_ != NULL) { result = strtok_r(NULL,
"\n", &buffer_ptr_); }
240 if (result != NULL) {
return result;
255 FD_SET(stream_, &readfds);
257 int i = select(stream_+1, &readfds, NULL, NULL, &tv);
260 if (i == 0) {
return NULL; }
266 if (i <= 0) {
return NULL; }
267 msg_buffer_in_[i] =
'\0';
270 return strtok(msg_buffer_in_,
"\n");
273 return strtok_r(msg_buffer_in_,
"\n", &buffer_ptr_);
281 closesocket(stream_);
289 static const char* ScrollViewProg() {
291 const char* prog =
"java -Xms512m -Xmx1024m";
293 const char* prog =
"sh";
300 static std::string ScrollViewCommand(std::string scrollview_path) {
307 const char* cmd_template =
"-Djava.library.path=%s -jar %s/ScrollView.jar";
310 const char* cmd_template =
311 "-c \"trap 'kill %%1' 0 1 2 ; java " 312 "-Xms1024m -Xmx2048m -jar %s/ScrollView.jar" 315 int cmdlen = strlen(cmd_template) + 4*strlen(scrollview_path.c_str()) + 1;
316 char* cmd =
new char[cmdlen];
317 const char* sv_path = scrollview_path.c_str();
318 snprintf(cmd, cmdlen, cmd_template, sv_path, sv_path, sv_path, sv_path);
319 std::string command(cmd);
326 static void FreeAddrInfo(
struct addrinfo* addr_info) {
327 #if defined(__linux__) 328 freeaddrinfo(addr_info);
330 delete addr_info->ai_addr;
337 #if !defined(__linux__) 338 static int GetAddrInfoNonLinux(
const char* hostname,
int port,
339 struct addrinfo** addr_info) {
341 struct sockaddr_in* address;
342 *addr_info =
new struct addrinfo;
343 memset(*addr_info, 0,
sizeof(
struct addrinfo));
344 address =
new struct sockaddr_in;
345 memset(address, 0,
sizeof(
struct sockaddr_in));
347 (*addr_info)->ai_addr = (
struct sockaddr*) address;
348 (*addr_info)->ai_addrlen =
sizeof(
struct sockaddr);
349 (*addr_info)->ai_family = AF_INET;
350 (*addr_info)->ai_socktype = SOCK_STREAM;
352 struct hostent *name;
355 WSAStartup(MAKEWORD(1, 1), &wsaData);
356 name = gethostbyname(hostname);
358 name = gethostbyname(hostname);
362 FreeAddrInfo(*addr_info);
368 address->sin_family = name->h_addrtype;
369 memcpy((
char *) &address->sin_addr.s_addr,
370 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, NULL, address);
386 return GetAddrInfoNonLinux(hostname, port, address);
395 msg_buffer_in_[0] =
'\0';
400 struct addrinfo *addr_info = NULL;
402 if (GetAddrInfo(hostname, port, &addr_info) != 0) {
403 std::cerr <<
"Error resolving name for ScrollView host " 404 << std::string(hostname) <<
":" << port << std::endl;
407 stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
408 addr_info->ai_protocol);
411 if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) < 0) {
412 const char* scrollview_path = getenv(
"SCROLLVIEW_PATH");
413 if (scrollview_path == NULL) {
414 #ifdef SCROLLVIEW_PATH 416 #define _XSTR(a) _STR(a) 417 scrollview_path = _XSTR(SCROLLVIEW_PATH);
421 scrollview_path =
".";
424 const char *prog = ScrollViewProg();
425 std::string command = ScrollViewCommand(scrollview_path);
432 stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
433 addr_info->ai_protocol);
435 while (connect(stream_, addr_info->ai_addr,
436 addr_info->ai_addrlen) < 0) {
437 std::cout <<
"ScrollView: Waiting for server...\n";
445 stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
446 addr_info->ai_protocol);
449 FreeAddrInfo(addr_info);
453 delete[] msg_buffer_in_;
457 #endif // GRAPHICS_DISABLED
SVSemaphore()
Sets up a semaphore.
static void ExitThread()
Signals a thread to exit.
SVMutex()
Sets up a new mutex.
void Wait()
Wait on a semaphore.
static void StartProcess(const char *executable, const char *args)
Starts a new process.
void Send(const char *msg)
Put a message in the messagebuffer to the server and try to send it.
void Lock()
Locks on a mutex.
SVNetwork(const char *hostname, int port)
Set up a connection to hostname on port.
void Flush()
Flush the buffer.
void Unlock()
Unlocks on a mutex.
void Close()
Close the connection to the server.
void Signal()
Signal a semaphore.
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.