src/event.c

00001  /*
00002   The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack.
00003   Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the Free Software
00017   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 #include "ortp/event.h"
00021 #include "ortp/ortp.h"
00022 
00023 RtpEndpoint *rtp_endpoint_new(struct sockaddr *addr, socklen_t addrlen){
00024         RtpEndpoint *ep=ortp_new(RtpEndpoint,1);
00025         if (sizeof(ep->addr)<addrlen){
00026                 ortp_free(ep);
00027                 ortp_fatal("Bad socklen_t size !");
00028                 return NULL;
00029         }
00030         memcpy(&ep->addr,addr,addrlen);
00031         ep->addrlen=addrlen;
00032         return ep;
00033 }
00034 
00035 void rtp_endpoint_destroy(RtpEndpoint *ep){
00036         ortp_free(ep);
00037 }
00038 
00039 RtpEndpoint *rtp_endpoint_dup(const RtpEndpoint *ep){
00040         return rtp_endpoint_new((struct sockaddr*)&ep->addr,ep->addrlen);
00041 }
00042 
00043 OrtpEvent * ortp_event_new(unsigned long type){
00044         const int size=sizeof(OrtpEventType)+sizeof(OrtpEventData);
00045         mblk_t *m=allocb(size,0);
00046         memset(m->b_wptr,0,size);
00047         *((OrtpEventType*)m->b_wptr)=type;
00048         return m;
00049 }
00050 
00051 OrtpEvent *ortp_event_dup(OrtpEvent *ev){
00052 #if 0
00053         OrtpEvent *nev=dupb(ev);
00054 #else
00055         OrtpEvent *nev = ortp_event_new(ortp_event_get_type(ev));
00056         OrtpEventData * ed = ortp_event_get_data(ev);
00057         OrtpEventData * edv = ortp_event_get_data(nev);
00058 
00059         if (ed->ep) edv->ep = rtp_endpoint_dup(ed->ep);
00060         if (ed->packet) edv->packet = copymsg(ed->packet);
00061         edv->info.telephone_event = ed->info.telephone_event;
00062 #endif
00063         return nev;
00064 }
00065 
00066 OrtpEventType ortp_event_get_type(const OrtpEvent *ev){
00067         return ((OrtpEventType*)ev->b_rptr)[0];
00068 }
00069 
00070 OrtpEventData * ortp_event_get_data(OrtpEvent *ev){
00071         return (OrtpEventData*)(ev->b_rptr+sizeof(OrtpEventType));
00072 }
00073 
00074 void ortp_event_destroy(OrtpEvent *ev){
00075         OrtpEventData *d=ortp_event_get_data(ev);
00076         if (ev->b_datap->db_ref==1){
00077                 if (d->packet)  freemsg(d->packet);
00078                 if (d->ep) rtp_endpoint_destroy(d->ep);
00079         }
00080         freemsg(ev);
00081 }
00082 
00083 OrtpEvQueue * ortp_ev_queue_new(){
00084         OrtpEvQueue *q=ortp_new(OrtpEvQueue,1);
00085         qinit(&q->q);
00086         ortp_mutex_init(&q->mutex,NULL);
00087         return q;
00088 }
00089 
00090 void ortp_ev_queue_flush(OrtpEvQueue * qp){
00091         OrtpEvent *ev;
00092         while((ev=ortp_ev_queue_get(qp))!=NULL){
00093                 ortp_event_destroy(ev);
00094         }
00095 }
00096 
00097 OrtpEvent * ortp_ev_queue_get(OrtpEvQueue *q){
00098         OrtpEvent *ev;
00099         ortp_mutex_lock(&q->mutex);
00100         ev=getq(&q->q);
00101         ortp_mutex_unlock(&q->mutex);
00102         return ev;
00103 }
00104 
00105 void ortp_ev_queue_destroy(OrtpEvQueue * qp){
00106         ortp_ev_queue_flush(qp);
00107         ortp_mutex_destroy(&qp->mutex);
00108         ortp_free(qp);
00109 }
00110 
00111 void ortp_ev_queue_put(OrtpEvQueue *q, OrtpEvent *ev){
00112         ortp_mutex_lock(&q->mutex);
00113         putq(&q->q,ev);
00114         ortp_mutex_unlock(&q->mutex);
00115 }
00116 

Generated on Wed Aug 29 01:39:22 2007 for oRTP by  doxygen 1.5.2