void readcb(struct bufferevent *bev, void *ctx) { struct evbuffer *input, *output; char *line; size_t n; int i; input=bufferevent_get_input(bev); output=bufferevent_get_output(bev); while( (line=evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) { for(i=0;i<n;++i) { line[i]=rot13_char(line[i]); } evbuffer_add(output, line, n); evbuffer_add(output, "\n", 1); free(line); } if(evbuffer_get_length(input)>=MAX_LINE) { char buf[1024]; while(evbuffer_get_length(input)) { int n=evbuffer_remove(input, buf, sizeof(buf)); for(i=0;i<n;++i) { buf[i]=rot13_char(buf[i]); } evbuffer_add(output, buf, n); } evbuffer_add(output, "\n", 1); } }
void readcb(struct bufferevent *bev, void *ctx) { struct evbuffer *input, *output; char *line; size_t n; int i; input = bufferevent_get_input(bev); output = bufferevent_get_output(bev); while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) { for (i = 0; i < n; ++i) line[i] = rot13_char(line[i]); evbuffer_add(output, line, n); evbuffer_add(output, "\n", 1); free(line); } if (evbuffer_get_length(input) >= MAX_LINE) { /* Too long; just process what there is and go on so that the buffer * doesn't grow infinitely long. */ char buf[1024]; while (evbuffer_get_length(input)) { int n = evbuffer_remove(input, buf, sizeof(buf)); for (i = 0; i < n; ++i) buf[i] = rot13_char(buf[i]); evbuffer_add(output, buf, n); } evbuffer_add(output, "\n", 1); } }
void do_read(evutil_socket_t fd, short events, void *arg) { struct fd_state *state = arg; char buf[1024]; int i; ssize_t result; while (1) { assert(state->write_event); result = recv(fd, buf, sizeof(buf), 0); if (result <= 0) break; printf("DEBUG: read a msg: %s\n", buf); for (i=0; i < result; ++i) { if (state->buffer_used < sizeof(state->buffer)) state->buffer[state->buffer_used++] = rot13_char(buf[i]); if (buf[i] == '\n') { assert(state->write_event); event_add(state->write_event, NULL); state->write_upto = state->buffer_used; } } } if (result == 0) { free_fd_state(state); } else if (result < 0) { if (errno == EAGAIN) // XXXX use evutil macro return; perror("recv"); free_fd_state(state); } }
int do_read(int fd, struct fd_state *state) { char buf[1024]; int i; ssize_t result; while (1) { result = recv(fd, buf, sizeof(buf), 0); if (result <= 0) break; for (i=0; i < result; ++i) { if (state->buffer_used < sizeof(state->buffer)) state->buffer[state->buffer_used++] = rot13_char(buf[i]); if (buf[i] == '\n') { state->writing = 1; state->write_upto = state->buffer_used; } } printf("Server : Receive %s\n", buf); } if (result == 0) { return 1; } else if (result < 0) { if (errno == EAGAIN) return 0; return -1; } return 0; }
int main(int argc, char * argv[] ) { FILE * input; //Input File FILE * output; char iChar; char nowChar; char extraChar; input=fopen(argv[1], "r"); output=fopen(argv[2],"w"); if (input == NULL || output == NULL) { printf("Usage: ./shitrot <input file> <output file>\n"); return 1; } int i = 0; do{ iChar = fgetc(input); if(iChar!=EOF){ if(isalpha(iChar)){ nowChar =rot13_char(iChar); fputc(nowChar,output); extraChar = 97 + ((i+iChar)%26); fputc(extraChar,output); i++; } } }while(iChar != EOF); fclose(input); fclose(output); }
void child(int fd) { char outbuf[MAX_LINE+1]; size_t outbuf_used = 0; size_t result; while(1){ char ch; result = recv(fd, &ch, 1, 0); if(result == 0){ break; } else if(result == -1){ perror("read"); break; } /* We do this test to keep the user from overflowing the buffer. */ if(outbuf_used < sizeof(outbuf)){ outbuf[outbuf_used++] = rot13_char(ch); } if(ch == '\n'){ send(fd, outbuf, outbuf_used, 0); outbuf_used = 0; continue; } } }
void child(int fd) { char outbuf[MAX_LINE+1]; size_t outbuf_used = 0; ssize_t result; printf("process handle connection...\n"); while (1) { char ch; result = recv(fd, &ch, 1, 0); if (result == 0) { printf("result is empty...\n"); break; } else if (result == -1) { perror("read"); break; } //we do this test to keep the user from overflowing the buffer. if (outbuf_used < sizeof(outbuf)) { outbuf[outbuf_used++] = rot13_char(ch); } if (ch == '\n') { send(fd, outbuf, outbuf_used, 0); outbuf_used = 0; continue; } } printf("process handle end...\n"); }
void do_read(evutil_socket_t fd, short events, void *arg) { struct fd_state *state = arg; char buf[1024]; int i; ev_ssize_t result; printf("do_read(): fd = %d\r\n", fd); while (1) { assert(state->write_event); result = recv(fd, buf, sizeof(buf), 0); printf("recv(): result = %d\r\n", result); if (result <= 0) break; for (i=0; i < result; ++i) { if (state->buffer_used < sizeof(state->buffer)) state->buffer[state->buffer_used++] = rot13_char(buf[i]); if (buf[i] == '\n') { //assert(state->write_event); //event_add(state->write_event, NULL); state->write_upto = state->buffer_used; buf[state->buffer_used] = '\0'; printf("\r\n"); printf("bufsize = %d, buf = %s\r\n", state->buffer_used, buf); } } } if (result == 0) { free_fd_state(state); } else if (result < 0) { //if (errno == EAGAIN) // XXXX use evutil macro // return; int err = evutil_socket_geterror(fd); if (EVUTIL_ERR_RW_RETRIABLE(err)) { // WSAEWOULDBLOCK or WSAEINTR printf("event_del(state->read_event);\r\n"); event_del(state->read_event); return; } else { perror("recv"); free_fd_state(state); } } }