void do_service(int sd) { int byte_ricevuti = 1; int operazione; struct protocol_header header; memset(&header,0,sizeof(struct protocol_header)); do { printf("* Mi metto in attesa di una nuova richiesta\n"); byte_ricevuti = riceviDato(sd,&header,sizeof(struct protocol_header)); if(byte_ricevuti == 0) break; operazione = controllaOperazione(&header); switch(operazione) { case 1: serverMap(&header,sd); break; case 2: serverUnmap(&header,sd); break; case 3: serverUpdate(&header,sd); break; case 4: serverWrite(&header,sd); break; case 5: serverWait(&header,sd); break; } printf("* Richiesta smistata, passiamo alla prossima\n"); } while (byte_ricevuti != 0); }
/* * Wait for replies from id (win) * Return 0 and the malloc'ed string when a reply is available. * Return -1 if the window becomes invalid while waiting. */ int serverReadReply(VimRemotingClient *client, Window w, char **str) { int len; char *s; VimRemotingClient_ServerReply *p; VimRemotingClient_WaitForReplyParams params = { client, w, NULL }; serverWait(client, w, waitForReply, ¶ms, -1); if (params.result && params.result->strings.ga_len > 0) { *str = strdup(params.result->strings.ga_data); len = strlen(*str) + 1; if (len < params.result->strings.ga_len) { s = (char *) params.result->strings.ga_data; memmove(s, s + len, params.result->strings.ga_len - len); params.result->strings.ga_len -= len; } else { /* Last string read. Remove from list */ ga_clear(¶ms.result->strings); findReply(client, w, SROP_Delete); } return 0; } return -1; }
/* * Send to an instance of Vim via the X display. * Returns 0 for OK, negative for an error. */ int serverSendToVim(VimRemotingClient *client, const char *name, const char *cmd, apr_size_t cmd_len, char **result) { Window w; char *property; int length; int res; int n; VimRemotingClient_PendingCommand pending; if (result != NULL) *result = NULL; prologue(client); /* * Bind the server name to a communication window. * * Find any survivor with a serialno attached to the name if the * original registrant of the wanted name is no longer present. * * Delete any lingering names from dead editors. */ do { w = lookupName(client, name); /* Check that the window is hot */ } while (w != None && !isWindowValid(client, w)); if (w == None) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, client->server_rec, "Failed to connect the server %s", name); epilogue(client); return -1; } /* * Send the command to target interpreter by appending it to the * comm window in the communication window. * Length must be computed exactly! */ #ifdef FEAT_MBYTE length = strlen(name) + strlen(p_enc) + cmd_len + 14; #else length = strlen(name) + cmd_len + 10; #endif property = (char *)malloc((unsigned)length + 30); #ifdef FEAT_MBYTE n = sprintf((char *)property, "%c%c%c-n %s%c-E %s%c-s ", 0, result ? 'c' : 'k', 0, name, 0, p_enc, 0); #else n = sprintf((char *)property, "%c%c%c-n %s%c-s ", 0, result ? 'c' : 'k', 0, name, 0); #endif { memcpy(property + n, cmd, cmd_len); property[n + cmd_len] = '\0'; } /* Add a back reference to our comm window */ client->serial++; sprintf((char *)property + length, "%c-r %x %d", 0, (unsigned int)client->window, client->serial); /* Add length of what "-r %x %d" resulted in, skipping the NUL. */ length += strlen(property + length + 1) + 1; res = appendPropCarefully(client, w, client->commProperty, property, length + 1); free(property); if (res < 0) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, client->server_rec, "Failed to send command to the destination program"); epilogue(client); return -1; } if (!result) { /* There is no answer for this - Keys are sent async */ epilogue(client); return client->got_x_error; } /* * Register the fact that we're waiting for a command to * complete (this is needed by SendEventProc and by * AppendErrorProc to pass back the command's results). */ pending.serial = client->serial; pending.code = 0; pending.result = NULL; pending.nextPtr = client->pendingCommands; client->pendingCommands = &pending; serverWait(client, w, waitForPend, &pending, 600); /* * Unregister the information about the pending command * and return the result. */ if (client->pendingCommands == &pending) { client->pendingCommands = pending.nextPtr; } else { VimRemotingClient_PendingCommand *pcPtr; for (pcPtr = client->pendingCommands; pcPtr; pcPtr = pcPtr->nextPtr) { if (pcPtr->nextPtr == &pending) { pcPtr->nextPtr = pending.nextPtr; break; } } } if (result) *result = pending.result; else free(pending.result); epilogue(client); return pending.code == 0 ? 0 : -1; }