| 39 | |
| 40 | |
| 41 | bool cSocketThreads::AddClient(const cSocket & a_Socket, cCallback * a_Client) |
| 42 | { |
| 43 | // Add a (socket, client) pair for processing, data from a_Socket is to be sent to a_Client |
| 44 | |
| 45 | // Try to add to existing threads: |
| 46 | cCSLock Lock(m_CS); |
| 47 | for (cSocketThreadList::iterator itr = m_Threads.begin(); itr != m_Threads.end(); ++itr) |
| 48 | { |
| 49 | if ((*itr)->IsValid() && (*itr)->HasEmptySlot()) |
| 50 | { |
| 51 | (*itr)->AddClient(a_Socket, a_Client); |
| 52 | return true; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // No thread has free space, create a new one: |
| 57 | LOGD("Creating a new cSocketThread (currently have " SIZE_T_FMT ")", m_Threads.size()); |
| 58 | cSocketThread * Thread = new cSocketThread(this); |
| 59 | if (!Thread->Start()) |
| 60 | { |
| 61 | // There was an error launching the thread (but it was already logged along with the reason) |
| 62 | LOGERROR("A new cSocketThread failed to start"); |
| 63 | delete Thread; |
| 64 | return false; |
| 65 | } |
| 66 | Thread->AddClient(a_Socket, a_Client); |
| 67 | m_Threads.push_back(Thread); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | |
| 72 |
no test coverage detected