Barst  2.0
A server that controls lab hardware.
named pipes.h
1 
3 #ifndef _CPL_NAMED_PIPES_H_
4 #define _CPL_NAMED_PIPES_H_
5 
6 #include <Windows.h>
7 #include <string>
8 #include <list>
9 #include <vector>
10 #include "mem pool.h"
11 #include "base classses.h"
12 #include "cpl queue.h"
13 #include <sstream>
14 
15 
16 extern __int64 g_llMaxQueueBytes;
17 extern void InitializeQueueLimit();
18 
19 
20 
21 
22 // we proceed with states downwards
23 enum ePipeStatus{
24  kCreated, // pipe was just created and isn't yet connected
25  kConnecting, // pipe is connecting or connected
26  kReading // we're reading the pipe.
27 };
28 
29 // resource which stores all the pipe's info
30 typedef struct SPipeResource
31 {
32  HANDLE hPipe; // handle to pipe
33  HANDLE hEvent; // general event, including signals reading
34  OVERLAPPED oOverlap; // struct used for connecting and reading
35  BOOL fPending; // if reading or connecting is pending
36  ePipeStatus eStatus; // pipe's status
37  HANDLE hWriteEvent; // event used to sginal write finished
38  OVERLAPPED oWriteOverlap; // struct for async writing
39  BOOL fWritePending; // if a write is pending
40  __int64 llId; // unique identifier for this pipe
41  void* pRead; // hodls buffer into which we're currently reading
42  CQueue<SData*> cWriteQueue; // holds data to be written to pipe
43 
44  SPipeResource(HANDLE hWriteEventE) : cWriteQueue(hWriteEventE)
45  {
46  memset(&hPipe, 0, offsetof(SPipeResource, cWriteQueue));
47  eStatus= kCreated;
48  hWriteEvent= hWriteEventE;
49  oWriteOverlap.hEvent= hWriteEvent;
50  }
51 
52  ~SPipeResource() // queue must be emptied before calling this, otherwise memory leak
53  {
54  //FlushFileBuffers(hPipe);
55  DisconnectNamedPipe(hPipe);
56  CloseHandle(hPipe);
57  CloseHandle(hEvent);
58  CloseHandle(hWriteEvent);
59  }
60 
61  __int64 ClearQueue()
62  {
63  bool bNotEmpty;
64  __int64 llCount = 0;
65  while (cWriteQueue.GetSize())
66  {
67  SData* pData= cWriteQueue.Front(true, bNotEmpty);
68  if (pData)
69  {
70  llCount += pData->dwSize;
71  pData->pDevice->Result(pData->pHead, false);
72  delete pData;
73  }
74  }
75  return llCount;
76  }
77 
78  void ResetResource()
79  {
80  memset(&oOverlap, 0, sizeof(OVERLAPPED));
81  memset(&oWriteOverlap, 0, sizeof(OVERLAPPED));
82  //FlushFileBuffers(hPipe);
83  DisconnectNamedPipe(hPipe);
84  SetEvent(hEvent);
85  ResetEvent(hWriteEvent);
86  fWritePending= fPending= FALSE;
87  eStatus= kCreated;
88  oWriteOverlap.hEvent= hWriteEvent;
89  oOverlap.hEvent= hEvent;
90  }
91 
93 
94 
98 class CPipeServer : public CComm
99 {
100 public:
101  CPipeServer();
102  virtual ~CPipeServer();
103 
114  int Init(const TCHAR szPipe[], int nPipes, DWORD dwBuffSizeIn, DWORD dwBuffSizeOut, CDevice *cDevice,
115  CLogBuffer *cLogBuffer);
116  int SendData(const SData *pData, __int64 llId);
117  void Close();
118 
119  DWORD ThreadProc(); // the thread function that deals with all the clients.
120 private:
121  std::tstring m_csPipe; // the pipe name
122  int m_nMaxPipes; // max number of clients connectable
123  int m_nConnected; // number of client currently connected.
124  DWORD m_dwBuffSizeIn;
125  DWORD m_dwBuffSizeOut;
126  HANDLE m_hThread; // thread handle
127  HANDLE m_hDone; // event set when we want the pipe thread to exit
128 
129  std::tostringstream m_sStream; // for log
130 
131  __int64 m_llNextId; // the ID of the next client that connects
132 
133  std::vector<SPipeResource*> m_aPipes; // holds all the pipes
134  std::vector<HANDLE> m_ahEvents; // holds all the events for the pipes so we can wait on them
135 
136  CRITICAL_SECTION m_sPipeSafe; // protects access to a pipe
137  CRITICAL_SECTION m_sInitSafe; // protects access to initialization and exits
138  bool m_bWorking; // if the instance was initialized already
139 };
140 
141 
142 #endif
virtual void Result(void *pHead, bool bPass)=0
int GetSize()
Definition: cpl queue.h:52
T Front(bool bPop, bool &bValid)
Definition: cpl queue.h:38