www.pudn.com > colortracker.rar > commthread.cpp
/* * commthread.cpp * * Copyright (c) 2003 Machine Perception Laboratory * University of California San Diego. * Please read the disclaimer and notes about redistribution * at the end of this file. * * Authors: Josh Susskind */ #ifdef WIN32 #pragma warning(disable:4786) #endif #include "commthread.h" #include "common.h" #include/* for NT threading */ using namespace std; /* Alternative to non-blocking or infinite mutex waits */ const int WAIT_EXTERNAL=16; /* ================================================================ */ CommThread::CommThread() : m_Available(false), m_Ini() { m_ThreadRunning = true; CreateMutex(m_DataMutex); /*#ifdef WIN32 m_DataMutex = CreateMutex (NULL, false, NULL); #else pthread_mutex_init(&m_DataMutex,NULL); #endif*/ if (MPConfigure::intval("socketio")) { WSAStartup(0x101,&ws); /* This is line is very important for initializing the socket */ char *client_ip = new char[255]; int port_num; GetClientIP(client_ip, port_num); m_SocketWriter = new MPSocketIO(client_ip, port_num); delete [] client_ip; m_SocketWriter->connectServer(); } m_ThreadHandle.mutex = (HANDLE) (_beginthread( CommLoop, 0, this )); SetThreadPriority(m_ThreadHandle.mutex, THREAD_PRIORITY_NORMAL-1); } /* ================================================================ */ void CommThread::CommLoop(void *ThisCommThread) { CommThread *CT = static_cast (ThisCommThread); while (CT->m_ThreadRunning) CT->Process(); _endthread(); } /* ================================================================ */ void CommThread::Process() { LockMutex(m_DataMutex); if (m_Available) { m_Available = false; if (MPConfigure::intval("socketio")) { if(m_SocketWriter->isConnect()) WriteData(m_SocketWriter); else { m_SocketWriter->disconnectServer(); m_SocketWriter->connectServer(); } } } ReleaseMutex( m_DataMutex); Sleep(WAIT_EXTERNAL); } /* ================================================================ */ void CommThread::PutData(int *vec, int n) { DWORD waitResult = TryLockMutex(m_DataMutex, WAIT_EXTERNAL ); switch( waitResult ) { case WAIT_OBJECT_0: /* The state of the specified object is signaled. */ if (!m_Available) { m_Data.clear(); for (int i = 0; i < n; ++i) m_Data.push_back(vec[i]); m_Available = true; } ReleaseMutex( m_DataMutex); break; case WAIT_TIMEOUT: /* The time-out interval elapsed, and the object's state is nonsignaled. */ case WAIT_ABANDONED: /* The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled. */ case WAIT_FAILED: default: break; } } /* ================================================================ */ void CommThread::WriteData(MPSocketIO *writer) { const char startSig = '+'; const char endSig = '-'; writer->write(startSig); for (vector ::iterator i = m_Data.begin(); i != m_Data.end(); ++i) writer->write(*i); writer->write(endSig); } /* ================================================================ */ void CommThread::GetClientIP(char*c_ip, int &c_p) { string ip = "127.0.0.1"; int port = 12000; ip = MPConfigure::stringval("ip"); memcpy(c_ip, ip.c_str(), sizeof(ip.c_str())*sizeof(string)); c_p = MPConfigure::intval("port"); } /* ================================================================ */ CommThread::~CommThread() { m_ThreadRunning = false; LockMutex(m_ThreadHandle); //WaitForSingleObject(m_ThreadHandle, INFINITE); if (MPConfigure::intval("socketio")) { m_SocketWriter->disconnectServer(); delete m_SocketWriter; } } /* ================================================================ */ /* * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */