int LuaIOLib::io_readline (lua_State *lua) { auto file = cast_upvalue_userdata<LuaFile>(lua, 1); int i; int n = (int)lua_tointeger(lua, lua_upvalueindex(2)); /* if (file->fil return luaL_error(lua, "file is already closed"); */ lua_settop(lua , 1); for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ { lua_pushvalue(lua, lua_upvalueindex(3 + i)); } n = g_read(lua, file->file(), 2); /* 'n' is number of results */ lua_assert(n > 0); /* should return at least a nil */ if (!lua_isnil(lua, -n)) /* read at least one value? */ { return n; /* return them */ } else { /* first result is nil: EOF or error */ if (n > 1) { /* is there error information? */ /* 2nd result is error message */ return luaL_error(lua, "%s", lua_tostring(lua, -n + 1)); } if (lua_toboolean(lua, lua_upvalueindex(3))) { /* generator created file? */ lua_settop(lua, 0); lua_pushvalue(lua, lua_upvalueindex(1)); file->file()->close(); } return 0; } }
static int io_readline (lua_State *L) { LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); int i; int n = (int)lua_tointeger(L, lua_upvalueindex(2)); if (isclosed(p)) /* file is already closed? */ return luaL_error(L, "file is already closed"); lua_settop(L , 1); luaL_checkstack(L, n, "too many arguments"); for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ lua_pushvalue(L, lua_upvalueindex(3 + i)); n = g_read(L, p->f, 2); /* 'n' is number of results */ lua_assert(n > 0); /* should return at least a nil */ if (lua_toboolean(L, -n)) /* read at least one value? */ return n; /* return them */ else { /* first result is nil: EOF or error */ if (n > 1) { /* is there error information? */ /* 2nd result is error message */ return luaL_error(L, "%s", lua_tostring(L, -n + 1)); } if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ lua_settop(L, 0); lua_pushvalue(L, lua_upvalueindex(1)); aux_close(L); /* close it */ } return 0; } }
int main (int argc, char **argv) { stream *s = (argc > 1) ? New buf_stream (argv[1], strlen (argv[1])) : New file_stream (stdin); gob *g = g_read (s); if (g) g->fwrite (stderr); else fprintf (stderr, "-- Error reading input"); fprintf (stderr, "\n"); }
bool g_file_utils::read_bytes(g_fd fd, uint8_t* buffer, size_t len) { uint32_t remain = len; while (remain) { size_t read = g_read(fd, &buffer[len - remain], remain); if (read == 0) { return false; } remain -= read; } return true; }
int io_detect(char *filename, file_table ft[], int ntypes){ FILE *fp; int i, status, words; int32type magic_no; int32type revmagic_no; char editfilename[513]; /* Node 0 reads and checks */ if(this_node == 0){ fp = g_open(filename,"rb"); if(fp == NULL){ /* Special provision for partition or multifile format. Try adding the extension to the filename */ strncpy(editfilename,filename,504); editfilename[504] = '\0'; /* Just in case of truncation */ strcat(editfilename,".vol0000"); fp = g_open(editfilename,"rb"); } if(fp == NULL)status = -2; else { words = g_read(&magic_no, sizeof(int32type), 1, fp); g_close(fp); if(words != 1)status = -3; else { revmagic_no = magic_no; byterevn(&revmagic_no, 1); status = -1; for(i = 0; i < ntypes; i++){ if(ft[i].magic_no == magic_no || ft[i].magic_no == revmagic_no) { status = ft[i].type; break; } } } } } /* Node 0 broadcasts the result */ broadcast_bytes((char *)&status, sizeof(int)); /* All nodes return the same value */ return status; }
bool g_file_utils::read_string(g_fd fd, std::string& out) { std::stringstream s; uint8_t c; while (g_read(fd, &c, 1) > 0) { // stop on null-terminator if (c == 0) { out = s.str(); return true; } s << (char) c; } // must have a null-terminator return false; }
static int f_read (lua_State *L) { return g_read(L, tofile(L), 2); }
int g_print_stats (char *file, uint32_t flags, size_t block_sz) { g_setjmp (0, "g_print_stats", NULL, NULL); if (block_sz) { g_act_1.block_sz = block_sz; } if (g_fopen (file, "r", F_DL_FOPEN_BUFFER | flags, &g_act_1)) { return 2; } if (gfl & F_OPT_LOADQ) { goto rc_end; } void *buffer = calloc (1, g_act_1.block_sz); pt_g_bmatch proc_match = g_bmatch; int r = 0; if (gfl & F_OPT_SORT) { if (gfl & F_OPT_NOBUFFER) { print_str ("ERROR: %s: unable to sort with buffering disabled\n", g_act_1.file); goto r_end; } void *s_exec = (void*) g_act_1.exec_args.exc; if (l_sfo == L_STFO_SORT) { if (g_print_do_filter (&g_act_1, s_exec)) { goto r_end; } } if (gfl & F_OPT_KILL_GLOBAL) { goto r_end; } if (do_sort (&g_act_1, g_sort_field, g_sort_flags)) { goto r_end; } if (l_sfo == L_STFO_FILTER) { if (g_print_do_filter (&g_act_1, s_exec)) { goto r_end; } } if (gfl & F_OPT_KILL_GLOBAL) { goto r_end; } g_act_1.max_hits = 0; g_act_1.max_results = 0; if (g_act_1.j_offset == 2) { g_act_1.buffer.r_pos = md_last (&g_act_1.buffer); } else { g_act_1.buffer.r_pos = md_first (&g_act_1.buffer); } //proc_match = g_bmatch_dummy; md_g_free_cb (&g_act_1._match_rr, g_cl_mrr); } __d_is_wb w_d_s = g_act_1.w_d; g_act_1.w_d = g_act_1.w_d_pr; g_do_ppprint (&g_act_1, F_GH_PRE_PRINT, &g_act_1.pre_print_mech, g_act_1.g_proc4_pr); if (gfl0 & F_OPT_LOADQA) { goto r_end; } g_act_1.w_d = w_d_s; void *ptr; size_t c = 0; g_setjmp (F_SIGERR_CONTINUE, "g_print_stats(loop)", NULL, NULL); g_act_1.buffer.offset = 0; if (!sigsetjmp(g_sigjmp.env, 1)) { while ((ptr = g_read (buffer, &g_act_1, g_act_1.block_sz))) { if ((gfl & F_OPT_KILL_GLOBAL)) { break; } if ((r = proc_match (ptr, &g_act_1, &g_act_1.buffer))) { if (r == -1) { print_str ("ERROR: %s: [%d] matching record failed\n", g_act_1.file, r); break; } continue; } c++; g_act_1.g_proc4 ((void*) &g_act_1, ptr, NULL); } } else { print_str ( "ERROR: %s: an exception has occured, terminating enumeration and attempt cleanup..\n", g_act_1.file); EXITVAL = 2; goto r_end; } g_act_1.w_d = g_act_1.w_d_po; g_do_ppprint (&g_act_1, F_GH_POST_PRINT, &g_act_1.post_print_mech, g_act_1.g_proc4_po); if (gfl & F_OPT_MODE_RAWDUMP) { #ifdef HAVE_ZLIB_H if ((g_act_1.flags & F_GH_IO_GZIP) && g_act_1.gz_fh1) { gzflush(g_act_1.gz_fh1, Z_FINISH); } #endif fflush (stdout); } // g_setjmp(0, "dirlog_print_stats(2)", NULL, NULL); if (!(g_act_1.flags & F_GH_ISONLINE) && (gfl0 & F_OPT_STATS)) { fprintf ( stderr, "STATS: %s: processed %llu/%llu records\n", file, (unsigned long long int) c, !g_act_1.buffer.count ? (unsigned long long int) c : g_act_1.buffer.count); } if (0 == c && 0 == EXITVAL) { EXITVAL = 2; } r_end: free (buffer); rc_end: g_close (&g_act_1); return EXITVAL; }
static int io_read (lua_State *L) { return g_read(L, getiofile(L, IO_INPUT), 1); }
int Client::cRead(char **msg) { return g_read(sockfd, msg); }
static void r_source_cmplx_fm(cmplx_source_file *csf, field_offset dest_site, complex *dest_field, int t0) { int rcv_rank, rcv_coords, status; int destnode; int x,y,z,t,i=0, byterevflag,a; int tmin, tmax; struct { fcomplex q; char pad[PAD_SEND_BUF]; /* Introduced because some switches perform better if message lengths are longer */ } cmsg; int buf_length=0, where_in_buf=0; fcomplex *cbuff=NULL; complex *c; fcomplex cfix; int vol3 = nx*ny*nz; int source_type; byterevflag = csf->byterevflag; source_type = csf->type; if(this_node == 0) { if(source_type == COMPLEX_FIELD_FM_FILE) cbuff = (fcomplex *)malloc(MAX_BUF_LENGTH*sizeof(fcomplex)); else { printf("r_source_cmplx_fm: Unknown source type %d\n", source_type); fflush(stdout); terminate(1); } buf_length = 0; where_in_buf = 0; } /* end of if(this_node == 0)*/ g_sync(); /* If requested, we replicate the source function on ALL time slices */ /* Otherwise we read only to time slice t0 */ if(t0 == ALL_T_SLICES){ tmin = 0; tmax = nt-1; } else { tmin = t0; tmax = t0; } /* Node 0 reads and deals out the values */ status = 0; /* Iterate only over timeslice t0 */ for(rcv_rank=0; rcv_rank<vol3; rcv_rank++) { /* We do only natural (lexicographic) order here */ rcv_coords = rcv_rank; x = rcv_coords % nx; rcv_coords /= nx; y = rcv_coords % ny; rcv_coords /= ny; z = rcv_coords % nz; if(this_node==0){ /* Node 0 fills its buffer, if necessary */ if(where_in_buf == buf_length) { /* get new buffer */ /* new buffer length = remaining sites, but never bigger than MAX_BUF_LENGTH */ buf_length = vol3 - rcv_rank; if(buf_length > MAX_BUF_LENGTH) buf_length = MAX_BUF_LENGTH; /* then do read */ a=(int)g_read(cbuff,sizeof(fcomplex),buf_length,csf->fp); if( a != buf_length) { if(status == 0) printf(" node %d source file read error %d file %s\n", this_node, errno, csf->filename); fflush(stdout); status = 1; } where_in_buf = 0; /* reset counter */ } /*** end of the buffer read ****/ /* Save data in msg.q for further processing */ cmsg.q = cbuff[where_in_buf]; } /* Loop either over all time slices or just one time slice */ for(t = tmin; t <= tmax; t++){ destnode=node_number(x,y,z,t); if(this_node == 0){ /* node 0 doesn't send to itself */ if(destnode != 0){ /* send to correct node */ send_field((char *)&cmsg, sizeof(cmsg), destnode); } } /* if(this_node==0) */ else { /* for all nodes other than node 0 */ if(this_node==destnode){ get_field((char *)&cmsg, sizeof(cmsg),0); } } /* The receiving node does the byte reversal. At this point msg contains the input vectors and i points to the destination site structure */ if(this_node==destnode) { /* Byte reverse a copy, since we may need to reuse cmsg.q */ cfix = cmsg.q; if(byterevflag==1){ byterevn((int32type *)&cfix, sizeof(fcomplex)/sizeof(int32type)); } /* Now copy the site data into the destination converting to generic precision if needed */ i = node_index(x,y,z,t); if(dest_site == (field_offset)(-1)) c = dest_field + i; else c = (complex *)F_PT(&lattice[i],dest_site); c->real = cfix.real; c->imag = cfix.imag; //printf("C %d %d %d %g %g\n", // x, y, z, cmsg.q.real, cmsg.q.imag); } /* if this_node == destnode */ } /* t */ where_in_buf++; } /* rcv_rank, irecord */ if(cbuff != NULL)free(cbuff); cbuff = NULL; }
void r_check(gauge_file *gf, float *max_deviation) { /* gf = gauge configuration file structure */ FILE *fp; gauge_header *gh; char *filename; int byterevflag; off_t offset ; /* File stream pointer */ off_t gauge_check_size; /* Size of gauge configuration checksum record */ off_t coord_list_size; /* Size of coordinate list in bytes */ off_t head_size; /* Size of header plus coordinate list */ off_t checksum_offset; /* Where we put the checksum */ int rcv_rank, rcv_coords; int destnode; int i,k; int x,y,z,t; int buf_length,where_in_buf; gauge_check test_gc; u_int32type *val; int rank29,rank31; su3_matrix *lbuf; su3_matrix work[4]; float deviation; char myname[] = "r_check"; fp = gf->fp; gh = gf->header; filename = gf->filename; byterevflag = gf->byterevflag; if(this_node == 0) { /* Compute offset for reading gauge configuration */ /* (1996 gauge configuration files had a 32-bit unused checksum record before the gauge link data) */ if(gh->magic_number == GAUGE_VERSION_NUMBER) gauge_check_size = sizeof(gf->check.sum29) + sizeof(gf->check.sum31); else if(gh->magic_number == GAUGE_VERSION_NUMBER_1996) gauge_check_size = 4; else gauge_check_size = 0; if(gf->header->order == NATURAL_ORDER)coord_list_size = 0; else coord_list_size = sizeof(int32type)*volume; checksum_offset = gf->header->header_bytes + coord_list_size; head_size = checksum_offset + gauge_check_size; /* Allocate space for read buffer */ if(gf->parallel) printf("%s: Attempting serial read from parallel file \n",myname); lbuf = (su3_matrix *)malloc(MAX_BUF_LENGTH*4*sizeof(su3_matrix)); if(lbuf == NULL) { printf("%s: Node %d can't malloc lbuf\n",myname,this_node); fflush(stdout); terminate(1); } /* Position file for reading gauge configuration */ offset = head_size; if( g_seek(fp,offset,SEEK_SET) < 0 ) { printf("%s: Node 0 g_seek %ld failed error %d file %s\n", myname,(long)offset,errno,filename); fflush(stdout);terminate(1); } buf_length = 0; where_in_buf = 0; } /* all nodes initialize checksums */ test_gc.sum29 = 0; test_gc.sum31 = 0; /* counts 32-bit words mod 29 and mod 31 in order of appearance on file */ /* Here all nodes see the same sequence because we read serially */ rank29 = 0; rank31 = 0; *max_deviation = 0; g_sync(); /* Node 0 reads and deals out the values */ for(rcv_rank=0; rcv_rank<volume; rcv_rank++) { /* If file is in coordinate natural order, receiving coordinate is given by rank. Otherwise, it is found in the table */ if(gf->header->order == NATURAL_ORDER) rcv_coords = rcv_rank; else rcv_coords = gf->rank2rcv[rcv_rank]; x = rcv_coords % nx; rcv_coords /= nx; y = rcv_coords % ny; rcv_coords /= ny; z = rcv_coords % nz; rcv_coords /= nz; t = rcv_coords % nt; /* The node that gets the next set of gauge links */ destnode=node_number(x,y,z,t); if(this_node==0){ /* Node 0 fills its buffer, if necessary */ if(where_in_buf == buf_length) { /* get new buffer */ /* new buffer length = remaining sites, but never bigger than MAX_BUF_LENGTH */ buf_length = volume - rcv_rank; if(buf_length > MAX_BUF_LENGTH)buf_length = MAX_BUF_LENGTH; /* then do read */ if( (int)g_read(lbuf,4*sizeof(su3_matrix),buf_length,fp) != buf_length) { printf("%s: node %d gauge configuration read error %d file %s\n", myname,this_node,errno,filename); fflush(stdout); terminate(1); } where_in_buf = 0; /* reset counter */ } /*** end of the buffer read ****/ if(destnode==0){ /* just copy links */ i = node_index(x,y,z,t); memcpy((void *)&work[0], (void *)&lbuf[4*where_in_buf], 4*sizeof(su3_matrix)); } else { /* send to correct node */ send_field((char *)&lbuf[4*where_in_buf], 4*sizeof(su3_matrix),destnode); } where_in_buf++; } /* The node which contains this site reads message */ else { /* for all nodes other than node 0 */ if(this_node==destnode){ i = node_index(x,y,z,t); get_field((char *)&work[0],4*sizeof(su3_matrix),0); } } /* The receiving node does the byte reversal and then checksum, if needed */ if(this_node==destnode) { if(byterevflag==1) byterevn((int32type *)&work[0], 4*sizeof(su3_matrix)/sizeof(int32type)); /* Accumulate checksums */ for(k = 0, val = (u_int32type *)&work[0]; k < 4*(int)sizeof(su3_matrix)/(int)sizeof(int32type); k++, val++) { test_gc.sum29 ^= (*val)<<rank29 | (*val)>>(32-rank29); test_gc.sum31 ^= (*val)<<rank31 | (*val)>>(32-rank31); rank29++; if(rank29 >= 29)rank29 = 0; rank31++; if(rank31 >= 31)rank31 = 0; } deviation = ck_unitarity(work,x,y,z,t); if(deviation > *max_deviation)*max_deviation = deviation; } else { rank29 += 4*sizeof(su3_matrix)/sizeof(int32type); rank31 += 4*sizeof(su3_matrix)/sizeof(int32type); rank29 %= 29; rank31 %= 31; } }
static int ff_read (lua_State *L) { return g_read(L, tointernalfile(L, 1), 2); }
/** * Waits for responses from the window manager and stores them * in the transaction message map. */ void g_ui::asynchronous_receiver_thread() { while (true) { // TODO properly check if each read/write was successful & validate the data length // read the id uint32_t idlen = sizeof(g_ui_transaction_id); uint8_t id[idlen]; g_read(g_ui_channel_in, &id, idlen); g_ui_transaction_id transaction = *((g_ui_transaction_id*) id); // read the length uint32_t lenlen = sizeof(uint32_t); uint8_t len[lenlen]; g_read(g_ui_channel_in, &len, lenlen); uint32_t length = *((uint32_t*) len); // read the data uint8_t* data = new uint8_t[length]; int32_t data_read = 0; while (data_read < length) { data_read += g_read(g_ui_channel_in, &data[data_read], length - data_read); } // no transaction? -> event if (transaction == 0) { // get id from data g_value_placer data_reader(data); uint32_t listener_id = data_reader.get<uint32_t>(); // notify listener if (listeners.count(listener_id) > 0) { g_listener* listener = listeners.at(listener_id); // add event to dispatch queue g_ui_event_dispatch_data ldata; ldata.listener = listener; ldata.data = data; ldata.length = length; event_dispatch_queue_add(ldata); } } else { // does map even exist? if (transaction_map == 0) { g_logger::log("transaction map did not exist when receiving request"); break; } // check if data exists if (transaction_map->count(transaction) < 1) { g_logger::log("transaction map did not contain data for a request that was received"); break; } // update the data g_ui_transaction_data* transaction_data = transaction_map->at(transaction); transaction_data->data = data; transaction_data->length = length; transaction_data->waiting = false; } } }
// ----------------------------------------------------------------- // Only node 0 reads the gauge configuration gf from a binary file static void r_serial(gauge_file *gf) { FILE *fp = gf->fp; gauge_header *gh = gf->header; char *filename = gf->filename; int byterevflag = gf->byterevflag; off_t offset = 0; // File stream pointer off_t gauge_check_size; // Size of gauge configuration checksum record off_t coord_list_size; // Size of coordinate list in bytes off_t head_size; // Size of header plus coordinate list off_t checksum_offset = 0; // Where we put the checksum int rcv_rank, rcv_coords, destnode, stat, idest = 0; int k, x, y, z, t; int buf_length = 0, where_in_buf = 0; gauge_check test_gc; u_int32type *val; int rank29, rank31; fmatrix *lbuf = NULL; // Only allocate on node0 fmatrix tmat[4]; if (this_node == 0) { // Compute offset for reading gauge configuration if (gh->magic_number == GAUGE_VERSION_NUMBER) gauge_check_size = sizeof(gf->check.sum29) + sizeof(gf->check.sum31); else gauge_check_size = 0; if (gf->header->order == NATURAL_ORDER) coord_list_size = 0; else coord_list_size = sizeof(int32type) * volume; checksum_offset = gf->header->header_bytes + coord_list_size; head_size = checksum_offset + gauge_check_size; // Allocate single-precision read buffer if (gf->parallel) printf("r_serial: Attempting serial read from parallel file\n"); lbuf = malloc(MAX_BUF_LENGTH * 4 * sizeof(*lbuf)); if (lbuf == NULL) { printf("r_serial: node%d can't malloc lbuf\n", this_node); fflush(stdout); terminate(1); } /* Position file for reading gauge configuration */ offset = head_size; if (g_seek(fp, offset, SEEK_SET) < 0) { printf("r_serial: node0 g_seek %lld failed error %d file %s\n", (long long)offset, errno, filename); fflush(stdout); terminate(1); } buf_length = 0; where_in_buf = 0; } // All nodes initialize checksums test_gc.sum29 = 0; test_gc.sum31 = 0; // Count 32-bit words mod 29 and mod 31 in order of appearance on file // Here all nodes see the same sequence because we read serially rank29 = 0; rank31 = 0; g_sync(); // node0 reads and deals out the values for (rcv_rank = 0; rcv_rank < volume; rcv_rank++) { /* If file is in coordinate natural order, receiving coordinate is given by rank. Otherwise, it is found in the table */ if (gf->header->order == NATURAL_ORDER) rcv_coords = rcv_rank; else rcv_coords = gf->rank2rcv[rcv_rank]; x = rcv_coords % nx; rcv_coords /= nx; y = rcv_coords % ny; rcv_coords /= ny; z = rcv_coords % nz; rcv_coords /= nz; t = rcv_coords % nt; // The node that gets the next set of gauge links destnode = node_number(x, y, z, t); // node0 fills its buffer, if necessary if (this_node == 0) { if (where_in_buf == buf_length) { /* get new buffer */ /* new buffer length = remaining sites, but never bigger than MAX_BUF_LENGTH */ buf_length = volume - rcv_rank; if (buf_length > MAX_BUF_LENGTH) buf_length = MAX_BUF_LENGTH; // Now do read stat = (int)g_read(lbuf, 4 * sizeof(fmatrix), buf_length, fp); if (stat != buf_length) { printf("r_serial: node%d gauge configuration read error %d file %s\n", this_node, errno, filename); fflush(stdout); terminate(1); } where_in_buf = 0; // Reset counter } // End of the buffer read if (destnode == 0) { // Just copy links idest = node_index(x, y, z, t); // Save 4 matrices in tmat for further processing memcpy(tmat, &lbuf[4 * where_in_buf], 4 * sizeof(fmatrix)); } else { // Send to correct node send_field((char *)&lbuf[4 * where_in_buf], 4 * sizeof(fmatrix), destnode); } where_in_buf++; } // The node that contains this site reads the message else { // All nodes other than node 0 if (this_node == destnode) { idest = node_index(x, y, z, t); // Receive 4 matrices in temporary space for further processing get_field((char *)tmat, 4 * sizeof(fmatrix), 0); } } /* The receiving node does the byte reversal and then checksum, if needed. At this point tmat contains the input matrices and idest points to the destination site structure. */ if (this_node == destnode) { if (byterevflag == 1) byterevn((int32type *)tmat, 4 * sizeof(fmatrix) / sizeof(int32type)); // Accumulate checksums for (k = 0, val = (u_int32type *)tmat; k < 4*(int)sizeof(fmatrix) / (int)sizeof(int32type); k++, val++) { test_gc.sum29 ^= (*val)<<rank29 | (*val)>>(32 - rank29); test_gc.sum31 ^= (*val)<<rank31 | (*val)>>(32 - rank31); rank29++; if (rank29 >= 29) rank29 = 0; rank31++; if (rank31 >= 31) rank31 = 0; } // Copy 4 matrices to generic-precision lattice[idest] f2d_4mat(tmat, &lattice[idest].link[0]); } else { rank29 += 4 * sizeof(fmatrix) / sizeof(int32type); rank31 += 4 * sizeof(fmatrix) / sizeof(int32type); rank29 %= 29; rank31 %= 31; } }
static gboolean gst_disk_cache_read_file(int file, gpointer data, gint64 start, glong size) { g_lseek64(file, start, SEEK_SET); return size == g_read(file, data, size); }
void RequestHandler::handling_thread(g_message* _request) { // wrap in local for auto-delete g_local < g_message > request(_request); // read parameters g_pid requester_pid = g_get_pid_for_tid(request()->sender); g_fd requesters_output = request()->parameterA; g_fd requesters_input = request()->parameterB; g_pid my_pid = g_get_pid(); // register a name std::stringstream namestr; namestr << "windowserver:handler@"; namestr << requester_pid; g_task_register_id(namestr.str().c_str()); // clone pipe ends g_fs_clonefd_status clone_input_status; g_fd requester_out = g_clone_fd_s(requesters_input, requester_pid, my_pid, &clone_input_status); if (clone_input_status != G_FS_CLONEFD_SUCCESSFUL) { g_logger::log("unable to clone input file descriptor (%i in process %i) on open request (status: %i)", requesters_input, requester_pid, clone_input_status); return; } g_fs_clonefd_status clone_output_status; g_fd requester_in = g_clone_fd_s(requesters_output, requester_pid, my_pid, &clone_output_status); if (clone_output_status != G_FS_CLONEFD_SUCCESSFUL) { g_logger::log("unable to clone output file descriptor (%i in process %i) on open request (status: %i)", requesters_input, requester_pid, clone_output_status); return; } // send response g_message_empty (response); response.type = G_UI_COMMAND_OPEN_RESPONSE; response.topic = request()->topic; g_send_msg(request()->sender, &response); // add process add_process(requester_pid, requester_out, requester_in); // start event dispatch thread g_create_thread((void*) &event_dispatch_thread); while (true) { // read transaction id uint32_t idlen = sizeof(g_ui_transaction_id); uint8_t id[idlen]; g_read(requester_in, id, idlen); g_ui_transaction_id transaction = *((g_ui_transaction_id*) id); // read length uint32_t lenlen = sizeof(uint32_t); uint8_t len[lenlen]; g_read(requester_in, len, lenlen); uint32_t length = *((uint32_t*) len); // read data // TODO limit data uint8_t* data = new uint8_t[length]; int32_t rd = 0; while (rd < length) { rd += g_read(requester_in, &data[rd], length - rd); } g_value_placer data_reader(data); // handle command g_ui_protocol_command_id command = data_reader.get<g_ui_protocol_command_id>(); if (command == G_UI_PROTOCOL_CREATE_WINDOW) { uint32_t window_id; g_ui_protocol_status status = createWindow(&window_id); // write response uint32_t response_len = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_CREATE_WINDOW_RESPONSE_LENGTH; g_local < uint8_t > response(new uint8_t[response_len]); g_value_placer response_writer(response()); response_writer.put(G_UI_PROTOCOL_CREATE_WINDOW); response_writer.put(status); response_writer.put(window_id); send(requester_out, transaction, response(), response_len); } else if (command == G_UI_PROTOCOL_SET_VISIBLE) { uint32_t component_id = data_reader.get<uint32_t>(); bool visible = data_reader.get<uint8_t>(); // handle command g_ui_protocol_status status = setVisible(component_id, visible); // write response uint32_t response_len = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_SET_VISIBLE_RESPONSE_LENGTH; g_local < uint8_t > response(new uint8_t[response_len]); g_value_placer response_writer(response()); response_writer.put(G_UI_PROTOCOL_SET_VISIBLE); response_writer.put(status); send(requester_out, transaction, response(), response_len); } else if (command == G_UI_PROTOCOL_CREATE_COMPONENT) { uint32_t component_type = data_reader.get<uint32_t>(); // handle command uint32_t component_id; g_ui_protocol_status status = createComponent(component_type, &component_id); // write response uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_CREATE_COMPONENT_RESPONSE_LENGTH; g_local < uint8_t > response(new uint8_t[response_length]); g_value_placer response_writer(response()); response_writer.put(G_UI_PROTOCOL_CREATE_COMPONENT); response_writer.put(status); response_writer.put(component_id); send(requester_out, transaction, response(), response_length); } else if (command == G_UI_PROTOCOL_ADD_COMPONENT) { uint32_t parent_id = data_reader.get<uint32_t>(); uint32_t child_id = data_reader.get<uint32_t>(); // handle command g_ui_protocol_status status = addComponent(parent_id, child_id); // write response uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_ADD_COMPONENT_RESPONSE_LENGTH; g_local < uint8_t > response(new uint8_t[response_length]); g_value_placer response_writer(response()); response_writer.put(G_UI_PROTOCOL_ADD_COMPONENT); response_writer.put(status); send(requester_out, transaction, response(), response_length); } else if (command == G_UI_PROTOCOL_SET_TITLE) { uint32_t component_id = data_reader.get<uint32_t>(); uint32_t title_length = data_reader.get<uint32_t>(); g_local<char> title(new char[title_length]); data_reader.get((uint8_t*) title(), title_length); // handle command g_ui_protocol_status status = setTitle(component_id, title()); // write response uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_SET_TITLE_RESPONSE_LENGTH; g_local < uint8_t > response(new uint8_t[response_length]); g_value_placer response_writer(response()); response_writer.put(G_UI_PROTOCOL_SET_TITLE); response_writer.put(status); send(requester_out, transaction, response(), response_length); } else if (command == G_UI_PROTOCOL_GET_TITLE) { uint32_t component_id = data_reader.get<uint32_t>(); // handle command std::string title; g_ui_protocol_status status = getTitle(component_id, title); int title_length = title.length() + 1; // write response uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_GET_TITLE_RESPONSE_LENGTH + title_length; g_local < uint8_t > response(new uint8_t[response_length]); g_value_placer response_writer(response()); response_writer.put(G_UI_PROTOCOL_GET_TITLE); response_writer.put(status); response_writer.put(title_length); response_writer.put((uint8_t*) title.c_str(), title_length); send(requester_out, transaction, response(), response_length); } else if (command == G_UI_PROTOCOL_SET_BOUNDS) { uint32_t component_id = data_reader.get<uint32_t>(); int32_t x = data_reader.get<int32_t>(); int32_t y = data_reader.get<int32_t>(); int32_t width = data_reader.get<int32_t>(); int32_t height = data_reader.get<int32_t>(); // handle command g_ui_protocol_status status = setBounds(component_id, x, y, width, height); // write response uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_SET_BOUNDS_RESPONSE_LENGTH; g_local < uint8_t > response(new uint8_t[response_length]); g_value_placer response_writer(response()); response_writer.put(G_UI_PROTOCOL_SET_BOUNDS); response_writer.put(status); send(requester_out, transaction, response(), response_length); } else if (command == G_UI_PROTOCOL_SET_ACTION_LISTENER) { uint32_t component_id = data_reader.get<uint32_t>(); // handle command uint32_t listener_id; g_ui_protocol_status status = setActionListener(requester_pid, component_id, &listener_id); // write response uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_SET_ACTION_LISTENER; g_local < uint8_t > response(new uint8_t[response_length]); g_value_placer response_writer(response()); response_writer.put<g_ui_protocol_command_id>(G_UI_PROTOCOL_SET_ACTION_LISTENER); response_writer.put<g_ui_protocol_status>(status); response_writer.put<uint32_t>(listener_id); send(requester_out, transaction, response(), response_length); } } // TODO close all windows // TODO remove listeners remove_process(requester_pid); }
int LuaIOLib::file_read(lua_State *lua) { auto file = cast_userdata<LuaFile>(lua, 1); return g_read(lua, file->file(), 2); }