From 7ee402d2ae7589fe21f927cb1b8f2ffb3ef74662 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 25 Jan 2013 07:19:44 -0800 Subject: [PATCH] move callback into queueelem so same queue can service udp and tcp (eventually) --- xwords4/relay/udpqueue.cpp | 12 ++---------- xwords4/relay/udpqueue.h | 28 +++++++++++++++++----------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/xwords4/relay/udpqueue.cpp b/xwords4/relay/udpqueue.cpp index 0f3c03cb1..e22372add 100644 --- a/xwords4/relay/udpqueue.cpp +++ b/xwords4/relay/udpqueue.cpp @@ -37,7 +37,6 @@ UdpThreadClosure::logStats() } UdpQueue::UdpQueue() - :m_cb(NULL) { pthread_mutex_init ( &m_queueMutex, NULL ); pthread_cond_init( &m_queueCondVar, NULL ); @@ -68,9 +67,8 @@ void UdpQueue::handle( const AddrInfo::AddrUnion* saddr, unsigned char* buf, int len, QueueCallback cb ) { - UdpThreadClosure* utc = new UdpThreadClosure( saddr, buf, len ); + UdpThreadClosure* utc = new UdpThreadClosure( saddr, buf, len, cb ); MutexLock ml( &m_queueMutex ); - setCB( cb ); m_queue.push_back( utc ); pthread_cond_signal( &m_queueCondVar ); } @@ -88,7 +86,7 @@ UdpQueue::thread_main() pthread_mutex_unlock( &m_queueMutex ); utc->noteDequeued(); - (*m_cb)( utc ); + (*utc->cb())( utc ); utc->logStats(); delete utc; } @@ -104,9 +102,3 @@ UdpQueue::thread_main_static( void* closure ) return me->thread_main(); } -void -UdpQueue::setCB( QueueCallback cb ) -{ - assert( cb == m_cb || !m_cb ); - m_cb = cb; -} diff --git a/xwords4/relay/udpqueue.h b/xwords4/relay/udpqueue.h index 04f05f956..61752b5de 100644 --- a/xwords4/relay/udpqueue.h +++ b/xwords4/relay/udpqueue.h @@ -29,15 +29,23 @@ using namespace std; +class UdpThreadClosure; + +typedef void (*QueueCallback)( UdpThreadClosure* closure ); + class UdpThreadClosure { public: - UdpThreadClosure( const AddrInfo::AddrUnion* saddr, unsigned char* buf, int len ) { - m_saddr = *saddr; - m_buf = new unsigned char[len]; - memcpy( m_buf, buf, len ); - m_len = len; - m_created = time( NULL ); - } + UdpThreadClosure( const AddrInfo::AddrUnion* saddr, unsigned char* buf, + int len, QueueCallback cb ) + : m_buf(new unsigned char[len]) + , m_len(len) + , m_saddr(*saddr) + , m_cb(cb) + , m_created(time( NULL )) + { + memcpy( m_buf, buf, len ); + } + ~UdpThreadClosure() { delete m_buf; } const unsigned char* buf() const { return m_buf; } @@ -45,17 +53,17 @@ public: const AddrInfo::AddrUnion* saddr() const { return &m_saddr; } void noteDequeued() { m_dequed = time( NULL ); } void logStats(); + const QueueCallback cb() const { return m_cb; } private: unsigned char* m_buf; int m_len; AddrInfo::AddrUnion m_saddr; + QueueCallback m_cb; time_t m_created; time_t m_dequed; }; -typedef void (*QueueCallback)( UdpThreadClosure* closure ); - class UdpQueue { public: static UdpQueue* get(); @@ -67,13 +75,11 @@ class UdpQueue { private: static void* thread_main_static( void* closure ); void* thread_main(); - void setCB( QueueCallback cb ); pthread_mutex_t m_queueMutex; pthread_cond_t m_queueCondVar; deque m_queue; - QueueCallback m_cb; }; #endif