static gpointer server_thread (gpointer user_data) { int listener = GPOINTER_TO_INT (user_data), client; gnutls_certificate_credentials creds; gnutls_session_t session; struct sockaddr_in sin; int len; char buf[BUFSIZE]; int status; gnutls_certificate_allocate_credentials (&creds); if (gnutls_certificate_set_x509_key_file (creds, ssl_cert_file, ssl_key_file, GNUTLS_X509_FMT_PEM) != 0) { g_error ("Failed to set SSL certificate and key files " "(%s, %s).", ssl_cert_file, ssl_key_file); } gnutls_certificate_set_dh_params (creds, dh_params); /* Create a new session */ gnutls_init (&session, GNUTLS_SERVER); gnutls_set_default_priority (session); gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, creds); gnutls_dh_set_prime_bits (session, DH_BITS); /* Wait for client thread to connect */ len = sizeof (sin); client = accept (listener, (struct sockaddr *) &sin, (void *)&len); gnutls_transport_set_ptr (session, GINT_TO_POINTER (client)); /* Initial handshake */ status = gnutls_handshake (session); if (status < 0) g_error ("initial handshake failed: %d", status); /* Synchronous client test. */ server_read (session, buf, BUFSIZE); server_write (session, buf, BUFSIZE); /* Async client test. */ server_read (session, buf, BUFSIZE); server_write (session, buf, BUFSIZE); /* That's all, folks. */ gnutls_bye (session, GNUTLS_SHUT_WR); gnutls_deinit (session); close (client); gnutls_certificate_free_credentials (creds); return NULL; }
static void select_loop(void) { int rv; while (1) { FD_ZERO(&select_fds); if (cstate != STATE_CMD_USER) FD_SET(server_fd, &select_fds); if (cstate != STATE_CMD_SERVER) FD_SET(0, &select_fds); rv = select(server_fd+1, &select_fds, NULL, NULL, NULL); if (rv < 0) { if (errno == EINTR) continue; else die("select: %m"); } if (FD_ISSET(server_fd, &select_fds)) { server_read(); update_state(); } if (FD_ISSET(0, &select_fds)) { rl_callback_read_char(); update_state(); } } }
void GrovepiSensorResource::get() { debugPrint({"in GET"}); debugPrint({"temperature: ", to_string(temperature)}); debugPrint({"humidity: ", to_string(humidity)}); debugPrint({"light: ", to_string(light)}); debugPrint({"sound: ", to_string(sound)}); server_read("sensor_temp", temperature); server_read("sensor_humidity", humidity); server_read("sensor_light", light); server_read("sensor_sound", sound); rep.setValue("temperature", temperature); rep.setValue("humidity", humidity); rep.setValue("light", light); rep.setValue("sound", sound); }
void SFSUnix::checkSocket() { for (int i = 0; i < MAX_FD; i++) { if (FD_ISSET(i, &_fd_read)) { if (_e.fd_type[i] == FD_SERVER) server_read(i); else if (_e.fd_type[i] == FD_CLIENT) client_read(i); } } }
static int server_init(Server *s) { Op_Init msg, *rep; int opcode; int size; msg.pid = getpid(); msg.server_id = 0; msg.handle = NULL; if (!server_send(s, 0, OP_INIT, sizeof(msg), (unsigned char *)(&msg))) return 0; rep = (Op_Init *)server_read(s, 0, &opcode, &size); if ((rep) && (opcode == OP_INIT) && (size == sizeof(Op_Init))) { s->pid = rep->pid; s->server_id = rep->server_id; s->main_handle = rep->handle; connect_num++; msg.pid = getpid(); msg.server_id = 1; msg.handle = rep->handle; free(rep); if (!server_send(s, 1, OP_INIT, sizeof(msg), (unsigned char *)(&msg))) return 0; rep = (Op_Init *)server_read(s, 1, &opcode, &size); if ((rep) && (opcode == OP_INIT) && (size == sizeof(Op_Init))) { free(rep); return 1; } if (rep) free(rep); return 0; } if (rep) free(rep); return 0; }
EAPI Op_Getinfo_Reply * evas_cserve_raw_info_get(void) { Op_Getinfo_Reply *rep; int opcode; int size; if (csrve_init > 0) server_reinit(); else return NULL; if (!cserve) return NULL; if (!server_send(cserve, 0, OP_GETINFO, 0, NULL)) return NULL; rep = (Op_Getinfo_Reply *)server_read(cserve, 0, &opcode, &size); if ((rep) && (opcode == OP_GETINFO) && (size >= (int)sizeof(Op_Getinfo_Reply))) { return rep; } if (rep) free(rep); return NULL; }
static t_res isset_fd_list(void *data_list, void *data) { t_client *client; t_select *select; client = data_list; select = data; if (client->status == OUT) return (R_CONTINUE); if (FD_ISSET(client->socket, &(select->fd_read))) { if (client->type == SERVER) server_read(select, client); else client_read(select, client); } if (FD_ISSET(client->socket, &(select->fd_write))) client_write(select, client); return (R_CONTINUE); }
EAPI Eina_Bool evas_cserve_raw_stats_get(Op_Getstats_Reply *stats) { Op_Getstats_Reply *rep; int opcode; int size; if (csrve_init > 0) server_reinit(); else return 0; if (!cserve) return 0; if (!server_send(cserve, 0, OP_GETSTATS, 0, NULL)) return 0; rep = (Op_Getstats_Reply *)server_read(cserve, 0, &opcode, &size); if ((rep) && (opcode == OP_GETSTATS) && (size == sizeof(Op_Getstats_Reply))) { memcpy(stats, rep, sizeof(Op_Getstats_Reply)); free(rep); return 1; } if (rep) free(rep); return 0; }
EAPI Eina_Bool evas_cserve_raw_config_get(Op_Getconfig_Reply *config) { Op_Getconfig_Reply *rep; int opcode; int size; if (csrve_init > 0) server_reinit(); else return 0; if (!cserve) return 0; if (!server_send(cserve, 0, OP_GETCONFIG, 0, NULL)) return 0; rep = (Op_Getconfig_Reply *)server_read(cserve, 0, &opcode, &size); if ((rep) && (opcode == OP_GETCONFIG) && (size == sizeof(Op_Getconfig_Reply))) { memcpy(config, rep, sizeof(Op_Getconfig_Reply)); free(rep); return 1; } if (rep) free(rep); return 0; }
void server_ready(struct connection *self, uint32_t mask) { struct conn_info *info = self->info; if (mask & E_ERROR) { LOG(ERROR, "server error: %s:%d %d", info->addr.ip, info->addr.port, self->fd); server_eof(self, rep_err); return; } if (mask & E_WRITABLE) { LOG(DEBUG, "server writable"); if (info->status == CONNECTING) info->status = CONNECTED; if (info->status == CONNECTED) { if (server_write(self) == CORVUS_ERR) { server_eof(self, rep_err); return; } } else { LOG(ERROR, "server not connected"); server_eof(self, rep_err); return; } } if (mask & E_READABLE) { LOG(DEBUG, "server readable"); if (!STAILQ_EMPTY(&info->waiting_queue)) { switch (server_read(self)) { case CORVUS_ERR: case CORVUS_EOF: server_eof(self, rep_err); return; } } else { LOG(WARN, "server is readable but waiting_queue is empty"); server_eof(self, rep_err); return; } } }
EAPI Eina_Bool evas_cserve_image_data_load(Image_Entry *ie) { Op_Loaddata msg; Op_Loaddata_Reply *rep; int opcode; int size; if (csrve_init > 0) server_reinit(); else return 0; if (!cserve) return 0; if (!ie->data1) return 0; if (cserve->server_id != ie->server_id) { ie->data1 = NULL; if (!evas_cserve_image_load(ie, ie->file, ie->key, &(ie->load_opts))) return 0; } if (ie->connect_num != connect_num) return 0; memset(&msg, 0, sizeof(msg)); msg.handle = ie->data1; msg.server_id = cserve->server_id; if (!server_send(cserve, ie->channel, OP_LOADDATA, sizeof(msg), (unsigned char *)(&msg))) return 0; if (!cserve) return 0; rep = (Op_Loaddata_Reply *)server_read(cserve, ie->channel, &opcode, &size); if ((rep) && (opcode == OP_LOADDATA) && (size == sizeof(Op_Loaddata_Reply))) { if (rep->mem.size <= 0) { free(rep); return 0; } ie->data2 = evas_cserve_mem_open(cserve->pid, rep->mem.id, NULL, rep->mem.size, 0); free(rep); return 1; } if (rep) free(rep); return 0; }
EAPI Eina_Bool evas_cserve_image_load(Image_Entry *ie, const char *file, const char *key, RGBA_Image_Loadopts *lopt) { Op_Load msg; Op_Load_Reply *rep; unsigned char *buf; char fbuf[PATH_MAX], wdb[PATH_MAX]; int flen, klen; int opcode; int size; if (csrve_init > 0) server_reinit(); else return 0; if (!cserve) return 0; if (!key) key = ""; memset(&msg, 0, sizeof(msg)); msg.lopt.scale_down_by = lopt->scale_down_by; msg.lopt.dpi = lopt->dpi; msg.lopt.w = lopt->w; msg.lopt.h = lopt->h; msg.lopt.region.x = lopt->region.x; msg.lopt.region.y = lopt->region.y; msg.lopt.region.w = lopt->region.w; msg.lopt.region.h = lopt->region.h; if (file[0] != '/') { if (getcwd(wdb, sizeof(wdb))) { snprintf(fbuf, sizeof(buf), "%s/%s", wdb, file); file = fbuf; } } if (!realpath(file, wdb)) file = wdb; flen = strlen(file) + 1; klen = strlen(key) + 1; buf = malloc(sizeof(msg) + flen + klen); memcpy(buf, &msg, sizeof(msg)); memcpy(buf + sizeof(msg), file, flen); memcpy(buf + sizeof(msg) + flen, key, klen); if (!buf) return 0; if (!server_send(cserve, ie->channel, OP_LOAD, sizeof(msg) + flen + klen, buf)) { free(buf); return 0; } free(buf); if (!cserve) return 0; rep = (Op_Load_Reply *)server_read(cserve, ie->channel, &opcode, &size); if ((rep) && (opcode == OP_LOAD) && (size == sizeof(Op_Load_Reply))) { ie->w = rep->image.w; ie->h = rep->image.h; ie->flags.alpha = rep->image.alpha; ie->data1 = rep->handle; } if (rep) free(rep); if (!ie->data1) return 0; ie->connect_num = connect_num; if (cserve) ie->server_id = cserve->server_id; return 1; }