/* 标准输入传消息过来了 */ void std_readcb(struct bufferevent *bev, void *ptr) { struct bufferevent *sockbev = (struct bufferevent*)ptr; struct evbuffer *input = bufferevent_get_input(bev); bufferevent_write_buffer(sockbev, input); }
/** * Called by libevent when there is data to read. */ void buffered_on_read(struct bufferevent *bev, void *arg) { /* Write back the read buffer. It is important to note that * bufferevent_write_buffer will drain the incoming data so it * is effectively gone after we call it. */ bufferevent_write_buffer(bev, bev->input); }
static void irc_eventcb(struct bufferevent *buf, short what, void *arg) { Server *s = (Server *)arg; if(what == BEV_EVENT_CONNECTED) { s->state = Server::Connected; DEBUGF(bufferevent_enable(s->buf, EV_READ)); struct evbuffer *eb; DEBUGF(eb = evbuffer_new()); DEBUGF(evbuffer_add_printf(eb, "USER %s 0 * :%s\r\nNICK %s\r\n", s->nick, s->nick, s->nick)); DEBUGF(bufferevent_write_buffer(buf, eb)); DEBUGF(evbuffer_free(eb)); s->state = Server::SentIdent; } else { bufferevent_print_error(what, "Disconnected from %s:", s->host); DEBUGF(bufferevent_free(s->buf)); s->buf = NULL; if(s->state == Server::Quitting) { if(s->client->server_quit_cb) s->client->server_quit_cb(s); s->client->delserv(s); } else { s->state = Server::None; // disconnected timeval tv; tv.tv_sec = 10; tv.tv_usec = 0; DEBUGF(evtimer_add(s->reconnect_event, &tv)); } } }
int apolloReturnPacket(struct bufferevent *bev, client_t *client, char *buffer, int length) { evbuffer_add(client->output_buffer, buffer, length); if (bufferevent_write_buffer(bev, client->output_buffer)) { closeClient(client); } return 0; }
void send_server_ident(struct bufferevent *bev) { struct packet_server_ident data; data.hdr.proto = PROTO_EDONKEY; data.hdr.length = sizeof(data) - sizeof(data.hdr); data.opcode = OP_SERVERSTATUS; memcpy(data.hash, g_srv.cfg->hash, sizeof(data.hash)); data.ip = g_srv.cfg->listen_addr_inaddr; data.port = g_srv.cfg->listen_port; data.tag_count = (g_srv.cfg->server_name_len > 0) + (g_srv.cfg->server_descr_len > 0); if (data.tag_count > 0) { struct evbuffer *buf = evbuffer_new(); evbuffer_add(buf, &data, sizeof(data)); if (g_srv.cfg->server_name_len > 0) { struct tag_header th; struct tag_strval *tv; size_t data_len = sizeof(*tv) + g_srv.cfg->server_name_len - 1; tv = (struct tag_strval *) alloca(data_len); th.type = TT_STRING; th.name_len = 1; *th.name = TN_SERVERNAME; tv->len = g_srv.cfg->server_name_len; memcpy(tv->str, g_srv.cfg->server_name, g_srv.cfg->server_name_len); evbuffer_add(buf, &th, sizeof(th)); evbuffer_add(buf, tv, data_len); } if (g_srv.cfg->server_descr_len > 0) { struct tag_header th; struct tag_strval *tv; size_t data_len = sizeof(*tv) + g_srv.cfg->server_descr_len - 1; tv = (struct tag_strval *) alloca(data_len); th.type = TT_STRING; th.name_len = 1; *th.name = TN_DESCRIPTION; tv->len = g_srv.cfg->server_descr_len; memcpy(tv->str, g_srv.cfg->server_descr, g_srv.cfg->server_descr_len); evbuffer_add(buf, &th, sizeof(th)); evbuffer_add(buf, tv, data_len); } { struct packet_header *ph = (struct packet_header *) evbuffer_pullup(buf, sizeof(*ph)); ph->length = evbuffer_get_length(buf) - sizeof(*ph); } bufferevent_write_buffer(bev, buf); evbuffer_free(buf); } else { bufferevent_write(bev, &data, sizeof(data)); } }
void evhtp_send_reply_body(evhtp_request_t * request, evbuf_t * buf) { evhtp_connection_t * c; c = request->conn; bufferevent_write_buffer(c->bev, buf); }
bool NwConnection::write(NwBufferBase* buf) { if (!mBackend.frontend) { return false; } return bufferevent_write_buffer(mBackend.frontend, buf->backend()) == 0; }
static void handle_close(tcp_client* c, struct evbuffer* b) { int rv = 1, size = 0; struct evbuffer* rep = evbuffer_new(); evbuffer_add(rep, &size, sizeof(int)); evbuffer_add(rep, &rv, sizeof(int)); bufferevent_write_buffer(c->buffer_ev, rep); evbuffer_free(rep); tcp_client_remove(c->id); }
void buff_read_cb(struct bufferevent *bev, void *ctx) { struct evbuffer *input = bufferevent_get_input(bev); size_t recvlen = evbuffer_get_length(input); // not enough size of a header if (recvlen < (sizeof(pdu_header) + 1)) { //LOG(INFO) << "not enough size of a header"; return; } uint8_t head_data[sizeof(pdu_header)+1]; evbuffer_copyout(input, head_data, sizeof(pdu_header)+1); struct pdu_header* ptr_header = (struct pdu_header *) (head_data+1); // not enough size of a complete package if (ptr_header->length > recvlen) { //LOG(INFO) << "not enough size, header->length["<<ptr_header->length <<"] received["<<recvlen<<"]" ; return; } else { //LOG(INFO) << "receive size["<< recvlen << "]" ; } void* pdu_data = malloc(ptr_header->length); if (pdu_data == NULL) { LOG(ERROR)<< "malloc failed for pdu data"; return; } std::shared_ptr<void> ap_pdu_data(pdu_data, free); evbuffer_remove(input, pdu_data, ptr_header->length); LOG(INFO) << "package content: " << bin2hex((uint8_t*)pdu_data, ptr_header->length); bool valid = valid_pdu_package(pdu_data, ptr_header->length); if (!valid) { LOG(WARNING) << "not a valid pdu package"; return; } LOG(INFO) << "received complete package"; bufferevent_disable(bev, EV_READ); char* resp_data; //Base64Encode((const unsigned char *)pdu_data, ptr_header->length, &resp_data); uint8_t *pdu_body = (uint8_t *)pdu_data + 1 +sizeof(pdu_header); size_t body_length = ptr_header->length - 2 - sizeof(ptr_header); resp_data = base64((const unsigned char *)pdu_body, body_length); LOG(INFO) << "base64 encode: " << resp_data; std::shared_ptr<void> ap_resp_data(resp_data, free); struct evbuffer* write_evbuf = evbuffer_new(); evbuffer_add_pdu(write_evbuf, resp_data, strlen(resp_data)+1); bufferevent_write_buffer(bev,write_evbuf); evbuffer_free(write_evbuf); bufferevent_enable(bev, EV_WRITE); }
void RequestSend(Server *pServer, u32 type, struct evbuffer *evSend) { struct Poll *poll = pServer->poll; //#define COMPRESS struct evbuffer *evReq = evbuffer_new(); struct Request Req, *pReq; bzero((char *) & Req, Size_Request); evbuffer_add(evReq, (char *) & Req, Size_Request); if (evSend > 0) { evbuffer_add_buffer(evReq, evSend); } debug("len:%d to %s:%d 0x%08x", evbuffer_get_length(evReq), pServer->host, pServer->port, pServer->poll->bev); pReq = (struct Request *) (EVBUFFER_DATA(evReq)); pReq->sizes.UncmprSize = evbuffer_get_length(evReq) - Size_Request_sizes; pReq->hdr.TesterId = config.testerid; pReq->hdr.ReqType = type; #ifdef COMPRESS if (pReq->sizes.UncmprSize > 200) { int lenAlloc = (pReq->sizes.UncmprSize)*1.01 + 12 + Size_Request_sizes; int lenCmpr = lenAlloc - Size_Request_sizes; char *ptrCmpr; ptrCmpr = getNulledMemory(lenAlloc); memcpy(ptrCmpr, pReq, Size_Request_sizes); compress2((Bytef *) ptrCmpr + Size_Request_sizes, (uLongf *) & lenCmpr, (Bytef *) & pReq->hdr, evbuffer_get_length(evReq) - Size_Request_sizes, Z_DEFAULT_COMPRESSION); evbuffer_free(evReq); evReq = evbuffer_new(); evbuffer_add(evReq, ptrCmpr, lenCmpr + Size_Request_sizes); free(ptrCmpr); pReq = (struct Request *) EVBUFFER_DATA(evReq); pReq->sizes.CmprSize = lenCmpr; } #endif pReq->sizes.crc = crc32(0xffffffff, (const Bytef *) pReq, evbuffer_get_length(evReq)); /* #ifdef DEBUG printf(cBLUE"\treq->sizes.CmprSize=%d"cEND, pReq->sizes.CmprSize); printf(cBLUE"\treq->sizes.UncmprSize=%d"cEND, pReq->sizes.UncmprSize); printf(cBLUE"\treq->sizes.crc=0x%08x\n"cEND, pReq->sizes.crc); #ifdef HEXPRINT hexPrint((char *) EVBUFFER_DATA(evReq), evbuffer_get_length(evReq)); #endif #endif */ bufferevent_write_buffer(poll->bev, evReq); evbuffer_free(evReq); }
static void direct_relay_readcb_helper(redsocks_client *client, struct bufferevent *from, struct bufferevent *to) { if (EVBUFFER_LENGTH(to->output) < to->wm_write.high) { if (bufferevent_write_buffer(to, from->input) == -1) redsocks_log_errno(client, LOG_ERR, "bufferevent_write_buffer"); } else { if (bufferevent_disable(from, EV_READ) == -1) redsocks_log_errno(client, LOG_ERR, "bufferevent_disable"); } }
static void ssl_readcb(struct bufferevent * bev, void * arg) { struct evbuffer *in = bufferevent_get_input(bev); printf("Received %zu bytes\n", evbuffer_get_length(in)); printf("----- data ----\n"); printf("%.*s\n", (int)evbuffer_get_length(in), evbuffer_pullup(in, -1)); bufferevent_write_buffer(bev, in); }
// add a part of output buffer to the outgoing request void s3http_client_add_output_data (S3HttpClient *http, char *buf, size_t size) { http->output_sent += size; evbuffer_add (http->output_buffer, buf, size); // send date, if we already are connected and received server's response if (http->connection_state == S3C_connected) { bufferevent_write_buffer (http->bev, http->output_buffer); } }
void send_search_result(struct bufferevent *bev, struct evbuffer *result, size_t count) { struct packet_search_result data; data.hdr.proto = PROTO_EDONKEY; data.hdr.length = sizeof(data) - sizeof(data.hdr) + evbuffer_get_length(result); data.opcode = OP_SEARCHRESULT; data.files_count = count; evbuffer_prepend(result, &data, sizeof(data)); bufferevent_write_buffer(bev, result); }
void evhtp_send_reply_start(evhtp_request_t * request, evhtp_res code) { evhtp_connection_t * c = request->conn; evbuffer * reply_buf=_evhtp_create_reply(request, code); if (!reply_buf) { delete c; return ; } bufferevent_write_buffer(c->bev, reply_buf); evbuffer_free(reply_buf); }
static void handle_open(tcp_client* c, struct evbuffer* b) { int rv = 1, size = 2*sizeof(int); struct evbuffer* rep = evbuffer_new(); evbuffer_add(rep, &size, sizeof(int)); evbuffer_add(rep, &rv, sizeof(int)); evbuffer_add(rep, &c->id, sizeof(int)); evbuffer_add(rep, &NodeID, sizeof(int)); bufferevent_write_buffer(c->buffer_ev, rep); evbuffer_free(rep); }
static void on_output_timer (evutil_socket_t fd, short event, void *ctx) { OutData *out = (OutData *) ctx; struct timeval tv; struct evbuffer *out_buf; char *buf; char c; LOG_debug (HTTP_TEST, "SRV: on output timer .."); if (out->test_id < TID_body && out->timer_count >= evbuffer_get_length (out->out_buf)) { bufferevent_free (out->bev); evconnlistener_disable (out->listener); event_base_loopbreak (out->evbase); LOG_debug (HTTP_TEST, "SRV: All headers data sent !! "); return; } out_buf = evbuffer_new (); if (out->test_id < TID_body) { buf = (char *)evbuffer_pullup (out->out_buf, -1); c = buf[out->timer_count]; evbuffer_add (out_buf, &c, sizeof (c)); out->timer_count++; LOG_debug (HTTP_TEST, "SRV: Sending %zd bytes:\n>>%s<<\n", evbuffer_get_length (out_buf), evbuffer_pullup (out_buf, -1)); } else { if (!out->header_sent) { evbuffer_add_buffer (out_buf, out->out_buf); out->header_sent = TRUE; } /* if (evbuffer_get_length (out->in_file) < 1) { bufferevent_free (out->bev); evconnlistener_disable (out->listener); event_base_loopbreak (out->evbase); LOG_debug (HTTP_TEST, "SRV: All data sent !! "); return; }*/ evbuffer_remove_buffer (out->in_file, out_buf, 1024*100); LOG_debug (HTTP_TEST, "SRV: Sending BODY %zd bytes", evbuffer_get_length (out_buf)); } bufferevent_write_buffer (out->bev, out_buf); evbuffer_free (out_buf); evutil_timerclear(&tv); tv.tv_sec = 0; tv.tv_usec = 500; event_add(out->timeout, &tv); }
/** * 客户端程序,USR/DAEMON都是从应用程序读取到数据了,然后推送到SRV进行转发到服务器 */ void bufferread_cb(struct bufferevent *bev, void *ptr) { P_PORTTRANS p_trans = (P_PORTTRANS)ptr; struct evbuffer *input = bufferevent_get_input(bev); struct evbuffer *output = bufferevent_get_output(bev); if (bev == p_trans->local_bev && p_trans->srv_bev) { bufferevent_write_buffer(p_trans->srv_bev, bufferevent_get_input(bev)); } else if (bev == p_trans->srv_bev && p_trans->local_bev) { bufferevent_write_buffer(p_trans->local_bev, bufferevent_get_input(bev)); } else { SYS_ABORT("WRRRRRR!"); } return; }
/* * if some data are in input buffer, copy it to remote bufferevent output buffer */ void read_callback(struct bufferevent *bev, void *ptr) { struct bev_arg *bev_arg=ptr; size_t len; /* if remote bufferevent exist */ if (bev_arg->remote) { struct bufferevent *bev_remote=bev_arg->remote->bev; /* update stats */ len = evbuffer_get_length(bufferevent_get_input(bev)); if (len) { /* update stats */ if (bev_arg->type==BEV_CLIENT) { bev_arg->listener->input_bytes+=len; } else if (bev_arg->type==BEV_TARGET) { bev_arg->listener->output_bytes+=len; } } /* write data from our intput buffer to remote output buffer */ //if (bufferevent_read_buffer(bev, bufferevent_get_output(bev_remote))==-1) { if (bufferevent_write_buffer(bev_remote, bufferevent_get_input(bev))==-1) { /* if error, close our socket, remote socket and free everything */ bev_arg->listener->nr_conn--; bufferevent_free(bev); bufferevent_free(bev_remote); free(bev_arg->remote); free(bev_arg); return; } /* If remote bufferevent has more data than OUTPUT_BUFFER_LIMIT * disable EV_READ on our bev and enable write_callback on remote bev. * We enable EV_READ again when all data on remote socket buffer are written, * this is done in write_callback() when remote socket write event is triggered. */ if (evbuffer_get_length(bufferevent_get_output(bev_remote)) >= OUTPUT_BUFFER_LIMIT) { bufferevent_disable(bev, EV_READ); bufferevent_setcb(bev_remote, read_callback, write_callback, event_callback, (void *)bev_arg->remote); } } else { /* remote socket is closed, free self */ bev_arg->listener->nr_conn--; bufferevent_free(bev); free(bev_arg); } }
void buf_read_callback(struct bufferevent *incoming, void *arg) { struct evbuffer *evreturn; char *req; req = evbuffer_readline(incoming->input); if (req == NULL) return; evreturn = evbuffer_new(); evbuffer_add_printf(evreturn, "You said %s\n", req); bufferevent_write_buffer(incoming, evreturn); evbuffer_free(evreturn); free(req); }
static void network_manager_read_handler(struct bufferevent* event, void* context) { printf("network_manager_read_handler\n"); char* data = NULL; struct evbuffer* input = bufferevent_get_input(event); struct evbuffer* output = bufferevent_get_output(event); size_t size = evbuffer_get_length(input); data = (char*)malloc(size * 2); if (NULL != data) { memset(data, 0, size * 2); evbuffer_remove(input, data, size); printf("%s\n", data); evbuffer_add_printf(output, "%s%s", data, "hello"); bufferevent_write_buffer(event, output); } }
void evhtp_send_reply(evhtp_request_t * request, evhtp_res code) { evhtp_connection_t * c= request->conn; evbuf_t * reply_buf; request->finished = 1; if (!(reply_buf = _evhtp_create_reply(request, code))) { delete request->conn; return ; } bufferevent_write_buffer(c->bev, reply_buf); evbuffer_free(reply_buf); }
/** * 读取事件,主要进行数据转发 * * 这里命令字段和数据字段分开处理,命令是自己解析,而数据需要转发,需要 * 为数据流程进行优化 */ void thread_bufferread_cb(struct bufferevent *bev, void *ptr) { P_TRANS_ITEM p_trans = (P_TRANS_ITEM)ptr; struct evbuffer *input = bufferevent_get_input(bev); struct evbuffer *output = bufferevent_get_output(bev); if (bev == p_trans->bev_u && p_trans->bev_d) { st_d_print("转发数据包USR->DAEMON"); bufferevent_write_buffer(p_trans->bev_d, bufferevent_get_input(bev)); } else if (bev == p_trans->bev_d && p_trans->bev_u) { st_d_print("转发数据包DAEMON->USR"); bufferevent_write_buffer(p_trans->bev_u, bufferevent_get_input(bev)); } else { SYS_ABORT("WRRRRRR!"); } return; }
static void proxy_cb(struct bufferevent *source, void *priv) { struct imap_context *ctx = priv; struct evbuffer *input = bufferevent_get_input(source); /* pick the right direction, if reading from client_bev, dump to server_bev * and vice versa */ struct bufferevent *target = (source == ctx->client_bev) ? ctx->server_bev : ctx->client_bev; skeeter_log(LOG_INFO, "Proxying %zu bytes from %s.", evbuffer_get_length(input), (source == ctx->client_bev) ? "client" : "server"); debug(LOG_DEBUG, "%.*s", evbuffer_get_length(input), evbuffer_pullup(input, -1)); bufferevent_write_buffer(target, input); }
static void send_mget_result(tcp_client* c, int rv, int n, val** values) { int size = 0, i; struct evbuffer* b = evbuffer_new(); for (i = 0; i < n; i++) size += values[i]->size + sizeof(int); size += sizeof(int); evbuffer_add(b, &size, sizeof(int)); evbuffer_add(b, &rv, sizeof(int)); evbuffer_add(b, &n, sizeof(int)); for (i = 0; i < n; i++) { evbuffer_add(b, &values[i]->size, sizeof(int)); evbuffer_add(b, values[i]->data, values[i]->size); } bufferevent_write_buffer(c->buffer_ev, b); evbuffer_free(b); }
void buffered_on_read(struct bufferevent *bev, void *arg) { client_t *client = (client_t *)arg; char data[4096]; int nbytes; while (bev->input->off > 0) { nbytes = (bev->input->off > 4096) ? 4096 : bev->input->off; evbuffer_remove(bev->input, data, nbytes); evbuffer_add(client->output_buffer, data, nbytes); } if (bufferevent_write_buffer(bev, client->output_buffer)) { errorOut("Error sending data to client on fd %d\n", client->fd); closeClient(client); } }
static void send_get_result(tcp_client* c, val* v) { int rv, size; struct evbuffer* b; if (v == NULL) { send_result(c->buffer_ev, -1); return; } rv = 1; size = v->size + sizeof(int); b = evbuffer_new(); evbuffer_add(b, &size, sizeof(int)); evbuffer_add(b, &rv, sizeof(int)); evbuffer_add(b, &v->size, sizeof(int)); evbuffer_add(b, v->data, v->size); bufferevent_write_buffer(c->buffer_ev, b); evbuffer_free(b); }
void buf_write_callback(struct bufferevent *outgoing, void *arg) { printf("call buf_write_callback\n"); struct evbuffer *ev_write; #if 0 ev_write = evbuffer_new(); evbuffer_add_printf(ev_write, "[kaka]"); bufferevent_write_buffer(outgoing, ev_write); evbuffer_free(ev_write); #endif #if 0 struct evbuffer *incomming_buffer; incomming_buffer = bufferevent_get_input(outgoing); if (incomming_buffer == NULL) { printf("error: failed to get input bufferevent\n"); return; } #endif size_t input_length = 100, output_length = 100; char *input_data, *output_data; input_data = evbuffer_readln(outgoing->input, &input_length, EVBUFFER_EOL_ANY); if (input_data) { printf("buf_write_callback: input = %s\n", input_data); } else { printf("buf_write_callback: input is NULL (%ld), <input=%p, output=%p>\n", input_length, outgoing->input, outgoing->output); } output_data = evbuffer_readln(outgoing->output, &output_length, EVBUFFER_EOL_ANY); if (output_data) { printf("buf_write_callback: output = %s\n", output_data); } else { printf("buf_write_callback: output is NULL (%ld) <input=%p, output=%p>\n", input_length, outgoing->input, outgoing->output); } }
// 44 20 16 03 04 02 03 01 08 25 25 25 25 25 25 25 int deviceBlockDataSendPacket(struct bufferevent *bev, client_t *client, char *packet, int length) { char u8DeviceId[LEN_DEVICE_ID*2+1] = {0}; char u8FileName[LEN_DEVICE_ID*8] = {0}; int totalLength = 0, sendLength = 0; if (length != LEN_DEVICE_DOWNLOAD) { return -3; } hextostring(u8DeviceId ,packet+IDX_DEVICE_ID, LEN_DEVICE_ID); if (*(packet+13) == 0x60) { genBlockFilePathName(u8DeviceId, u8FileName); apollo_printf("block FileName:%s\n", u8FileName); } else if(*(packet+13) == 0x61) { genAgpsFilePathName(u8DeviceId, u8FileName); apollo_printf("agps FileName:%s\n", u8FileName); } else { genBlockFilePathName(u8DeviceId, u8FileName); apollo_printf("block FileName:%s\n", u8FileName); } totalLength = takeDataPacket(u8FileName, client->pBlock->buffer, packet+1); apollo_printf("totalLength:%d\n", totalLength); while (sendLength < totalLength) { if ((totalLength - sendLength) >= LEN_BLOCK_PACKET) { evbuffer_add(client->output_buffer, client->pBlock->buffer+sendLength, LEN_BLOCK_PACKET); sendLength += LEN_BLOCK_PACKET; } else { evbuffer_add(client->output_buffer, client->pBlock->buffer+sendLength, (totalLength - sendLength)); sendLength = totalLength; } if (bufferevent_write_buffer(bev, client->output_buffer)) { if (client != NULL) { if (client->fd >= 0) { close(client->fd); client->fd = -1; } } } } return totalLength; }
void buf_read_callback(struct bufferevent *incomming, void *arg) { struct evbuffer *ev_return; char *req = NULL; struct client *client = (struct client*)arg; size_t input_length; int echo_enable = 1; printf("call buf_read_callback #sock:%d\n", client->fd); if (echo_enable) { // req = evbuffer_readline(incomming->input); req = evbuffer_readln(incomming->input, &input_length, EVBUFFER_EOL_ANY); if (req == NULL) { return; } printf("input data length = %ld\n", input_length); } // Allocate storage for a new evbuffer. ev_return = evbuffer_new(); if (echo_enable) { // Append a formatted string to the end of an evbuffer. evbuffer_add_printf(ev_return, "You said %s\n", req); } // Write data from an evbuffer to a bufferevent buffer. The evbuffer is // being drained as a result. printf("\t before call bufferevent_write_buffer <input:%p, output:%p>\n", incomming->input, incomming->output); bufferevent_write_buffer(incomming, ev_return); printf("\t after call bufferevent_write_buffer <input:%p, output:%p>\n", incomming->input, incomming->output); evbuffer_free(ev_return); free(req); }