void msg_event_handler( int fd, int msgid, int event ) { char buf[PACK_DATA_SIZE]; ssize_t n; // printf( "event 0x%03x for msg %d\n", event, msgid ); switch ( event ) { case MSG_EVT_ACCEPT_READY: if ( msg_accept( fd, msgid ) != 0 ) msg_set_read( fd, msgid ); // These are commands break; case MSG_EVT_READ_READY: msg_clr_read( fd, msgid ); n = msg_read( fd, msgid, buf, sizeof( buf ) ); if ( n <= 0 ) { cleanup_connection( msg_get_type( fd, msgid ) ); break; } if ( msg_get_type( fd, msgid ) < 0 ) { /* If the msgid is less than zero, it refers to a command and the message should not be routed to a file descriptor. */ // the entire command must fit inside one transport command_handler( fd, msgid, buf, n ); } else { zs_set_write( msg_get_type( fd, msgid ) ); /* if msgid is zero or greater it refers to a socket file descriptor that the message should be routed to. */ if ( zs_write( msg_get_type( fd, msgid ), buf, n ) == -1 ) cleanup_connection( msg_get_type( fd, msgid ) ); } break; case MSG_EVT_WRITE_READY: if ( msgid < 0 ) break; zs_set_read( msg_get_type( fd, msgid ) ); msg_clr_write( fd, msgid ); break; case MSG_SWITCH_EVT_IO_ERROR: //zclose( msg_switch->sockfd ); if ( website_get_by_sockfd( fd ) ) remove_website( fd ); else { for ( n = 0; n < FD_SETSIZE; n++ ) { if ( msg_exists( fd, n ) ) { if ( msg_get_type( fd, n ) > 0 ) cleanup_connection( msg_get_type( fd, n ) ); // else // msg_close( fd, n ); } } msg_switch_destroy( fd ); zs_close( fd ); } break; } }
static int msg_read_buf32(msg_t *m, void *buf, uint32_t *len) { int r; r = msg_read_u32(m, len); if (r) return r; r = msg_read(m, buf, *len); if (r) return r; return 0; }
int main(int argc, char **argv) { t_env e; if ((e.key = ftok(argv[0], 'a')) < 0) { perror("ftok"); exit(1); } // printf("e.key : %d\n", e.key); if (argc == 2) { init_env(&e, argv[1]); while (1) { printf("ICI\n"); op_sem_proberen(&e); printf("LA\n"); // printf("heya\n"); if ((*((int*)(e.addr + sizeof(int)))) == 0) { check_start(&e); op_sem_verhogen(&e); continue ; } printf("hey0\n"); test_print(&e); if (check_elim(&e)) player_lost(&e); msg_read(&e); printf("hey1\n"); begin_turn(&e); op_sem_verhogen(&e); if ((*((int*)(e.addr + sizeof(int)))) == 1 && check_victory(&e)) { printf("VICTOIRRRRRRRRE team : %d\n", e.team); kill_player(&e); } printf("leader : %d\n", e.leader); usleep(500000); } } else printf("Usage : %s [team]\n", argv[0]); return (0); }
static int msg_read_str32(msg_t *m, char **str) { int r; uint32_t len; r = msg_read_u32(m, &len); if (r) return r; char *s = (char *)zalloc(len+1); r = msg_read(m, s, len); if (r) { free(s); return r; } *str = s; return 0; }
static int msg_read_allocbuf32(msg_t *m, void **_buf, uint32_t *_len) { int r; uint32_t len; r = msg_read_u32(m, &len); if (r) return r; void *buf = zalloc(len); r = msg_read(m, buf, len); if (r) { free(buf); return r; } *_buf = buf; *_len = len; return 0; }
/* * Commits messages to the FIFO. If the FIFO is full, then enough * messages are dropped to create space for the new message. */ void ipc_log_write(void *ctxt, struct encode_context *ectxt) { struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt; int bytes_to_write; unsigned long flags; if (!ilctxt || !ectxt) { pr_err("%s: Invalid ipc_log or encode context\n", __func__); return; } spin_lock_irqsave(&ipc_log_context_list_lock, flags); spin_lock(&ilctxt->ipc_log_context_lock); while (ilctxt->write_avail < ectxt->offset) msg_read(ilctxt, NULL); bytes_to_write = MIN(((PAGE_SIZE - sizeof(struct ipc_log_page_header)) - ilctxt->write_page->hdr.write_offset), ectxt->offset); memcpy((ilctxt->write_page->data + ilctxt->write_page->hdr.write_offset), ectxt->buff, bytes_to_write); if (bytes_to_write != ectxt->offset) { ilctxt->write_page->hdr.write_offset = 0xFFFF; ilctxt->write_page = get_next_page(ilctxt, ilctxt->write_page); ilctxt->write_page->hdr.write_offset = 0; memcpy((ilctxt->write_page->data + ilctxt->write_page->hdr.write_offset), (ectxt->buff + bytes_to_write), (ectxt->offset - bytes_to_write)); bytes_to_write = (ectxt->offset - bytes_to_write); } ilctxt->write_page->hdr.write_offset += bytes_to_write; ilctxt->write_avail -= ectxt->offset; complete(&ilctxt->read_avail); spin_unlock(&ilctxt->ipc_log_context_lock); spin_unlock_irqrestore(&ipc_log_context_list_lock, flags); }
static int run_worker(struct worker_data *data) { char buf[PIPE_BUF+1] = {}; char outbuf[PIPE_BUF+1] = {}; char *urls[MAX_URLS]; size_t urls_c; char *p; int res, i; ssize_t len; double bytes; long header_bytes; if(init_worker(data)) return -1; while(1) { if((len = msg_read(data->pipe_r, buf, sizeof(buf))) == -1) { return EXIT_FAILURE; } buf[len] = '\0'; if(strncmp(buf, "STOP", 4) == 0 || len == 0) return destroy_worker(data); if(strncmp(buf, "RESET", 5) == 0) { if((res = reset_worker(data)) != 0) { len = sprintf(outbuf, "ERR %d", res); msg_write(data->pipe_w, outbuf, len); } else { msg_write(data->pipe_w, "OK", sizeof("OK")); } continue; } if(strncmp(buf, "URLLIST ", 8) == 0) { p = buf + 8; data->chunk.enabled = 1; } else if(strncmp(buf, "URL ", 4) == 0) { p = buf + 4; } else { fprintf(stderr, "Unrecognised command '%s'!\n", buf); break; } if(data->debug) { fprintf(stderr, "Getting URL '%s'.\n", p); } curl_easy_setopt(data->curl, CURLOPT_URL, p); data->chunk.size = 0; if((res = curl_easy_perform(data->curl)) != CURLE_OK) { len = sprintf(outbuf, "ERR %d", res); msg_write(data->pipe_w, outbuf, len); } else { if((res = curl_easy_getinfo(data->curl, CURLINFO_SIZE_DOWNLOAD, &bytes)) != CURLE_OK || (res = curl_easy_getinfo(data->curl, CURLINFO_HEADER_SIZE, &header_bytes)) != CURLE_OK ) { fprintf(stderr, "cURL error: %s\n", curl_easy_strerror(res)); } if(data->chunk.enabled == 0) { len = sprintf(outbuf, "OK %lu bytes", (long)bytes + header_bytes); msg_write(data->pipe_w, outbuf, len); } else { urls_c = parse_urls(data->chunk.memory, data->chunk.size, urls, MAX_URLS); len = sprintf(outbuf, "OK %lu bytes %lu urls", (long)bytes + header_bytes, (long)urls_c); msg_write(data->pipe_w, outbuf, len); for(i = 0; i < urls_c; i++) { len = sprintf(outbuf, "%s", urls[i]); msg_write(data->pipe_w, outbuf, len); free(urls[i]); } data->chunk.enabled = 0; } } } return 0; }
void * lisen() { char c; char respond_code[20] = {0}; char respond_code1[20] = {0}; char temp[20] = {0}; Message inbox; CL r; char *Num; int i; int tel_count; int msg_count; FILE *fp1; tty_init(); gprs_init(); // 初識化GPRS模塊 while (Mon_flag) { // memset(respond_code,0,20); tty_read(&c, 1); //printf("dds\n"); if (c == 'R') { tty_read(respond_code, 3); respond_code[3] = '\0'; if (strcmp(respond_code, "ING") == 0) { //printf("%s",respond_code); calltype = 2; SendNotifyMessage(hcwd, MSG_RING, 0, 0L); printf("call call ...."); } } if (c == 'N') { //收到掛電話 tty_read(respond_code1, 9); //respond_code1[9]='\0'; if (strcmp(respond_code1, "O CARRIER") == 0) { //發送消息到通話界面,結束正在通話界面,返回主界面。 if (calltype == 1) { SendNotifyMessage(h_calling, MSG_USRHOLD, 0, 0L); } else if (calltype == 2) { strcpy(r.Phone_num, PhoneNum); //records_write(FILE_MISSED_CALL_DAT,&r); SendMessage(c_holding, MSG_MISSED, 0, 0L); } } } if (c == '+') { tty_read(respond_code, 4); respond_code[4] = '\0'; if (strcmp(respond_code, "CLIP") == 0) { printf("CLIP"); for (i = 0; i < 20; i++) { tty_read(&c, 1); PhoneNum[i] = c; } PhoneNum[i] = '\0'; Num = (char*)strtok(PhoneNum, "\""); Num = (char*)strtok(NULL, "\""); strcpy(temp, Num); strcpy(PhoneNum, temp); //printf("%s",Num); } else if (strcmp(respond_code, "CMTI") == 0) { //msg_read(Mes_index); tty_read(temp, 7); tty_read(temp, 2); temp[3] = '\0'; printf(" msg index= %s ", temp); Mes_index = atoi(temp); printf(" Mes_index= %d", Mes_index); msg_read(Mes_index); SendNotifyMessage(hMainWnd, MSG_NEWMSG, 0, 0L); } else if (strcmp(respond_code, "CMGR") == 0) { tty_read(&c, 1); while (c != ',') { tty_read(&c, 1); } tty_read(&c, 1); tty_read(&c, 1); while (c != '\"') { Tel_Num[tel_count++] = c; tty_read(&c, 1); } Tel_Num[tel_count] = '\0'; printf("\n- %s \n", Tel_Num); tty_read(temp, 3); tty_read(Data, 17); Data[17] = '\0'; printf("\n %s\n ", Data); tty_read(&c, 1); while (c != '\n') { tty_read(&c, 1); } tty_read(&c, 1); tty_read(&c, 1); while (c != '\n') { Msg_text[msg_count++] = c; tty_read(&c, 1); } Msg_text[msg_count] = '\0'; printf("\n %s ", Msg_text); tty_read(&c, 1); while (c != 'K') { tty_read(&c, 1); } if ((fp1 = fopen("./message/inbox.dat", "a+")) == NULL) { printf("open file inbox.dat error, program ending !"); } // printf("now find the file\n"); // fprintf(fp1,"%d",Mes_index); inbox.index = Mes_index; memset(inbox.name, 20, 0); strcpy(inbox.PhoneNumber, Tel_Num); strcpy(inbox.Msgtime, Data); strcpy(inbox.Msgtext, Msg_text); fwrite(&inbox, sizeof(Message), 1, fp1); fclose(fp1); } } } }
int main() { int fd; int i = TESTNUM; int shared_data_size = (1 * sizeof(struct shm_data)); struct shm_data *msg; /* shared memory */ fd = shm_open(SHM_PATH, O_CREAT | O_RDWR, S_IRWXU | S_IRWXG); if (fd < 0) err("shm_open"); int r = ftruncate(fd, shared_data_size); if (r != 0) err("ftruncate"); msg = (struct shm_data *) mmap(NULL, shared_data_size, PROT_WRITE, MAP_SHARED, fd, 0); if (msg == NULL) err("mmap"); /* write cmd in shm */ /* while(i >= 0) { unsigned addr = (unsigned) rand() % 70; r = msg_write(msg, addr); msg_read(msg, addr, r); i--; } */ char * rabbit[6]; rabbit[0] = "bitmaps/rabbit1.bmp"; rabbit[1] = "bitmaps/rabbit2.bmp"; rabbit[2] = "bitmaps/rabbit3.bmp"; rabbit[3] = "bitmaps/rabbit4.bmp"; rabbit[4] = "bitmaps/rabbit5.bmp"; rabbit[5] = "bitmaps/rabbit6.bmp"; unsigned addr = (unsigned) rand() % 10; FILE * fp = fopen(rabbit[0], "rb"); fseek(fp, 0L, SEEK_END); unsigned size = ftell(fp); fseek(fp, 0L, SEEK_SET); void * img = malloc(size+1); int ieee = 0; ieee = fread(img, size, 1, fp); printf("readed %d\n", ieee); msg_write(msg, addr, img); fclose(fp); // free(img); img = msg_read(msg, addr, size); fp = fopen("bitmaps/rabbit_0.bmp", "wb+"); fwrite(img, size, 1,fp); fclose(fp); free(img); /* unsigned addr = (unsigned) rand() % 10; int idx, size; for (idx = 0; idx < 6; idx++) { FILE * fp = fopen(rabbit[0], "rb"); fseek(fp, 0L, SEEK_END); size = ftell(fp); fseek(fp, 0L, SEEK_SET); unsigned char * img = (unsigned char *) malloc(size); fread(img, sizeof(unsigned char), size, fp); msg_write(msg, addr, img); fclose(fp); free(img); addr += size + 10; } FILE * fp = fopen( unsigned addr0 = (unsigned) rand() % 41; unsigned addr1 = addr0 + sizeof(bmp0) + 2; */ if (shm_unlink(SHM_PATH) != 0) err("shm_unlink"); return 0; }
static int msg_read_u32(msg_t *m, uint32_t *v) { return msg_read(m, v, 4); }