add sockets

This commit is contained in:
2024-12-24 23:15:44 +01:00
parent 494b835d11
commit bdaf894aea
17 changed files with 354 additions and 7 deletions
+76
View File
@@ -0,0 +1,76 @@
# lib: -lysocket
PROJECT_LIB=libysocket.so
CC=gcc
INCLUDE_DIRS=$(PWD)
SOCDIR=$(PWD)
#$(wildcard $(PWD)/**/include)
INCLUDE=-I$(PWD)/include
CFLAGS=-g -lpthread -Wall -Werror -fpic $(INCLUDE) #"-D DEBUG=1"
#LDFLAGS=
TOPTARGETS := all clean #update_headers
#SRC=$(wildcard y*/src/**/**/*.c)
SRC=$(wildcard src/*/*.c)
OBJ=$(SRC:.c=.o)
#SUBDIRS :=$(wildcard y*) $(TOOLDIR) $(BARPDIR)
#export
#$(TOPTARGETS): $(SUBDIRS)
all: $(PROJECT_LIB)
#update_headers
$(PROJECT_LIB): $(OBJ)
echo $(OBJ)
#$(CC) -shared -o $@ $^ $(INCLUDE) $(LDFLAGS)
#$(CC) -shared -o $@ $^ $(LDFLAGS)
$(CC) -shared -o $@ $^ $(CFLAGS)
$(SUBDIRS):
$(MAKE) -C $@ $(MAKECMDGOALS)
# .PHONY: $(TOPTARGETS) $(SUBDIRS)
.PHONY: clean
clean:
rm -f $(OBJ)
mrproper: clean
rm -f $(PROJECT_LIB)
install:
cp libytest.so /usr/lib/
@if [ -d /usr/local/include ] ; then\
echo "copy include to /usr/local/include/" ;\
cp -r include/* /usr/local/include/;\
else\
echo "copy include to /usr/include/" ;\
cp -r include/* /usr/include/;\
fi
uninstall:
rm /usr/lib/libytest.so
@if [ -d /usr/local/include ] ; then\
echo "remove from /usr/local/include/" ;\
rm -r /usr/local/include/y_socket ;\
else\
echo "remove from /usr/include/" ;\
rm -r /usr/include/y_socket ;\
fi
#SRC_test=test/is_good.c
#compile: $(SRC_test) $(PROJECT_LIB)
# $(CC) -o launch_is_good_m $< -L. test/src/permutation_t/permutation_t.o test/src/set_theoric_t/set_theoric_t.o -lytest -I./test/src -I./include_ytest
+24
View File
@@ -0,0 +1,24 @@
#ifndef Y_CLIENT_H__C
#define Y_CLIENT_H__C
#include <pthread.h>
#include "y_socket/y_socket.h"
struct y_client{
size_t id;
struct y_socket_t *socket;
int nb_threads;
pthread_t *thread_client;
pthread_mutex_t *mut_client;
void *(*launch)(void*);
};
#endif /* Y_CLIENT_H__C */
+8
View File
@@ -0,0 +1,8 @@
#ifndef __Y_QUEUE_T_H__
#define __Y_QUEUE_T_H__
#include "y_socket/y_threads.h"
#endif /*__Y_QUEUE_T_H__*/
+29
View File
@@ -0,0 +1,29 @@
#ifndef Y_SERVER_H__C
#define Y_SERVER_H__C
#include <pthread.h>
#include "y_socket/y_socket.h"
struct y_server_t{
struct y_socket_t *socket;
// struct y_client_t *client;
// int nb_clients;
// int max_length_queue; //backlog;// max connexion
pthread_t *thread_server;
pthread_mutex_t *mut_server;
int nb_threads;
void *(*launch)(void*);
};
#endif /* Y_SERVER_H__C */
+42
View File
@@ -0,0 +1,42 @@
#ifndef Y_SOCKET_H__C
#define Y_SOCKET_H__C
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
struct y_socket_t{
int fd; /* Socket descriptor, */
int domain; /*AF_INET, AF_INET6 */
int service; /*type: SOCK_STREAM: TCP, SOCK_DGRAM: UDP */
int protocol; /* 0 */
u_long interface;
int port;
int backlog;/*max_queue */
struct sockaddr_in *address; /* INADDR_ANY or */
int address_len;
};
struct y_socket_t * socket_create(int domain, int service, int protocol, u_long interface,
int port, int backlog);
void socket_destroy(struct y_socket_t *socket);
#endif /* Y_SOCKET_H__C */
+18
View File
@@ -0,0 +1,18 @@
#ifndef __Y_THREAD_H__
#define __Y_THREAD_H__
#include <pthread.h>
#include "y_socket/y_queue.h"
struct y_threadpool{
int nb_threads;
pthread_cond_t cond_;
pthread_mutex_t mut_;
pthread_t *thread;
void *(*launch)(void* arg);
};
#endif /*__Y_THREAD_H__*/
+1
View File
@@ -0,0 +1 @@
#include "y_socket/y_client.h"
+1
View File
@@ -0,0 +1 @@
#include "y_socket/y_queue.h"
+2
View File
@@ -0,0 +1,2 @@
#include "y_socket/y_server.h"
+34
View File
@@ -0,0 +1,34 @@
#include "y_socket/y_socket.h"
#if 0
struct y_server_t server;
void close_server(struct y_server_t server){
char *message ="HTTP/1.1 201 OK\r\n"
"Connection: close";
for(int i=0; i<server.nb_clients;++i){
pthread_mutex_lock(server.client[i].mut_client);
write(server.client[i].id, message, strlen(message));
pthread_mutex_unlock(server.client[i].mut_client);
close(server.client[i].id);
}
close(server.sock);
}
void func_sig_Handler(int sig){
if(sig==SIGINT){
close_server(server);
sigaction(SIGINT, &old, NULL); /* equivalent à .sa_falg = SA_RESETHAND , i.e. 1 seul new_action, puis on revient à l'ancien*/
exit(0);
}
}
#endif
+3
View File
@@ -0,0 +1,3 @@
#include "y_socket/y_threads.h"
+69
View File
@@ -0,0 +1,69 @@
NAME_TEST=is_good
CC=gcc
ROOT_DIR=$(PWD)
YTESTDIR=$(PWD)/../../ytest_t
SOCDIR=$(PWD)/..
INCLUDE_DIR=$(PWD)/../include
CFLAGS=-I$(INCLUDE_DIR) #"-D DEBUG=1"
LDFLAGS=-L$(YTESTDIR) -lytest -lOpenCL
#SRC_DIR=$(ROOT_DIR)/src
#SRC=$(wildcard */*/*.c)
SRC=$(wildcard **/**/*.c)
#HEADS=$(OBJS:.o=.h)
TEST_DIR=$(PWD)
EXECSRC=$(NAME_TEST).c
#EXECSRC=openF.c
EXEC=launch_$(NAME_TEST)_m
SOCRC=$(SOCDIR)/src/y_socket/y_socket.c
SOCSRC=$(wildcard ../src/*/*.c)
SOCSRC_O=$(SOCSRC:.c=.o)
TOPTARGETS := all clean
DEPS=$(YTESTDIR) $(SOCDIR)
OBJ=$(SOCSRC_O)
LIB_YTEST=$(YTESTDIR)/libytest.so
$(TOPTARGETS): $(DEPS)
$(DEPS):
$(MAKE) -C $@ $(MAKECMDGOALS)
#PERMSRC_O=$(PERMSRC:.c=.o)
#SETTSRC_O=$(PWD)/../src/set_theoric_t/set_theoric_t.o
#SETTSRC_O=$(SETTSRC:.c=.o)
#TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c
#TOOLSRC_O=$(TOOLSRC:.c=.o)
all: $(EXEC) $(LIB_YTEST)
$(EXEC): $(EXECSRC) $(OBJ)
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
.PHONY: clean mrproper
clean:
rm -f $(OBJ)
mrproper: clean
rm -f $(EXEC)
run: $(EXEC)
$(EXEC) -h
+35
View File
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// for sleep !
#ifdef __linux__
#include <unistd.h>
#elif _WIN32
#include <windows.h>
#endif
#include "ftest/ftest.h"
#include "ftest/ftest_array.h"
#include "fmock/fmock.h"
//#include "permutation_t/permutation_t.h"
#include "y_socket/y_socket.h"
#define VALGRIND_ 1
TEST(first){
LOG("hey%s\n"," you");
}
int main(int argc, char **argv){
run_all_tests_args(argc, argv);
return 0;
}