コード例 #1
0
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);
	}
}
コード例 #2
0
ファイル: rot13-libevent+.c プロジェクト: pank7/pank7-test
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);
    }
}
コード例 #3
0
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);
    }
}
コード例 #4
0
ファイル: server2.c プロジェクト: vfa-taipa/sandbox
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;
}
コード例 #5
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);

}
コード例 #6
0
ファイル: rot13_fork.c プロジェクト: FrankLiu/pine
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;
		}
	}
}
コード例 #7
0
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");
}
コード例 #8
0
ファイル: rot13_client.c プロジェクト: shines77/libevent
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);
        }
    }
}