Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <signal.h>
- static void signal_action(int signo, siginfo_t *si, void *ctx) {
- exit(0);
- }
- size_t compress(const char* prev_buf, char* data, size_t n, char* cur_buf) {
- return 15;
- }
- size_t get_compress_buf_size() {
- return 10000;
- }
- int main(int argc, char *argv[]) {
- // EXIT PROPERLY BULLSHT
- struct sigaction sig_a;
- memset(&sig_a, 0, sizeof(sig_a));
- sig_a.sa_flags = SA_SIGINFO | SA_RESTART /*| SA_NODEFER*/;
- sig_a.sa_sigaction = signal_action;
- if (sigaction(SIGTERM, &sig_a, NULL) < 0) {
- perror("Sigaction fault");
- fflush(NULL);
- return 1;
- }
- if (sigaction(SIGPIPE, &sig_a, NULL) < 0) {
- perror("Sigaction fault");
- fflush(NULL);
- return 1;
- }
- // EXIT PROPERLY BULLSHT - END
- int BLOCKSIZE = atoi(argv[2]);
- // START SERVER BULLSHT
- int fd = socket(PF_INET, SOCK_STREAM, 0);
- if (fd < 0) {
- perror("Couldn't start server\n");
- exit(1);
- }
- int val = 1;
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
- setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
- struct sockaddr_in sa;
- sa.sin_family = AF_INET;
- sa.sin_addr.s_addr = INADDR_ANY;
- sa.sin_port = htons(atoi(argv[1]));
- if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
- perror("Couldn't bind");
- exit(1);
- }
- if (listen(fd, 1) < 0) {
- perror("Cound't listen");
- exit(1);
- }
- // START SERVER BULLSHT - END
- char buf[BLOCKSIZE];
- char prev_buf[get_compress_buf_size()];
- char data[get_compress_buf_size()];
- char cur_buf[get_compress_buf_size()];
- while (1) {
- struct sockaddr_in ss;
- socklen_t sz = sizeof(ss);
- int afd = accept(fd, (void*) &ss, &sz);
- if (afd < 0) {
- perror("Couldn't accept");
- continue;
- }
- // printf("client: %s, %d\n", inet_ntoa(ss.sin_addr), ntohs(ss.sin_port));
- int msg_size = 0;
- int total_size = 0;
- size_t compressed_size = 0;
- int ok = 1;
- while ((msg_size = read(afd, buf, BLOCKSIZE)) > 0) {
- total_size += msg_size;
- if (total_size > BLOCKSIZE) {
- int tmp = htonl(0);
- if (write(afd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
- perror("Couldn't print");
- continue;
- }
- fflush(NULL);
- ok = 0;
- break;
- } else {
- memcpy(data, buf, msg_size);
- compressed_size += compress(prev_buf, data, msg_size, cur_buf);
- memcpy(prev_buf, cur_buf, get_compress_buf_size());
- }
- }
- if (ok) {
- compressed_size = htonl(compressed_size);
- if (write(afd, &compressed_size, sizeof(compressed_size)) != sizeof(compressed_size)) {
- perror("Couldn't print");
- continue;
- }
- fflush(NULL);
- }
- close(afd);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment