Separate modes of tcp handlers
This commit is contained in:
124
src/kqueue_file_watch.cpp
Normal file
124
src/kqueue_file_watch.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
#include <sys/event.h>
|
||||
#include <cstdio>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
// #include <vector>
|
||||
// #include <map>
|
||||
#include <sys/param.h>
|
||||
// #include <sys/epoll.h>
|
||||
#include <sys/event.h>
|
||||
//
|
||||
//
|
||||
|
||||
typedef struct task_list_s {
|
||||
siginfo_t admin;
|
||||
pthread_t thread;
|
||||
pthread_mutex_t queue_cond_lock;
|
||||
// std::vector<MessageDef *> message_queue;
|
||||
// std::map<long,timer_elm_t> timer_map;
|
||||
uint64_t next_timer=UINT_MAX;
|
||||
|
||||
/** Replace these shits **/
|
||||
struct epoll_event *events =NULL;
|
||||
int nb_fd_epoll=0;
|
||||
int epoll_fd=-1;
|
||||
|
||||
/** kqueque equivalent **/
|
||||
|
||||
|
||||
int nb_events=0;
|
||||
int sem_fd=-1;
|
||||
} task_list_t;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
struct kevent event1;
|
||||
struct kevent event2;
|
||||
struct kevent tevent;
|
||||
struct kevent tevent2;
|
||||
|
||||
struct epoll_event *events =NULL;
|
||||
|
||||
static task_list_t **tasks=NULL;
|
||||
// printf("%s\n", task_list_t);
|
||||
|
||||
// General Pointers
|
||||
int kq, fd, ret1, ret2;
|
||||
|
||||
// File Pointers
|
||||
int fd1, fd2;
|
||||
|
||||
kq = kqueue();
|
||||
|
||||
printf("%d number of args\n", argc);
|
||||
|
||||
for (int i=1; i <= 2; i++){
|
||||
|
||||
printf("==> %s\n", argv[i]);
|
||||
if (i == 1)
|
||||
fd1 = open(argv[1], O_RDONLY);
|
||||
if (i == 2)
|
||||
fd2 = open(argv[2], O_RDONLY);
|
||||
}
|
||||
|
||||
// fd = open(argv[1], O_RDONLY);
|
||||
// for (int i=1; i <= 2; i++){
|
||||
// printf("YOUR FILES \n");
|
||||
// printf("==> %s\n", argv[i]);
|
||||
// }
|
||||
|
||||
|
||||
if (kq == -1){
|
||||
printf("Error kq...\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* initialzing kevent struct */
|
||||
EV_SET(
|
||||
&event1,
|
||||
fd1,
|
||||
EVFILT_VNODE,
|
||||
EV_ADD
|
||||
|
|
||||
EV_CLEAR,
|
||||
NOTE_WRITE,
|
||||
0,
|
||||
NULL
|
||||
);
|
||||
|
||||
ret1 = kevent(kq, &event1, 1, NULL, 0, NULL);
|
||||
|
||||
EV_SET(
|
||||
&event2,
|
||||
fd2,
|
||||
EVFILT_VNODE,
|
||||
EV_ADD
|
||||
|
|
||||
EV_CLEAR,
|
||||
NOTE_WRITE,
|
||||
0,
|
||||
NULL
|
||||
);
|
||||
|
||||
ret2 = kevent(kq, &event2, 1, NULL, 0, NULL);
|
||||
|
||||
if ( ret1 == -1 || ret2 == -1 ){
|
||||
printf("event failure to attched");
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
ret1 = kevent(kq, NULL, 0, &tevent, 1, NULL);
|
||||
ret2 = kevent(kq, NULL, 0, &tevent, 1, NULL);
|
||||
if (ret1 == -1 || ret2 == -1) {
|
||||
printf("Waiting.......\n");
|
||||
} else if ( ret1 > 0 || ret2 > 0 ){
|
||||
printf("Change detected on file...\n");
|
||||
printf("--> RET 1 VAL = %d\n", ret1);
|
||||
printf("--> RET 2 VAL = %d\n", ret2);
|
||||
}
|
||||
// printf("--> RET 1 VAL = %d\n", ret1);
|
||||
// printf("--> RET 2 VAL = %d\n", ret2);
|
||||
}
|
||||
}
|
||||
|
||||
86
src/kqueue_ktimer.c
Normal file
86
src/kqueue_ktimer.c
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <sys/event.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct inf_kevent {
|
||||
uintptr_t ident;
|
||||
short filter;
|
||||
u_short flags;
|
||||
u_int fflags;
|
||||
int64_t data;
|
||||
void *udata;
|
||||
u_int64_t ext[4];
|
||||
};
|
||||
|
||||
struct addrinfo *add;
|
||||
struct addrinfo hints;
|
||||
|
||||
void inf_info(const char *s);
|
||||
|
||||
int main(void) {
|
||||
struct kevent event;
|
||||
struct kevent change;
|
||||
pid_t pid;
|
||||
int kq, nev;
|
||||
|
||||
printf("\n");
|
||||
printf("---------------------------------------\n");
|
||||
printf("Stupid struct\n");
|
||||
printf("---------------------------------------\n");
|
||||
printf("\n");
|
||||
|
||||
|
||||
if ((kq = kqueue()) == -1){
|
||||
inf_info("kqueue()");
|
||||
}
|
||||
|
||||
EV_SET(&change, 1, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, 2000, 0);
|
||||
|
||||
for (;;) {
|
||||
|
||||
nev = kevent(kq, &change, 1, &event, 1, NULL);
|
||||
|
||||
printf("--> kq %d\n", kq);
|
||||
printf("--> pid %d\n", pid);
|
||||
|
||||
if (nev < 0){
|
||||
inf_info("kevent()");
|
||||
}
|
||||
|
||||
else if (nev > 0) {
|
||||
|
||||
if (event.flags & EV_ERROR) {
|
||||
fprintf(stderr, "EV_ERROR...");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if ((pid = fork()) < 0 ) {
|
||||
fprintf(stderr, "EV_ERROR...");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
else if (pid == 0) {
|
||||
if (execlp("date", "date", (char *)0) < 0)
|
||||
inf_info("execplp()");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
close(kq);
|
||||
}
|
||||
|
||||
void inf_info(const char *s)
|
||||
{
|
||||
perror(s);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
106
src/ktcp_socket_01.c
Normal file
106
src/ktcp_socket_01.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/event.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main() {
|
||||
int socket_listen_fd,
|
||||
port_num = 1339,
|
||||
client_len,
|
||||
socket_connection_fd,
|
||||
kq,
|
||||
new_events;
|
||||
|
||||
struct kevent change_event[4], event[4];
|
||||
struct kevent evList[32];
|
||||
|
||||
struct sockaddr_in serv_addr, client_addr;
|
||||
|
||||
if (((socket_listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0))
|
||||
{
|
||||
perror("Failed creating socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
bzero((char *)&serv_addr, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
serv_addr.sin_port = htons(port_num);
|
||||
|
||||
if (bind(socket_listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
|
||||
{
|
||||
perror("Failed binding socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
listen(socket_listen_fd, 3);
|
||||
client_len = sizeof(client_addr);
|
||||
|
||||
// kqueue SETUP
|
||||
|
||||
kq = kqueue();
|
||||
|
||||
EV_SET(change_event, socket_listen_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
|
||||
if (kevent(kq, change_event, 1, NULL, 0, NULL) == -1)
|
||||
{
|
||||
perror("Failed create kevent");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
|
||||
new_events = kevent(kq, NULL, 0, event, 1, NULL);
|
||||
|
||||
// printf("New events -> %d\n", new_events);
|
||||
|
||||
if (new_events == -1)
|
||||
{
|
||||
perror("Failed create new kevent");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (int i = 0; new_events > i; i++)
|
||||
{
|
||||
printf("Amount of new events : %d\n", new_events);
|
||||
int event_fd = event[i].ident;
|
||||
|
||||
if (event[i].flags & EV_EOF)
|
||||
{
|
||||
printf("Client disconnected...\n");
|
||||
close(event_fd);
|
||||
}
|
||||
|
||||
else if (event_fd == socket_listen_fd)
|
||||
{
|
||||
printf("New connection coming in...\n");
|
||||
|
||||
socket_connection_fd = accept(event_fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_len);
|
||||
if (socket_connection_fd == -1)
|
||||
{
|
||||
perror("Failed to accept connection...");
|
||||
}
|
||||
|
||||
EV_SET(change_event, socket_connection_fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
|
||||
if (kevent(kq, change_event, 1, NULL, 0, NULL) < 0)
|
||||
{
|
||||
perror("Kevent failed to maintain connection");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else if (event[i].filter == EVFILT_READ)
|
||||
{
|
||||
char buf[1024];
|
||||
size_t bytes_read = recv(event_fd, buf, sizeof(buf), 0);
|
||||
printf("Reading %zu bytes\n", bytes_read);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
116
src/ktcp_socket_02.c
Normal file
116
src/ktcp_socket_02.c
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/event.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main() {
|
||||
int socket_listen_fd,
|
||||
port_num = 2339,
|
||||
client_len,
|
||||
socket_connection_fd,
|
||||
kq,
|
||||
new_events;
|
||||
|
||||
struct kevent change_event, event;
|
||||
struct kevent evList[32];
|
||||
|
||||
struct sockaddr_in serv_addr, client_addr;
|
||||
|
||||
if (((socket_listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0))
|
||||
{
|
||||
perror("Failed creating socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
bzero((char *)&serv_addr, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
serv_addr.sin_port = htons(port_num);
|
||||
|
||||
if (bind(socket_listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
|
||||
{ perror("Failed binding socket"); exit(1);
|
||||
}
|
||||
|
||||
listen(socket_listen_fd, 3);
|
||||
client_len = sizeof(client_addr);
|
||||
|
||||
// kqueue SETUP
|
||||
|
||||
kq = kqueue();
|
||||
|
||||
EV_SET(&change_event, socket_listen_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
|
||||
if (kevent(kq, &change_event, 1, NULL, 0, NULL) == -1)
|
||||
{
|
||||
perror("Failed create kevent");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
|
||||
new_events = kevent(kq, NULL, 0, evList, 1, NULL);
|
||||
|
||||
// printf("New events -> %d\n", new_events);
|
||||
|
||||
if (new_events == -1)
|
||||
{
|
||||
perror("Failed create new kevent");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (int i = 0; new_events > i; i++)
|
||||
{
|
||||
printf("Amount of new events : %d\n", new_events);
|
||||
int event_fd = evList[i].ident;
|
||||
|
||||
if (evList[i].flags & EV_EOF)
|
||||
{
|
||||
printf("Client disconnected...\n");
|
||||
close(event_fd);
|
||||
}
|
||||
|
||||
else if (event_fd == socket_listen_fd)
|
||||
{
|
||||
printf("New connection coming in...\n");
|
||||
|
||||
socket_connection_fd = accept(event_fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_len);
|
||||
if (socket_connection_fd == -1)
|
||||
{
|
||||
perror("Failed to accept connection...");
|
||||
}
|
||||
|
||||
EV_SET(&change_event, socket_connection_fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
|
||||
if (kevent(kq, &change_event, 1, NULL, 0, NULL) < 0)
|
||||
{
|
||||
perror("Kevent failed to maintain connection");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else if (evList[i].filter == EVFILT_READ)
|
||||
{
|
||||
char buf[1024];
|
||||
size_t bytes_read = recv(event_fd, buf, sizeof(buf), 0);
|
||||
printf("--------------------------------------------------\n");
|
||||
printf("Reading %zu bytes\n", bytes_read);
|
||||
printf("Incoming msg : %s", buf);
|
||||
printf("--------------------------------------------------\n");
|
||||
|
||||
char s_buf[1024];
|
||||
int len;
|
||||
char *message = "[+] Hello from kTCP\n";
|
||||
|
||||
strcpy(s_buf, message);
|
||||
|
||||
send(event_fd, s_buf, strlen(s_buf), 0);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
125
src/wtf.c
Normal file
125
src/wtf.c
Normal file
@@ -0,0 +1,125 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/event.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// All needed variables.
|
||||
int socket_listen_fd,
|
||||
portno = 1815,
|
||||
client_len,
|
||||
socket_connection_fd,
|
||||
kq,
|
||||
new_events;
|
||||
struct kevent change_event[4],
|
||||
event[4];
|
||||
struct sockaddr_in serv_addr,
|
||||
client_addr;
|
||||
|
||||
// Create socket.
|
||||
if (((socket_listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0))
|
||||
{
|
||||
perror("ERROR opening socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Create socket structure and bind to ip address.
|
||||
bzero((char *)&serv_addr, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
serv_addr.sin_port = htons(portno);
|
||||
|
||||
if (bind(socket_listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
|
||||
{
|
||||
perror("Error binding socket");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Start listening.
|
||||
listen(socket_listen_fd, 3);
|
||||
client_len = sizeof(client_addr);
|
||||
|
||||
// Prepare the kqueue.
|
||||
kq = kqueue();
|
||||
|
||||
// Create event 'filter', these are the events we want to monitor.
|
||||
// Here we want to monitor: socket_listen_fd, for the events: EVFILT_READ
|
||||
// (when there is data to be read on the socket), and perform the following
|
||||
// actions on this kevent: EV_ADD and EV_ENABLE (add the event to the kqueue
|
||||
// and enable it).
|
||||
EV_SET(change_event, socket_listen_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
|
||||
|
||||
// Register kevent with the kqueue.
|
||||
if (kevent(kq, change_event, 1, NULL, 0, NULL) == -1)
|
||||
{
|
||||
perror("kevent");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Actual event loop.
|
||||
for (;;)
|
||||
{
|
||||
// Check for new events, but do not register new events with
|
||||
// the kqueue. Hence the 2nd and 3rd arguments are NULL, 0.
|
||||
// Only handle 1 new event per iteration in the loop; 5th
|
||||
// argument is 1.
|
||||
new_events = kevent(kq, NULL, 0, event, 1, NULL);
|
||||
if (new_events == -1)
|
||||
{
|
||||
perror("kevent");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (int i = 0; new_events > i; i++)
|
||||
{
|
||||
printf("amount of new events: %d\n", new_events);
|
||||
int event_fd = event[i].ident;
|
||||
|
||||
// When the client disconnects an EOF is sent. By closing the file
|
||||
// descriptor the event is automatically removed from the kqueue.
|
||||
if (event[i].flags & EV_EOF)
|
||||
{
|
||||
printf("Client has disconnected\n");
|
||||
close(event_fd);
|
||||
}
|
||||
// If the new event's file descriptor is the same as the listening
|
||||
// socket's file descriptor, we are sure that a new client wants
|
||||
// to connect to our socket.
|
||||
else if (event_fd == socket_listen_fd)
|
||||
{
|
||||
printf("New connection coming in...\n");
|
||||
|
||||
// Incoming socket connection on the listening socket.
|
||||
// Create a new socket for the actual connection to client.
|
||||
socket_connection_fd = accept(event_fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_len);
|
||||
if (socket_connection_fd == -1)
|
||||
{
|
||||
perror("Accept socket error");
|
||||
}
|
||||
|
||||
// Put this new socket connection also as a 'filter' event
|
||||
// to watch in kqueue, so we can now watch for events on this
|
||||
// new socket.
|
||||
EV_SET(change_event, socket_connection_fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
|
||||
if (kevent(kq, change_event, 1, NULL, 0, NULL) < 0)
|
||||
{
|
||||
perror("kevent error");
|
||||
}
|
||||
}
|
||||
|
||||
else if (event[i].filter & EVFILT_READ)
|
||||
{
|
||||
// Read bytes from socket
|
||||
char buf[1024];
|
||||
size_t bytes_read = recv(event_fd, buf, sizeof(buf), 0);
|
||||
printf("read %zu bytes\n", bytes_read);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user