![]() |
SocketThread.cppGo to the documentation of this file.00001 00006 /* 00007 Copyright (C) 2011 Anders Hedstrom 00008 00009 This library is made available under the terms of the GNU GPL. 00010 00011 If you would like to use this library in a closed-source application, 00012 a separate license agreement is available. For information about 00013 the closed-source license agreement for the C++ sockets library, 00014 please visit http://www.alhem.net/Sockets/license.html and/or 00015 email license@alhem.net. 00016 00017 This program is free software; you can redistribute it and/or 00018 modify it under the terms of the GNU General Public License 00019 as published by the Free Software Foundation; either version 2 00020 of the License, or (at your option) any later version. 00021 00022 This program is distributed in the hope that it will be useful, 00023 but WITHOUT ANY WARRANTY; without even the implied warranty of 00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00025 GNU General Public License for more details. 00026 00027 You should have received a copy of the GNU General Public License 00028 along with this program; if not, write to the Free Software 00029 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00030 */ 00031 #include "SocketThread.h" 00032 00033 #ifdef ENABLE_DETACH 00034 #include "Utility.h" 00035 00036 #ifdef SOCKETS_NAMESPACE 00037 namespace SOCKETS_NAMESPACE { 00038 #endif 00039 00040 SocketThread::SocketThread(Socket *p) 00041 :Thread(false) 00042 ,m_socket(p) 00043 { 00044 // Creator will release 00045 } 00046 00047 00048 SocketThread::~SocketThread() 00049 { 00050 if (IsRunning()) 00051 { 00052 SetRelease(true); 00053 SetRunning(false); 00054 m_h.Release(); 00055 Utility::Sleep(5); 00056 } 00057 } 00058 00059 00060 void SocketThread::Run() 00061 { 00062 m_h.SetSlave(); 00063 m_h.Add(m_socket); 00064 m_socket -> SetSlaveHandler(&m_h); 00065 m_socket -> OnDetached(); 00066 m_h.EnableRelease(); 00067 while (m_h.GetCount() > 1 && IsRunning()) 00068 { 00069 m_h.Select(0, 500000); 00070 } 00071 // m_socket now deleted oops 00072 // (a socket can only be detached if DeleteByHandler() is true) 00073 // yeah oops m_socket delete its socket thread, that means this 00074 // so Socket will no longer delete its socket thread, instead we do this: 00075 SetDeleteOnExit(); 00076 } 00077 00078 #ifdef SOCKETS_NAMESPACE 00079 } // namespace SOCKETS_NAMESPACE { 00080 #endif 00081 00082 #endif // ENABLE_DETACH |