Overview
Comment: | Basic socket server written, need to add HTTP support and file serving |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
c34b109ceba1d1905d3e427b42b627d6 |
User & Date: | rkeene on 2014-02-04 05:33:44 |
Other Links: | manifest | tags |
Context
2014-02-04
| ||
07:43 | Implemented basic stubs for sending files check-in: bdec14db58 user: rkeene tags: trunk | |
05:33 | Basic socket server written, need to add HTTP support and file serving check-in: c34b109ceb user: rkeene tags: trunk | |
04:46 | initial empty check-in check-in: d4ba0b0651 user: rkeene tags: trunk | |
Changes
Added .fossil-settings/ignore-glob version [c62bf95d8d].
> > | 1 2 | filed filed.o |
Added Makefile version [798e5fa576].
> > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | CC = gcc CFLAGS = -Wall -Werror -W -pthread -O3 LDFLAGS = -pthread LIBS = -lpthread filed: filed.o filed.o: filed.c clean: rm -f filed filed.o distclean: clean .PHONY: clean distclean |
Added filed.c version [50c442afde].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #include <sys/socket.h> #include <sys/types.h> #include <arpa/inet.h> #include <sys/mman.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h> /* Default values */ #define MAX_FAILURE_COUNT 30 #define PORT 8080 #define THREAD_COUNT 10 #define BIND_ADDR "::" /* Arguments for worker threads */ struct filed_worker_thread_args { int fd; }; static void filed_init(void) { mlockall(MCL_CURRENT | MCL_FUTURE); } static int filed_listen(const char *address, unsigned int port) { struct sockaddr_in6 addr; int pton_ret, bind_ret, listen_ret; int fd; addr.sin6_family = AF_INET6; addr.sin6_flowinfo = 0; addr.sin6_scope_id = 0; addr.sin6_port = htons(port); pton_ret = inet_pton(AF_INET6, address, addr.sin6_addr.s6_addr); if (pton_ret != 1) { return(-1); } fd = socket(AF_INET6, SOCK_STREAM, 0); if (fd < 0) { return(fd); } bind_ret = bind(fd, (const struct sockaddr *) &addr, sizeof(addr)); if (bind_ret < 0) { close(fd); return(-1); } listen_ret = listen(fd, 128); if (listen_ret != 0) { close(fd); return(-1); } return(fd); } static int filed_logging_thread_init(void) { /* XXX:TODO: Unimplemented */ return(0); } static void filed_handle_client(int fd) { /* XXX:TODO: Unimplemented */ fd = fd; return; } static void *filed_worker_thread(void *arg_v) { struct filed_worker_thread_args *arg; struct sockaddr_in6 addr; socklen_t addrlen; int failure_count = 0, max_failure_count = MAX_FAILURE_COUNT; int master_fd, fd; /* Read arguments */ arg = arg_v; master_fd = arg->fd; while (1) { /* Failure loop prevention */ if (failure_count > max_failure_count) { break; } /* Accept a new client */ addrlen = sizeof(addr); fd = accept(master_fd, (struct sockaddr *) &addr, &addrlen); /* * If we fail, make a note of it so we don't go into a loop of * accept() failing */ if (fd < 0) { failure_count++; continue; } /* Reset failure count*/ failure_count = 0; /* Handle socket */ filed_handle_client(fd); /* Cleanup */ close(fd); } /* XXX:TODO: Report error */ return(NULL); } static int filed_worker_threads_init(int fd, int thread_count) { struct filed_worker_thread_args *arg; pthread_t threadid; int pthread_ret; int i; for (i = 0; i < thread_count; i++) { arg = malloc(sizeof(*arg)); arg->fd = fd; pthread_ret = pthread_create(&threadid, NULL, filed_worker_thread, arg); if (pthread_ret != 0) { return(-1); } } return(0); } int main(int argc, char **argv) { int port = PORT, thread_count = THREAD_COUNT; const char *bind_addr = BIND_ADDR; int fd; /* Ignore */ argc = argc; argv = argv; /* Create listening socket */ fd = filed_listen(bind_addr, port); if (fd < 0) { return(1); } /* Become a daemon */ /* XXX:TODO: Become a daemon */ /* Initialize */ filed_init(); /* Create logging thread */ /* XXX:TODO: Check for errors */ filed_logging_thread_init(); /* Create worker threads */ /* XXX:TODO: Check for errors */ filed_worker_threads_init(fd, thread_count); /* Wait for threads to exit */ /* XXX:TODO: Monitor thread usage */ while (1) { sleep(60); } /* Return in failure */ return(2); } |