Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 300932cf rédigé par Kubat's avatar Kubat
Parcourir les fichiers

adding a client executable, don't unlik the socket if the client is killed.

parent a8545b5c
Branches
Étiquettes
2 requêtes de fusion!6Resolve "Liste des commandes",!5Cmd Client
......@@ -4,13 +4,14 @@ LDFLAGS = `pkg-config --libs mpv` -pthread -ldl
EXE = bin/player.out
FILES = serv_main.c socket.c mpv.c thread_pool.c server.c database.c sqlite3.c
CLIENT = bin/client.out
FILES = socket.c mpv.c thread_pool.c server.c database.c sqlite3.c
OBJ = $(FILES:%.c=obj/%.o)
SRC = $(FILES:%.c=src/%.c)
all: $(EXE)
all: $(EXE) $(CLIENT)
style: ../astyle.sh
......@@ -24,11 +25,17 @@ obj/%.o: src/%.c
clean:
-@rm $(EXE)
-@rm $(OBJ)
-@rm $(CLIENT)
-@rm bin/*.out
-@rm obj/*.o
$(EXE): $(OBJ)
$(EXE): $(OBJ) obj/serv_main.o
$(CC) -o $@ $^ $(LDFLAGS)
$(CLIENT): $(OBJ) obj/client_main.o
$(CC) -o $@ obj/client_main.o obj/socket.o $(LDFLAGS)
.PHONY: clean style
......@@ -13,10 +13,12 @@ typedef struct socket socket_t;
/**
* @brief Create the socket server on unix domain sockets.
* @param is_client Set to `true` if the socket is a client socket, `false` otherwise
* (only for the server socket).
* @return The function returns the socket uppon successfull creation, `NULL` otherwise.
* @note Before closing the app don't forget to call `socket_destruct`.
*/
socket_t * socket_construct(void);
socket_t * socket_construct(bool is_client);
/**
......
#include <stdio.h>
#include <unistd.h>
#include "socket.h"
int main(int argc, char * argv[])
{
(void)argc;
(void)argv;
socket_t * sock = socket_construct(true);
sleep(10);
socket_destruct(sock);
return 0;
}
......@@ -21,6 +21,7 @@ static const char * const socket_file = "/tmp/lektor";
struct socket
{
bool is_client;
int fd;
struct sockaddr_un address;
socklen_t address_length;
......@@ -28,13 +29,14 @@ struct socket
};
socket_t * socket_construct(void)
socket_t * socket_construct(bool is_client)
{
struct socket * socket_ptr = (struct socket *) calloc(1, sizeof(struct socket));
if (NULL == socket_ptr)
{ return NULL; }
socket_ptr->is_client = is_client;
socket_ptr->fd = socket(PF_UNIX, SOCK_STREAM, 0);
socket_ptr->file = socket_file;
......@@ -44,7 +46,9 @@ socket_t * socket_construct(void)
goto error;
}
if (!is_client)
unlink(socket_ptr->file);
explicit_bzero(&socket_ptr->address, sizeof(struct sockaddr_un));
socket_ptr->address.sun_family = AF_UNIX;
memcpy(socket_ptr->address.sun_path, socket_ptr->file, UNIX_PATH_MAX);
......@@ -69,6 +73,9 @@ error:
if (NULL != socket_ptr)
{ free(socket_ptr); }
if (!is_client)
unlink(socket_ptr->file);
return NULL;
}
......@@ -93,6 +100,7 @@ void socket_destruct(socket_t * socket_ptr)
if (socket_ptr != NULL)
{
close(socket_ptr->fd);
if (!socket_ptr->is_client)
unlink(socket_ptr->file);
free(socket_ptr);
}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter