add circular buffer logic and fix makefile bug
This commit is contained in:
1
Makefile
1
Makefile
@@ -3,6 +3,7 @@ CFLAGS = -I./include -fPIC -Wall -Wextra -O2 -fvisibility=hidden
|
||||
LDFLAGS = -lssl -lcrypto
|
||||
|
||||
BUILD_DIR = build
|
||||
SRC_DIR = src
|
||||
|
||||
LIBRARY = libircc.so
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
#ifndef IRC_EVENTS_H
|
||||
#define IRC_EVENTS_H
|
||||
|
||||
#define QUEUE_FAILURE -1
|
||||
#define QUEUE_SUCCESS 0
|
||||
#define QUEUE_FULL 1
|
||||
#define QUEUE_EMPTY 2
|
||||
|
||||
typedef enum
|
||||
{
|
||||
IRC_EVENT_PRIVMSG,
|
||||
|
||||
@@ -41,9 +41,13 @@ PUBLIC void IRC_Close(irc_client_t *irc);
|
||||
// init
|
||||
PUBLIC int IRC_Init(irc_client_t *irc);
|
||||
PUBLIC void IRC_Free(irc_client_t *irc);
|
||||
int IRC_InitQueue(irc_client_t *irc, int size);
|
||||
|
||||
PUBLIC int IRC_PollEvent(irc_event_t *event, irc_client_t *irc);
|
||||
// PUBLIC int IRC_PollEvent(irc_event_t *event, irc_client_t *irc);
|
||||
|
||||
void IRC_PushEvent(irc_event_t *event, irc_client_t *irc);
|
||||
int IRC_Enqueue(irc_event_t *data, irc_client_t *irc);
|
||||
int IRC_Dequeue(irc_event_t *data, irc_client_t *irc);
|
||||
|
||||
int IRC_PeekQueue(irc_event_t *data, irc_client_t *irc);
|
||||
|
||||
#endif
|
||||
@@ -31,7 +31,7 @@ typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
irc_event_type_t type;
|
||||
irc_message_t orig_msg;
|
||||
char line[513];
|
||||
} irc_event_t;
|
||||
|
||||
typedef struct
|
||||
@@ -96,6 +96,14 @@ typedef struct
|
||||
irc_connection_type_t connType;
|
||||
} irc_connection_info_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
irc_event_t *events;
|
||||
int eventsSize;
|
||||
int front;
|
||||
int rear;
|
||||
} irc_event_queue_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
irc_connection_info_t connectionInfo;
|
||||
@@ -111,13 +119,13 @@ typedef struct
|
||||
/* Identity */
|
||||
irc_identity_t identity;
|
||||
|
||||
irc_event_t* eventQueue; // lives on the heap
|
||||
int eventQueuePtr;
|
||||
/* Event queues */
|
||||
irc_event_queue_t evtQueue;
|
||||
} irc_client_t;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
void (*handler)(irc_message_t *msg, irc_client_t *irc);
|
||||
} command_t;
|
||||
} irc_command_t;
|
||||
|
||||
#endif
|
||||
@@ -1,22 +1,65 @@
|
||||
#include "IRC/IRC.h"
|
||||
#include "IRC/IRC_events.h"
|
||||
#include "IRC/IRC_structs.h"
|
||||
#include "IRC/IRC_functions.h"
|
||||
|
||||
#define MAX_EVENT_QUEUE_SIZE 12
|
||||
|
||||
void IRC_PushEvent(irc_event_t *event, irc_client_t *irc)
|
||||
static int isQueueFull(irc_event_queue_t *queue)
|
||||
{
|
||||
// push the event to the queue
|
||||
if(irc->eventQueuePtr >= MAX_EVENT_QUEUE_SIZE - 1) return;
|
||||
irc->eventQueue[++irc->eventQueuePtr] = *event;
|
||||
return (queue->rear + 1) % queue->eventsSize == queue->front;
|
||||
}
|
||||
|
||||
int IRC_PollEvent(irc_event_t *event, irc_client_t *irc)
|
||||
static int isQueueEmpty(irc_event_queue_t *queue)
|
||||
{
|
||||
// pop events from the queue
|
||||
return queue->front == -1;
|
||||
}
|
||||
|
||||
if(irc->eventQueuePtr < 0)
|
||||
int IRC_Enqueue(irc_event_t *data, irc_client_t *irc)
|
||||
{
|
||||
if(isQueueFull(&irc->evtQueue))
|
||||
{
|
||||
return 0;
|
||||
return QUEUE_FULL;
|
||||
}
|
||||
*event = irc->eventQueue[irc->eventQueuePtr--];
|
||||
return 1;
|
||||
if (irc->evtQueue.front == -1) irc->evtQueue.front = 0;
|
||||
|
||||
irc->evtQueue.rear = (irc->evtQueue.rear + 1) % irc->evtQueue.eventsSize;
|
||||
irc->evtQueue.events[irc->evtQueue.rear] = *data;
|
||||
|
||||
return QUEUE_SUCCESS;
|
||||
}
|
||||
|
||||
int IRC_Dequeue(irc_event_t *data, irc_client_t *irc)
|
||||
{
|
||||
if(isQueueEmpty(&irc->evtQueue))
|
||||
{
|
||||
return QUEUE_EMPTY;
|
||||
}
|
||||
*data = irc->evtQueue.events[irc->evtQueue.front];
|
||||
if (irc->evtQueue.front == irc->evtQueue.rear)
|
||||
{
|
||||
irc->evtQueue.front = irc->evtQueue.rear = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
irc->evtQueue.front = (irc->evtQueue.front + 1) % irc->evtQueue.eventsSize;
|
||||
}
|
||||
return QUEUE_SUCCESS;
|
||||
}
|
||||
|
||||
int IRC_PeekQueue(irc_event_t *data, irc_client_t *irc)
|
||||
{
|
||||
if(isQueueEmpty(&irc->evtQueue))
|
||||
{
|
||||
return QUEUE_EMPTY;
|
||||
}
|
||||
*data = irc->evtQueue.events[irc->evtQueue.front];
|
||||
return QUEUE_SUCCESS;
|
||||
}
|
||||
|
||||
int IRC_InitQueue(irc_client_t *irc, int size)
|
||||
{
|
||||
irc->evtQueue.events = malloc(sizeof(irc_event_t) * size);
|
||||
if(!irc->evtQueue.events) return QUEUE_FAILURE;
|
||||
irc->evtQueue.eventsSize = size;
|
||||
irc->evtQueue.front = -1;
|
||||
irc->evtQueue.rear = -1;
|
||||
return QUEUE_SUCCESS;
|
||||
}
|
||||
@@ -6,16 +6,10 @@
|
||||
int IRC_Init(irc_client_t *irc)
|
||||
{
|
||||
// allocate the event queue space
|
||||
irc->eventQueue = (irc_event_t*)malloc(sizeof(irc_event_t) * MAX_EVENT_QUEUE_SIZE);
|
||||
if(irc->eventQueue == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
irc->eventQueuePtr = -1;
|
||||
return 0;
|
||||
return IRC_InitQueue(irc, MAX_EVENT_QUEUE_SIZE);
|
||||
}
|
||||
|
||||
void IRC_Free(irc_client_t *irc)
|
||||
{
|
||||
free(irc->eventQueue);
|
||||
free(irc->evtQueue.events);
|
||||
}
|
||||
@@ -120,7 +120,7 @@ static void handleSaslSuccess(irc_message_t *msg, irc_client_t *irc)
|
||||
*/
|
||||
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc)
|
||||
{
|
||||
command_t commands[] =
|
||||
irc_command_t commands[] =
|
||||
{
|
||||
{"PING", handlePing},
|
||||
{"CAP", handleCapabilities},
|
||||
|
||||
Reference in New Issue
Block a user