source: filezilla/trunk/fuentes/src/engine/iothread.h @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 3 years ago

First release to xenial

File size: 2.1 KB
Line 
1#ifndef __IOTHREAD_H__
2#define __IOTHREAD_H__
3
4#include <wx/file.h>
5#include "event_loop.h"
6
7#define BUFFERCOUNT 5
8#define BUFFERSIZE 128*1024
9
10// Does not actually read from or write to file
11// Useful for benchmarks to avoid IO bottleneck
12// skewing results
13//#define SIMULATE_IO
14
15struct io_thread_event_type{};
16typedef CEvent<io_thread_event_type> CIOThreadEvent;
17
18enum IORet
19{
20        IO_Success = 0,
21        IO_Error = -2,
22        IO_Again = -1
23};
24
25class CFile;
26class CIOThread final : protected wxThread
27{
28public:
29        CIOThread();
30        virtual ~CIOThread();
31
32        bool Create(std::unique_ptr<CFile> && pFile, bool read, bool binary);
33        virtual void Destroy(); // Only call that might be blocking
34
35        // Call before first call to one of the GetNext*Buffer functions
36        // This handler will receive the CIOThreadEvent events. The events
37        // get triggerd iff a buffer is available after a call to the
38        // GetNext*Buffer functions returned IO_Again
39        void SetEventHandler(CEventHandler* handler);
40
41        // Gets next buffer
42        // Return value:  IO_Success on EOF
43        //                IO_Again if it would block
44        //                IO_Error on error
45        //                buffersize else
46        int GetNextReadBuffer(char** pBuffer);
47
48        // Gets next write buffer
49        // Return value: IO_Again if it would block
50        //               IO_Error on error
51        //               IO_Success else
52        int GetNextWriteBuffer(char** pBuffer);
53
54        bool Finalize(int len);
55
56        wxString GetError();
57
58protected:
59        void Close();
60
61        virtual ExitCode Entry();
62
63        int ReadFromFile(char* pBuffer, int maxLen);
64        bool WriteToFile(char* pBuffer, int len);
65        bool DoWrite(const char* pBuffer, int len);
66
67        CEventHandler* m_evtHandler{};
68
69        bool m_read{};
70        bool m_binary{};
71        std::unique_ptr<CFile> m_pFile;
72
73        char* m_buffers[BUFFERCOUNT];
74        unsigned int m_bufferLens[BUFFERCOUNT];
75
76        mutex m_mutex;
77        condition m_condition;
78
79        int m_curAppBuf{};
80        int m_curThreadBuf{};
81
82        bool m_error{};
83        bool m_running{};
84        bool m_threadWaiting{};
85        bool m_appWaiting{};
86
87        bool m_destroyed{};
88
89        bool m_wasCarriageReturn{};
90
91        wxString m_error_description;
92
93#ifdef SIMULATE_IO
94        wxFileOffset size_{};
95#endif
96};
97
98#endif //__IOTHREAD_H__
Note: See TracBrowser for help on using the repository browser.