X-Git-Url: http://git.eversberg.eu/gitweb.cgi?p=lcr.git;a=blobdiff_plain;f=message.c;h=ff0d70c9505e94d16a06a544e1a25a5e12bcf523;hp=97c032cbbaca6c4f9c4edbed225862bb0c90282a;hb=a425aedc1ee2c0bba4ba20904943afb21bd6e2e5;hpb=2ed0fee489c37a6e2d4473f6185ebbe3e746ac11 diff --git a/message.c b/message.c index 97c032c..ff0d70c 100644 --- a/message.c +++ b/message.c @@ -1,6 +1,6 @@ /*****************************************************************************\ ** ** -** PBX4Linux ** +** Linux Call Route ** ** ** **---------------------------------------------------------------------------** ** Copyright: Andreas Eversberg ** @@ -9,46 +9,23 @@ ** ** \*****************************************************************************/ -#include -#include -#include #include "main.h" MESSAGES -struct message *message_first = NULL; -struct message **messagepointer_end = &message_first; - -//#ifdef H323 -//PMutex mutex_message; -//#endif +struct lcr_msg *message_first = NULL; +struct lcr_msg **messagepointer_end = &message_first; /* creates a new message with the given attributes. the message must be filled then. after filling, the message_put must be called */ -struct message *message_create(int id_from, int id_to, int flow, int type) +struct lcr_msg *message_create(int id_from, int id_to, int flow, int type) { - struct message *message; - int i = 0; + struct lcr_msg *message; - while(i < 10) - { - message = (struct message *)calloc(1, sizeof(struct message)); - if (message) - break; - - if (!i) - PERROR("no mem for message, retrying...\n"); - i++; - usleep(300000); - } + message = (struct lcr_msg *)MALLOC(sizeof(struct lcr_msg)); if (!message) - { - PERROR("***Fatal error: no mem for message!!! exitting.\n"); - exit(-1); - } + FATAL("No memory for message.\n"); mmemuse++; - memset(message, 0, sizeof(struct message)); - message->id_from = id_from; message->id_to = id_to; message->flow = flow; @@ -58,13 +35,8 @@ struct message *message_create(int id_from, int id_to, int flow, int type) } /* attaches a message to the end of the message chain */ -void message_put(struct message *message) +void message_put(struct lcr_msg *message) { - /* the mutex prevents from creating two messages at a time (h323 thread and main thread). */ -//#ifdef H323 -// mutex_message.Wait(); -//#endif - if (message->id_to == 0) { PDEBUG(DEBUG_MSG, "message %s not written, because destination is 0.\n", messages_txt[message->type]); @@ -77,28 +49,35 @@ void message_put(struct message *message) *messagepointer_end = message; messagepointer_end = &(message->next); - -//#ifdef H323 -// mutex_message.Signal(); -//#endif + /* Nullify next pointer if recycled messages */ + *messagepointer_end=NULL; } +struct lcr_msg *message_forward(int id_from, int id_to, int flow, union parameter *param) +{ + struct lcr_msg *message; + + /* get point to message */ + message = (struct lcr_msg *)((unsigned long)param - ((unsigned long)(&message->param) - (unsigned long)message)); + + /* protect, so forwarded messages are not freed after handling */ + message->keep = 1; + + message->id_from = id_from; + message->id_to = id_to; + message->flow = flow; + message_put(message); + + return(message); +} /* detaches the first messages from the message chain */ -struct message *message_get(void) +struct lcr_msg *message_get(void) { - struct message *message; - - /* the mutex prevents from getting a message while creating a messages at a time (h323 thread and main thread). */ -//#ifdef H323 -// mutex_message.Wait(); -//#endif + struct lcr_msg *message; if (!message_first) { -//#ifdef H323 -// mutex_message.Signal(); -//#endif return(0); } @@ -107,21 +86,21 @@ struct message *message_get(void) if (!message_first) messagepointer_end = &message_first; -//#ifdef H323 -// mutex_message.Signal(); -//#endif + message->keep = 0; if ((options.deb&DEBUG_MSG) && message->type != MESSAGE_DATA) + PDEBUG(DEBUG_MSG, "message %s reading from %ld to %ld (memory %x)\n", messages_txt[message->type], message->id_from, message->id_to, message); return(message); } /* free a message */ -void message_free(struct message *message) +void message_free(struct lcr_msg *message) { - memset(message, 0, sizeof(struct message)); - free(message); + if (message->keep) + return; + FREE(message, sizeof(struct lcr_msg)); mmemuse--; }