core: add all the required files
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
/* ============================================================================
|
||||
* mini_serv — fill-in skeleton.
|
||||
*
|
||||
* Compiles clean with: cc -Wall -Wextra -Werror mini_serv.skeleton.c -o test
|
||||
* It does NOT work until you fill every TODO. That's the point.
|
||||
*
|
||||
* The main() loop and the socket setup are already typed for you (memorize
|
||||
* them anyway). The four functions below are where the exam is won or lost.
|
||||
*
|
||||
* Do not look at reference/mini_serv.c until you have done a full timed
|
||||
* attempt. Hints are in cheatsheet.md (read it once, then close it).
|
||||
* ==========================================================================*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
typedef struct s_client
|
||||
{
|
||||
int fd; /* -1 = free slot */
|
||||
int id;
|
||||
char *buf; /* pending partial line */
|
||||
} t_client;
|
||||
|
||||
t_client g_c[FD_SETSIZE];
|
||||
int g_maxfd;
|
||||
int g_server;
|
||||
int g_next_id;
|
||||
|
||||
void fatal(void)
|
||||
{
|
||||
write(2, "Fatal error\n", 12);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* The exam GIVES YOU these two functions in main.c — copy them verbatim from
|
||||
* subject/main.c (both of them). Type them once by hand in drill D3, then
|
||||
* you'll never think about them again.
|
||||
*
|
||||
* str_join(buf, add) -> new buffer = buf + add (old buf freed)
|
||||
* extract_message(&buf, &msg) -> 1: one \n-terminated line pulled into msg
|
||||
* 0: no complete line yet
|
||||
* -1: allocation failure
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
char *str_join(char *buf, char *add)
|
||||
{
|
||||
/* TODO: copy verbatim from subject/main.c */
|
||||
(void)buf;
|
||||
(void)add;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int extract_message(char **buf, char **msg)
|
||||
{
|
||||
/* TODO: copy verbatim from subject/main.c */
|
||||
(void)buf;
|
||||
(void)msg;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* Broadcast s to every connected client except fd (fd = -1 means "everyone").
|
||||
* Remember: slots are indexed by fd, empty slots have fd == -1.
|
||||
* ------------------------------------------------------------------------- */
|
||||
void to_all_except(int fd, char *s)
|
||||
{
|
||||
/* TODO */
|
||||
(void)fd;
|
||||
(void)s;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* accept() the pending connection, fill its slot (fd, id = g_next_id++, buf),
|
||||
* raise g_maxfd if needed, then broadcast
|
||||
* "server: client %d just arrived\n"
|
||||
* to the OTHER clients (NOT to the newcomer).
|
||||
* If accept fails: just return.
|
||||
* ------------------------------------------------------------------------- */
|
||||
void accept_new(void)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* The client on fd is gone (recv() <= 0):
|
||||
* remember its id, close(fd), mark the slot free (-1), free its buffer,
|
||||
* broadcast "server: client %d just left\n" to everyone else,
|
||||
* then shrink g_maxfd while the last slot is empty (never below g_server).
|
||||
* The leftover partial line in the buffer is DROPPED, never flushed.
|
||||
* ------------------------------------------------------------------------- */
|
||||
void disconnect(int fd)
|
||||
{
|
||||
/* TODO */
|
||||
(void)fd;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------------
|
||||
* recv() -> if <= 0: disconnect(fd).
|
||||
* Otherwise: append the chunk to this client's buffer (str_join), then in a
|
||||
* loop pull every complete line (extract_message) and broadcast
|
||||
* "client %d: <line>"
|
||||
* to the OTHER clients. Free the message extract_message gave you.
|
||||
* A chunk can contain several lines, a partial line, or both.
|
||||
* ------------------------------------------------------------------------- */
|
||||
void handle_read(int fd)
|
||||
{
|
||||
/* TODO */
|
||||
(void)fd;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
fd_set readfds;
|
||||
int i;
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
write(2, "Wrong number of arguments\n", 26);
|
||||
return (1);
|
||||
}
|
||||
g_server = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (g_server < 0)
|
||||
fatal();
|
||||
bzero(&addr, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(2130706433); /* 127.0.0.1 only */
|
||||
addr.sin_port = htons(atoi(argv[1]));
|
||||
if (bind(g_server, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
||||
fatal();
|
||||
if (listen(g_server, 10) < 0)
|
||||
fatal();
|
||||
i = 0;
|
||||
while (i < FD_SETSIZE)
|
||||
{
|
||||
g_c[i].fd = -1;
|
||||
g_c[i].id = -1;
|
||||
g_c[i].buf = NULL;
|
||||
i++;
|
||||
}
|
||||
g_maxfd = g_server;
|
||||
while (1)
|
||||
{
|
||||
/* STEP: rebuild the set from scratch — select() destroys it. */
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(g_server, &readfds);
|
||||
i = 0;
|
||||
while (i <= g_maxfd)
|
||||
{
|
||||
if (g_c[i].fd != -1)
|
||||
FD_SET(i, &readfds);
|
||||
i++;
|
||||
}
|
||||
if (select(g_maxfd + 1, &readfds, NULL, NULL, NULL) < 0)
|
||||
continue ;
|
||||
if (FD_ISSET(g_server, &readfds))
|
||||
accept_new();
|
||||
i = 0;
|
||||
while (i <= g_maxfd)
|
||||
{
|
||||
if (i != g_server && g_c[i].fd != -1 && FD_ISSET(i, &readfds))
|
||||
handle_read(i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user