static ret_code_t on_rw_authorize_req(nrf_ble_escs_t * p_escs, ble_evt_t * p_ble_evt) { ret_code_t err_code; VERIFY_PARAM_NOT_NULL(p_escs); VERIFY_PARAM_NOT_NULL(p_ble_evt); ble_gatts_evt_rw_authorize_request_t *ar = &p_ble_evt->evt.gatts_evt.params.authorize_request; if (ar->type == BLE_GATTS_AUTHORIZE_TYPE_READ) { err_code = on_read(p_escs, p_ble_evt); RETURN_IF_ERROR(err_code); } else if (ar->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) { if (ar->request.write.op == BLE_GATTS_OP_WRITE_REQ || ar->request.write.op == BLE_GATTS_OP_WRITE_CMD) { err_code = on_write(p_escs, p_ble_evt); RETURN_IF_ERROR(err_code); } else if(ar->request.write.op == BLE_GATTS_OP_PREP_WRITE_REQ || ar->request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) { on_long_write(p_escs, p_ble_evt); } else { } } else { return NRF_ERROR_INVALID_STATE; } return NRF_SUCCESS; }
int main( int argc, char * argv[] ) { extern char *optarg ; int c ; while( ( c = getopt ( argc, argv, "h:p:c:m:v" )) != EOF ) { switch ( c ) { case 'h' : gHost = optarg; break; case 'p': gPort = atoi( optarg ); break; case 'c' : gClients = atoi ( optarg ); break; case 'm' : gMsgs = atoi( optarg ); break; case 'v' : case '?' : showUsage( argv[0] ); exit( 0 ); } } #ifdef SIGPIPE signal( SIGPIPE, SIG_IGN ); #endif WSADATA wsaData; int err = WSAStartup( MAKEWORD( 2, 0 ), &wsaData ); if ( err != 0 ) { printf( "Couldn't find a useable winsock.dll.\n" ); return -1; } HANDLE hIocp = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 ); if( NULL == hIocp ) { printf( "CreateIoCompletionPort failed, errno %d\n", WSAGetLastError() ); return -1; } SP_TestClient * clientList = (SP_TestClient*)calloc( gClients, sizeof( SP_TestClient ) ); struct sockaddr_in sin; memset( &sin, 0, sizeof(sin) ); sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr( gHost ); sin.sin_port = htons( gPort ); int totalClients = gClients, i = 0; printf( "Create %d connections to server, it will take some minutes to complete.\n", gClients ); for( i = 0; i < gClients; i++ ) { SP_TestClient * client = clientList + i; memset( client, 0, sizeof( SP_TestClient ) ); client->mFd = socket( AF_INET, SOCK_STREAM, 0 ); if( client->mFd < 0 ) { printf( "socket failed, errno %d, %s\n", errno, strerror( errno ) ); spwin32_pause_console(); return -1; } if( connect( client->mFd, (struct sockaddr *)&sin, sizeof(sin) ) != 0) { printf( "connect failed, errno %d, %s\n", errno, strerror( errno ) ); spwin32_pause_console(); return -1; } if( NULL == CreateIoCompletionPort( (HANDLE)client->mFd, hIocp, (DWORD)client, 0 ) ) { printf( "CreateIoCompletionPort failed, errno %d\n", WSAGetLastError() ); return -1; } if( 0 == ( i % 10 ) ) printf( "." ); } for( i = 0; i < gClients; i++ ) { SP_TestClient * client = clientList + i; on_read( client, &( client->mRecvEvent ) ); on_write( client, &( client->mSendEvent ) ); } printf( "\n" ); time( &gStartTime ); struct timeval startTime, stopTime; sp_gettimeofday( &startTime, NULL ); time_t lastInfoTime = time( NULL ); // start event loop until all clients are exit while( gClients > 0 ) { eventLoop( hIocp ); if( time( NULL ) - lastInfoTime > 5 ) { time( &lastInfoTime ); printf( "waiting for %d client(s) to exit\n", gClients ); } } sp_gettimeofday( &stopTime, NULL ); double totalTime = (double) ( 1000000 * ( stopTime.tv_sec - startTime.tv_sec ) + ( stopTime.tv_usec - startTime.tv_usec ) ) / 1000000; // show result printf( "\n\nTest result :\n" ); printf( "Clients : %d, Messages Per Client : %d\n", totalClients, gMsgs ); printf( "Failure : send %d, WSASend %d, recv %d, WSARecv %d, GQCS %d\n", gStat.mSendFail, gStat.mWSASendFail, gStat.mRecvFail, gStat.mWSARecvFail, gStat.mGQCSFail ); printf( "ExecTimes : %.6f seconds\n\n", totalTime ); printf( "client\tSend\tRecv\n" ); int totalSend = 0, totalRecv = 0; for( i = 0; i < totalClients; i++ ) { SP_TestClient * client = clientList + i; //printf( "client#%d : %d\t%d\n", i, client->mSendMsgs, client->mRecvMsgs ); totalSend += client->mSendMsgs; totalRecv += client->mRecvMsgs; if( INVALID_HANDLE_VALUE != (HANDLE)client->mFd ) { closesocket( client->mFd ); } } printf( "total : %d\t%d\n", totalSend, totalRecv ); printf( "average : %.0f/s\t%.0f/s\n", totalSend / totalTime, totalRecv / totalTime ); free( clientList ); CloseHandle( hIocp ); spwin32_pause_console(); return 0; }
static void on_read_once(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) { uv_read_stop(tcp); on_read(tcp, nread, buf); }
static void win_notify_on_read(grpc_endpoint *ep, grpc_endpoint_read_cb cb, void *arg) { grpc_tcp *tcp = (grpc_tcp *) ep; grpc_winsocket *handle = tcp->socket; grpc_winsocket_callback_info *info = &handle->read_info; int status; DWORD bytes_read = 0; DWORD flags = 0; int error; WSABUF buffer; GPR_ASSERT(!tcp->outstanding_read); GPR_ASSERT(!tcp->shutting_down); tcp_ref(tcp); tcp->outstanding_read = 1; tcp->read_cb = cb; tcp->read_user_data = arg; tcp->read_slice = gpr_slice_malloc(8192); buffer.len = GPR_SLICE_LENGTH(tcp->read_slice); buffer.buf = (char *)GPR_SLICE_START_PTR(tcp->read_slice); /* First let's try a synchronous, non-blocking read. */ status = WSARecv(tcp->socket->socket, &buffer, 1, &bytes_read, &flags, NULL, NULL); info->wsa_error = status == 0 ? 0 : WSAGetLastError(); /* Did we get data immediately ? Yay. */ if (info->wsa_error != WSAEWOULDBLOCK) { info->bytes_transfered = bytes_read; /* This might heavily recurse. */ on_read(tcp, 1); return; } /* Otherwise, let's retry, by queuing a read. */ memset(&tcp->socket->read_info.overlapped, 0, sizeof(OVERLAPPED)); status = WSARecv(tcp->socket->socket, &buffer, 1, &bytes_read, &flags, &info->overlapped, NULL); if (status == 0) { grpc_socket_notify_on_read(tcp->socket, on_read, tcp); return; } error = WSAGetLastError(); if (error != WSA_IO_PENDING) { char *utf8_message = gpr_format_message(WSAGetLastError()); gpr_log(GPR_ERROR, "WSARecv error: %s - this means we're going to leak.", utf8_message); gpr_free(utf8_message); /* I'm pretty sure this is a very bad situation there. Hence the log. What will happen now is that the socket will neither wait for read or write, unless the caller retry, which is unlikely, but I am not sure if that's guaranteed. And there might also be a write pending. This means that the future orphanage of that socket will be in limbo, and we're going to leak it. I have no idea what could cause this specific case however, aside from a parameter error from our call. Normal read errors would actually happen during the overlapped operation, which is the supported way to go for that. */ tcp->outstanding_read = 0; tcp_unref(tcp); cb(arg, NULL, 0, GRPC_ENDPOINT_CB_ERROR); /* Per the comment above, I'm going to treat that case as a hard failure for now, and leave the option to catch that and debug. */ __debugbreak(); return; } grpc_socket_notify_on_read(tcp->socket, on_read, tcp); }
int ipip_attach(int argc, char *argv[], void *p) { char *ifname = "ipip"; int fd; int port = IP4_PTCL; int type = USE_IP; struct edv_t *edv; struct iface *ifp; struct sockaddr_in addr; if (argc >= 2) ifname = argv[1]; if (if_lookup(ifname) != NULL) { printf("Interface %s already exists\n", ifname); return -1; } if (argc >= 3) switch (*argv[2]) { case 'I': case 'i': type = USE_IP; break; case 'U': case 'u': type = USE_UDP; break; default: printf("Type must be IP or UDP\n"); return -1; } if (argc >= 4) port = atoi(argv[3]); if (type == USE_IP) fd = socket(AF_INET, SOCK_RAW, port); else fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { printf("cannot create socket: %s\n", strerror(errno)); return -1; } if (type == USE_UDP) { memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); if (bind(fd, (struct sockaddr *) &addr, sizeof(addr))) { printf("cannot bind address: %s\n", strerror(errno)); close(fd); return -1; } } ifp = (struct iface *) callocw(1, sizeof(struct iface)); ifp->name = strdup(ifname); ifp->addr = Ip_addr; ifp->broadcast = 0xffffffffUL; ifp->netmask = 0xffffffffUL; ifp->mtu = MAX_FRAME; ifp->flags = NO_RT_ADD; setencap(ifp, "None"); edv = (struct edv_t *) malloc(sizeof(struct edv_t)); edv->type = type; edv->port = port; edv->fd = fd; ifp->edv = edv; ifp->send = ipip_send; on_read(fd, ipip_receive, (void * ) ifp); ifp->next = Ifaces; Ifaces = ifp; return 0; }
static int begin_process(lsi_session_t *session) { #define VALMAXSIZE 4096 #define LINEMAXSIZE (VALMAXSIZE + 50) char val[VALMAXSIZE], line[LINEMAXSIZE] = {0}; int n; int i; const char *p; char *buf; mydata_t *mydata = (mydata_t *)g_api->get_module_data(session, &MNAME, LSI_MODULE_DATA_HTTP); ls_xpool_t *pPool = g_api->get_session_pool(session); //Create response body append(session, CONTENT_HEAD, 0); //Original request header n = g_api->get_req_raw_headers_length(session); buf = (char *)ls_xpool_alloc(pPool, n + 1); memset(buf, 0, n + 1); n = g_api->get_req_raw_headers(session, buf, n + 1); append(session, "Original request<table border=1><tr><td><pre>\r\n", 0); append(session, buf, n); append(session, "\r\n</pre></td></tr>\r\n", 0); ls_xpool_free(pPool, buf); append(session, "\r\n</table><br>Request headers<br><table border=1>\r\n", 0); for (i = 0; i < sizeof(reqhdr_array) / sizeof(char *); ++i) { p = g_api->get_req_header(session, reqhdr_array[i], strlen(reqhdr_array[i]), &n); if ((p != NULL) && p[0] != 0 && n > 0) { memcpy(val, p, n); val[n] = '\0'; n = snprintf(line, LINEMAXSIZE - 1, CONTENT_FORMAT, reqhdr_array[i], val); append(session, line, n); } } append(session, "\r\n</table><br>Server req env<br><table border=1>\r\n", 0); //Server req env for (i = LSI_REQ_VAR_REMOTE_ADDR; i < LSI_REQ_COUNT; ++i) { n = g_api->get_req_var_by_id(session, i, val, VALMAXSIZE); if (n > 0) { val[n] = '\0'; n = snprintf(line, LINEMAXSIZE - 1, CONTENT_FORMAT, req_array[i], val); append(session, line, n); } } append(session, "\r\n</table><br>env varibles<br><table border=1>\r\n", 0); for (i = 0; i < sizeof(env_array) / sizeof(char *); ++i) { //env varibles n = g_api->get_req_env(session, env_array[i], strlen(env_array[i]), val, VALMAXSIZE); if (n > 0) { val[n] = '\0'; n = snprintf(line, LINEMAXSIZE - 1, CONTENT_FORMAT, env_array[i], val); append(session, line, n); } } p = g_api->get_req_cookies(session, &n); if ((p != NULL) && p[0] != 0 && n > 0) { append(session, "\r\n</table><br>Request cookies<br><table border=1>\r\n", 0); append(session, "<tr><td>Cookie</td><td>", 0); append(session, p, n); append(session, "</td></tr>", 0); } n = g_api->get_req_cookie_count(session); if (n > 0) { //try get a certen cookie p = g_api->get_cookie_value(session, "LSWSWEBUI", 9, &n); if ((p != NULL) && n > 0) { append(session, "<tr><td>cookie_LSWSWEBUI</td><td>", 0); append(session, p, n); append(session, "</td></tr>", 0); } } append(session, "</table>", 0); n = get_reqbody_dealertype(session); mydata->req_body_len = g_api->get_req_content_length(session); mydata->rcvd_req_body_len = 0; mydata->type = n; sprintf(line, "Will deal with the req body.Type = %d, req body lenghth = %d<br>", n, mydata->req_body_len); append(session, line, 0); if (mydata->type == 0) { append(session, CONTENT_TAIL, 0); mydata->rcvd_done = 1; mydata->resp_done = 1; } g_api->set_status_code(session, 200); if (mydata->type == 3) // Save to file mydata->fp = fopen("/tmp/uploadfile", "wb"); else if (mydata->type == 2) // Md5 MD5_Init(&mydata->ctx); g_api->flush(session); // if ( mydata->type != 0) on_read(session); //g_api->end_resp(session); return 0; }
static void tcp_ready(struct tcb *tcb, int32 cnt) { if (tcb->user > 0) on_read(tcb->user, (void (*)(void *)) tcp_send, tcb); }