int forwarder(int from,int to) { int rec,sen; fd_set rfds; char buffer[BUFSIZ+1]; while(1){ FD_ZERO(&rfds); FD_SET(from, &rfds); FD_SET(to, &rfds); int bigger = (from>to?from:to)+1; if(select(bigger, &rfds, NULL, NULL, NULL)==0) continue; if(FD_ISSET(from,&rfds)){ if ((rec = recv(from, buffer, BUFSIZ,0)) > 0){ sen=msend(to, &buffer, rec); //printf("%d => %d\n",rec,sen); if(sen<=0) break; } else break; } if(FD_ISSET(to,&rfds)){ if ((rec = recv(to, buffer, BUFSIZ,0)) > 0){ sen=msend(from, &buffer, rec); //printf("%d <= %d\n",rec,sen); if(sen<=0) break; } else break; } } return 0; }
int DealRequest(int sock, char *buf) { int ret = 0; printf("Deal Request\n"); if(strncasecmp(buf, "login", 5) != 0 && logined != 1) { msend(sock, "401 Please login first\r\n"); return -1; } if(strncasecmp(buf, "list", 4) == 0) { ret = DoList(sock, buf); } else if(strncasecmp(buf, "cd", 2) == 0) { ret = DoChdir(sock, buf); } else if(strncasecmp(buf, "login", 5) == 0) { ret = DoLogin(sock, buf); } else if(strncasecmp(buf, "get", 3) == 0) { ret = DoGet(sock, buf); } else if(strncasecmp(buf, "quit", 4) == 0) { ret = DoQuit(sock, buf); } else { printf("Unknown Request:%s\n", buf); msend(sock, "400 Unknown command\r\n"); } return ret; }
void do_send() { int i=0; mfile *m; mfile *n; for(m=mftop[MFSEND];m;m=n){ if(i == moption.parallel){ return; } n = m->next; if(m->mdata.head.flags & MAKUO_FLAG_ACK){ msend(m); continue; } if(!is_send(m)){ if(m->sendwait){ i++; } continue; } msend(m); i++; } }
int main() { int s[2]; /* Send-throttling. */ int rc = unix_pair(s); assert(rc == 0); int pfx0 = pfx_start(s[0]); assert(pfx0 >= 0); int pfx1 = pfx_start(s[1]); assert(pfx1 >= 0); int thr = mthrottler_start(pfx0, 1000, 10, 0, 0); assert(thr >= 0); int64_t start = now(); int i; for(i = 0; i != 95; ++i) { rc = msend(thr, "ABC", 3, -1); assert(rc == 0); } int64_t elapsed = now() - start; assert(elapsed > 80 && elapsed < 100); char buf[3]; for(i = 0; i != 95; ++i) { ssize_t sz = mrecv(pfx1, buf, sizeof(buf), -1); assert(sz == 3); } hclose(thr); hclose(pfx1); /* Recv-throttling. */ rc = unix_pair(s); assert(rc == 0); int crlf0 = crlf_start(s[0]); assert(pfx0 >= 0); int crlf1 = crlf_start(s[1]); assert(pfx1 >= 0); thr = mthrottler_start(crlf0, 0, 0, 1000, 10); assert(thr >= 0); for(i = 0; i != 95; ++i) { rc = msend(crlf1, "ABC", 3, -1); assert(rc == 0); } start = now(); for(i = 0; i != 95; ++i) { rc = mrecv(thr, buf, sizeof(buf), -1); assert(rc == 0); } elapsed = now() - start; assert(elapsed > 80 && elapsed < 100); hclose(thr); hclose(crlf1); return 0; }
coroutine void dialogue(int s) { int rc = msend(s, "What's your name?", 17, -1); if(rc != 0) goto cleanup; char inbuf[256]; ssize_t sz = mrecv(s, inbuf, sizeof(inbuf), -1); if(sz < 0) goto cleanup; inbuf[sz] = 0; char outbuf[256]; rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!", inbuf); rc = msend(s, outbuf, rc, -1); if(rc != 0) goto cleanup; cleanup: rc = hclose(s); assert(rc == 0); }
int rp_share(uint64_t rp, void *buf, size_t size, uint64_t offset, int prot) { struct msg *msg; int err; /* check alignment */ if ((uintptr_t) buf % PAGESZ) { return 1; } msg = aalloc(PAGESZ + size, PAGESZ); if (!msg) return 1; msg->source = RP_CURRENT_THREAD; msg->target = rp; msg->length = PAGESZ - sizeof(struct msg) + size; msg->port = PORT_SHARE; msg->arch = ARCH_NAT; ((uint64_t*) msg->data)[0] = offset; page_self(buf, &msg->data[PAGESZ - sizeof(struct msg)], size); page_prot(&msg->data[PAGESZ - sizeof(struct msg)], size, prot); if (msend(msg)) return 1; msg = mwait(PORT_REPLY, rp); if (msg->length != 1) { err = 1; } else { err = msg->data[0]; } free(msg); return err; }
static int lz4_msendv(struct msock_vfs *mvfs, const struct iovec *iov, size_t iovlen, int64_t deadline) { struct lz4_sock *obj = dsock_cont(mvfs, struct lz4_sock, mvfs); /* Adjust the buffer size as needed. */ size_t len = iov_size(iov, iovlen); size_t maxlen = LZ4F_compressFrameBound(len, NULL); if(obj->outlen < maxlen) { uint8_t *newbuf = realloc(obj->outbuf, maxlen); if(dsock_slow(!newbuf)) {errno = ENOMEM; return -1;} obj->outbuf = newbuf; obj->outlen = maxlen; } /* Compress the data. */ /* TODO: Avoid the extra allocations and copies. */ uint8_t *buf = malloc(len); if(dsock_slow(!buf)) {errno = ENOMEM; return -1;} iov_copyallfrom(buf, iov, iovlen); LZ4F_preferences_t prefs = {0}; prefs.frameInfo.contentSize = len; size_t dstlen = LZ4F_compressFrame(obj->outbuf, obj->outlen, buf, len, &prefs); dsock_assert(!LZ4F_isError(dstlen)); dsock_assert(dstlen <= obj->outlen); free(buf); /* Send the compressed frame. */ return msend(obj->s, obj->outbuf, dstlen, deadline); }
char *fs_list(uint64_t dir, int entry) { struct msg *msg; char *name; msg = aalloc(sizeof(struct msg) + sizeof(uint32_t), PAGESZ); if (!msg) return NULL; msg->source = RP_CONS(getpid(), 0); msg->target = dir; msg->length = sizeof(uint32_t); msg->port = PORT_LIST; msg->arch = ARCH_NAT; ((uint32_t*) msg->data)[0] = entry; if (msend(msg)) return NULL; msg = mwait(PORT_REPLY, dir); if (msg->length == 0) { free(msg); return NULL; } name = strdup((const char*) msg->data); free(msg); return name; }
size_t rp_read(uint64_t file, void *buf, size_t size, uint64_t offset) { struct msg *msg; msg = aalloc(sizeof(struct msg) + sizeof(uint64_t) + sizeof(uint32_t), PAGESZ); if (!msg) return 0; msg->source = RP_CURRENT_THREAD; msg->target = file; msg->length = sizeof(uint64_t) + sizeof(uint32_t); msg->port = PORT_READ; msg->arch = ARCH_NAT; ((uint64_t*) msg->data)[0] = offset; ((uint32_t*) msg->data)[2] = size; if (msend(msg)) return 0; msg = mwait(PORT_REPLY, file); if (size > msg->length) { size = msg->length; } if (size) memcpy(buf, msg->data, size); free(msg); return size; }
void QTTEST::on_cmdButton_clicked() { char sends[1024]="Server> /cmd "; std::string cmd_text = (ui.cmdEdit->text()).toLocal8Bit().constData(); char *cstr = new char[cmd_text.length() + 1]; strcpy(cstr, cmd_text.c_str()); strcat(sends, cstr); msend(sends); }
void sendSTOP() { buf[0] = STOP; msend(buf,1); mylogfd(MOTOFD,"[moto]sendSTOP\n"); vleft = 0; vright = 0; mylogfd(1,"[moto]stop\n"); }
void broadcastOthers(char * data, int me){ int len = strlen(data); int j; for(j = 0; j<=fdmax; j++){ if(FD_ISSET(j, &master) && j!=me && j!=listener && ol_list[j]){ msend(j, data, len, 0); } } }
int DoGet(int sock, char *req) { int fd; int size; char filename[256]; char *p; struct stat st; char resp[256]; printf("In %s\n", __func__); p = strchr(req, ' '); if(p == NULL) { msend(sock, "400 Invalid parameter\r\n"); return 0; } memset(filename, 0, sizeof(filename)); strncpy(filename, p+1, 255); p = strrchr(filename, '\n'); if(p != NULL) { *p = '\0'; } p = strrchr(filename, '\r'); if(p != NULL) { *p = '\0'; } fd = open(filename, O_RDONLY); if(fd < 0) { msend(sock, "500 Open file failed\r\n"); return 0; } fstat(fd, &st); sprintf(resp, "200 OK %d\r\n", (int)st.st_size); msend(sock, resp); while(1) { size = read(fd, resp, sizeof(resp)); if(size <= 0) break; send(sock, resp, size, 0); } close(fd); return 0; }
static int mthrottler_msend(int s, const void *buf, size_t len, int64_t deadline) { struct mthrottlersock *obj = hdata(s, msock_type); dsock_assert(obj->vfptrs.type == mthrottler_type); /* If send-throttling is off forward the call. */ if(obj->send_full == 0) return msend(obj->s, buf, len, deadline); /* If there's no quota wait till it is renewed. */ if(!obj->send_remaining) { int rc = msleep(obj->send_last + obj->send_interval); if(dsock_slow(rc < 0)) return -1; obj->send_remaining = obj->send_full; obj->send_last = now(); } /* Send the message. */ int rc = msend(obj->s, buf, len, deadline); if(dsock_slow(rc < 0)) return -1; --obj->send_remaining; return 0; }
int get_width_map(t_all *all) { char *b; b = (char*)bunny_ini_get_field(all->ini, INI_NAME, "width", 0); all->width = my_getnbr(b); if (all->whoami == 2) msend(all->sock, b); return (0); }
int get_height_map(t_all *all) { char *b; b = (char*)bunny_ini_get_field(all->ini, INI_NAME, "height", 0); all->height = my_getnbr(b); if (all->whoami == 2) msend(all->sock, b); return (0); }
void broadcastSelf(char * data, char * user){ int len = strlen(data); int j; for(j = 0; j<=fdmax; j++){ if(FD_ISSET(j, &master) && j!=listener && ol_list[j]){ if(user_list[j][0] && !strcmp(user, user_list[j])){ msend(j, data, len, 0); } } } }
void broadcastOne(char *data){ int len = strlen(data); int j; for(j = 0; j<=fdmax; j++){ if(FD_ISSET(j, &master) && !strcmp(user_list[j], pm_global)){ msend(j, data, len, 0); //printf("PM-ing to %s\n", pm_global); //printf("%s", data); } } }
void ciCsdSendMsgToServer(int procId, unsigned char *msg, int msglen) { ShmApiMsg *pShmArgs; pShmArgs = (ShmApiMsg *) msg; pShmArgs->svcId = CiCsdStubSvc_Id; pShmArgs->msglen = msglen; pShmArgs->procId = procId; msend(cicsdstubsockfd, msg, msglen + SHM_HEADER_SIZE, MSOCKET_ATOMIC); }
static int mlog_msend(int s, const void *buf, size_t len, int64_t deadline) { struct mlogsock *obj = hdata(s, msock_type); dsock_assert(obj->vfptrs.type == mlog_type); fprintf(stderr, "send %8zuB: 0x", len); size_t i; for(i = 0; i != len; ++i) fprintf(stderr, "%02x", (int)((uint8_t*)buf)[i]); fprintf(stderr, "\n"); return msend(obj->s, buf, len, deadline); }
/* *send AA_DISTANCE to motor,left = vleft,right = vright,设置buf后通过msend发送 */ void sendAA(int left, int right, int a) { buf[0] = HEAD3; buf[1] = left; buf[2] = right; buf[3] = a; buf[4] = AA_DISTANCE; msend(buf, 5); mylogfd(-1,"<%d,%d>", left, right); vleft = left; vright = right; }
void QTTEST::on_sendEdit_returnPressed() { std::string current_locale_text = (ui.sendEdit->text()).toLocal8Bit().constData(); char *cstr = new char[current_locale_text.length() + 1]; strcpy(cstr, current_locale_text.c_str()); char sends[1024]="Server> "; strcat(sends,cstr); msend(this, sends); ui.textBrowser->append(sends); //Changes the text on the Text-label ui.sendEdit->setText(""); }
void pingpong(int n) { int i; unsigned int s; struct timeval tv; mfile *m = mfins(MFSEND); mping *p = NULL; char buff[MAKUO_HOSTNAME_MAX + 1]; if(!m){ lprintf(0, "[error] %s: out of memmory\r\n", __func__); return; } m->mdata.head.reqid = getrid(); m->mdata.head.seqno = 0; m->mdata.head.szdata = 0; m->sendwait = 0; if(gethostname(buff, sizeof(buff)) == -1){ buff[0] = 0; } p = (mping *)(m->mdata.data); p->hostnamelen = strlen(buff); p->versionlen = strlen(PACKAGE_VERSION); m->mdata.head.szdata = sizeof(mping) + p->hostnamelen + p->versionlen; m->mdata.p = p->data; memcpy(m->mdata.p, buff, p->hostnamelen); m->mdata.p += p->hostnamelen; memcpy(m->mdata.p, PACKAGE_VERSION, p->versionlen); m->mdata.p += p->versionlen; p->hostnamelen = htons(p->hostnamelen); p->versionlen = htons(p->versionlen); gettimeofday(&lastpong, NULL); switch(n){ case 0: for(i=0;buff[i];i++){ s += buff[i]; } gettimeofday(&tv,NULL); s += tv.tv_usec; srand(s); m->mdata.head.opcode = MAKUO_OP_PING; break; case 1: m->mdata.head.opcode = MAKUO_OP_PING; m->mdata.head.flags |= MAKUO_FLAG_ACK; break; case 2: m->mdata.head.opcode = MAKUO_OP_EXIT; msend(m); break; } }
coroutine void client(void) { ipaddr addr; int rc = ipaddr_remote(&addr, "127.0.0.1", 5555, 0, -1); assert(rc == 0); int s = tcp_connect(&addr, -1); assert(s >= 0); int cs = crlf_start(s); assert(cs >= 0); rc = msend(cs, "ABC", 3, -1); assert(rc == 0); char buf[3]; ssize_t sz = mrecv(cs, buf, 3, -1); assert(sz == 3); assert(buf[0] == 'G' && buf[1] == 'H' && buf[2] == 'I'); rc = msend(cs, "DEF", 3, -1); assert(rc == 0); s = crlf_stop(cs, -1); assert(s >= 0); rc = hclose(s); assert(rc == 0); }
void mouse_button(int down,int up,int rep) { if(rep==0) { msend(0,0,0,rep,down,up); if(down&BUTTON1) ms.buttons[map_buttons(BUTTON1)]=button_down; if(down&BUTTON2) ms.buttons[map_buttons(BUTTON2)]=button_down; if(down&BUTTON3) ms.buttons[map_buttons(BUTTON3)]=button_down; if(up&BUTTON1) ms.buttons[map_buttons(BUTTON1)]=button_up; if(up&BUTTON2) ms.buttons[map_buttons(BUTTON2)]=button_up; if(up&BUTTON3) ms.buttons[map_buttons(BUTTON3)]=button_up; } }
coroutine void dialogue(int s, int ch) { int op = CONN_ESTABLISHED; int rc = chsend(ch, &op, sizeof(op), -1); assert(rc == 0); int64_t deadline = now() + 60000; rc = msend(s, "What's your name?", 17, deadline); if(rc != 0) goto cleanup; char inbuf[256]; ssize_t sz = mrecv(s, inbuf, sizeof(inbuf), deadline); if(sz < 0) goto cleanup; inbuf[sz] = 0; char outbuf[256]; rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!", inbuf); rc = msend(s, outbuf, rc, deadline); if(rc != 0) goto cleanup; cleanup: op = errno == 0 ? CONN_SUCCEEDED : CONN_FAILED; rc = chsend(ch, &op, sizeof(op), -1); assert(rc == 0 || errno == ECANCELED); rc = hclose(s); assert(rc == 0); }
int msendb(uint64_t target, uint8_t port) { struct msg *msg; msg = aalloc(sizeof(struct msg), PAGESZ); if (!msg) return 1; msg->source = RP_CONS(getpid(), 0); msg->target = target; msg->length = 0; msg->port = port; msg->arch = ARCH_NAT; return msend(msg); }
static BT_u32 i2c_master_transfer(BT_HANDLE hI2C, BT_I2C_MESSAGE *msgs, BT_u32 num, BT_ERROR *pError) { while(hI2C->pRegs->STATUS & STATUS_BA) { BT_ThreadYield(); } if(num > 1) { hI2C->bHold = 1; hI2C->pRegs->CONTROL |= CONTROL_HOLD; } else { hI2C->bHold = BT_FALSE; } BT_u32 count; for(count = 0; count < num; count++, msgs++) { if(count == (num - 1)) { hI2C->bHold = BT_FALSE; } retry: hI2C->err_status = 0; hI2C->p_msg = msgs; if(msgs->flags & BT_I2C_M_TEN) { hI2C->pRegs->CONTROL &= ~CONTROL_NEA; } else { hI2C->pRegs->CONTROL |= CONTROL_NEA; } if(msgs->flags & BT_I2C_M_RD) { mrecv(hI2C); } else { msend(hI2C); } // Wait on transfer complete signal. BT_kMutexPend(hI2C->pMutex, 0); hI2C->pRegs->INT_DISABLE = 0x000002FF; if(hI2C->err_status & INT_MASK_ARB_LOST) { BT_kPrint("ZYNQ I2C: Lost ownership on bus, trying again..."); BT_ThreadSleep(2); goto retry; } } hI2C->p_msg = NULL; hI2C->err_status = 0; return num; }
void QTTEST::on_beepButton_clicked() { char sends[1024]="Server> /beep "; std::string pitch_text = (ui.pitchEdit->text()).toLocal8Bit().constData(); char *cstr = new char[pitch_text.length() + 1]; strcpy(cstr, pitch_text.c_str()); std::string length_text = (ui.lengthEdit->text()).toLocal8Bit().constData(); char *cstr2 = new char[length_text.length() + 1]; strcpy(cstr2, length_text.c_str()); strcat(sends, cstr); strcat(sends, " "); strcat(sends, cstr2); msend(sends); }
int event(rp_t rp, const char *value) { struct msg *msg; if (!value) value = ""; msg = aalloc(sizeof(struct msg) + strlen(value) + 1, PAGESZ); if (!msg) return 1; msg->source = RP_CONS(getpid(), 0); msg->target = rp; msg->length = strlen(value) + 1; msg->action = ACTION_EVENT; msg->arch = ARCH_NAT; strcpy((char*) msg->data, value); return msend(msg); }