00001 #ifndef __TRANSFERTHREAD_H__ 00002 #define __TRANSFERTHREAD_H__ 00003 00004 #include <util/thread.h> 00005 #include <util/message.h> 00006 #include <util/locker.h> 00007 #include <list> 00008 #include <queue> 00009 00010 #include <curl/curl.h> 00011 00012 using namespace os; 00013 00014 class Server; 00015 00018 enum CurlHandleStatus { 00019 HANDLE_IDLE = -1, 00020 HANDLE_INVALID = -2 00021 }; 00022 00026 typedef struct { 00027 CURL* m_pHandle; 00028 int m_nStatus; 00029 } CurlHandle_s; 00030 00037 class TransferThread : public Thread 00038 { 00039 public: 00040 TransferThread( Server* pcServer ); 00041 ~TransferThread(); 00042 00043 /* Main thread code is in Run() */ 00044 int32 Run(); 00045 00046 /* Lock/unlock the message list so no-one will change it while we are using it */ 00047 int Lock(); 00048 int Unlock(); 00049 00050 /* Send a message to the transfer thread asking it to add, start, etc a given transfer */ 00051 int SendMessage( int nCode ); 00052 int SendMessage( Message* pcMsg ); 00053 00054 private: 00055 void _Notify(); /* Send a signal to the thread to notify it that it has new messages */ 00056 00057 void _ProcessMessages(); /* Process the messages in the message list */ 00058 00059 void _Schedule(); /* Assign queued transfers from the queue to idle curl handles */ 00060 00061 void _CleanupNode( int nNode ); /* Remove & delete a finished queue node */ 00062 00064 Server* m_pcServer; 00065 00066 /* Inter-thread communication */ 00067 int m_nReadPipe; 00068 int m_nWritePipe; 00069 std::queue< Message* > m_apcMessages; 00070 Locker* m_pcLock; 00072 /* libcurl stuff */ 00073 CURLM* m_pCurlMultiHandle; 00074 std::vector< CurlHandle_s* > m_asHandles; 00076 friend class Server; 00077 }; 00078 00079 00080 00081 #endif /* __TRANSFERTHREAD_H__ */ 00082 00083 00084 00085 00086 00087