winss
pipe_instance.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 Morgan Stanley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIB_WINSS_PIPE_INSTANCE_HPP_
18 #define LIB_WINSS_PIPE_INSTANCE_HPP_
19 
20 #include <windows.h>
21 #include <vector>
22 #include <queue>
23 #include "handle_wrapper.hpp"
24 #include "pipe_name.hpp"
25 
26 namespace winss {
34 };
35 
39 class PipeInstance {
40  protected:
41  OVERLAPPED overlapped;
42  HANDLE handle = nullptr;
43  bool connected = false;
44  bool pending_io = false;
45  bool close = false;
46  std::vector<char> buffer;
47  DWORD bytes = 0;
54  bool CheckError();
55 
56  public:
57  static const DWORD kBufferSize = 4096;
58  static const DWORD kTimeout = 5000;
63  PipeInstance();
64 
65  PipeInstance(const PipeInstance&) = delete;
72  PipeInstance(PipeInstance&& instance);
73 
79  virtual bool IsPendingIO() const;
80 
86  virtual bool IsConnected() const;
87 
93  virtual bool IsClosing() const;
94 
101  virtual winss::HandleWrapper GetHandle() const;
102 
108  virtual bool SetConnected();
109 
116 
123  virtual bool CreateNamedPipe(const winss::PipeName& pipe_name);
124 
131  virtual bool CreateFile(const winss::PipeName& pipe_name);
132 
138  virtual void Closing();
139 
143  virtual void DisconnectNamedPipe();
144 
150  virtual bool Close();
151 
153  PipeInstance& operator=(const PipeInstance&) = delete;
154 
161  PipeInstance& operator=(PipeInstance&& instance);
162 
164  virtual ~PipeInstance();
165 };
166 
171  private:
172  bool writting = false;
173  std::queue<std::vector<char>> message_queue;
175  public:
180 
183 
190 
196  bool Queue(const std::vector<char>& data);
197 
203  bool HasMessages() const;
204 
210  bool IsWriting() const;
211 
217  bool FinishWrite();
218 
224  bool Write();
225 
229  void Read();
230 
233 
241 };
242 
247  public:
252 
254  InboundPipeInstance(const InboundPipeInstance&) = delete;
255 
262 
268  bool FinishRead();
269 
273  void Read();
274 
280  std::vector<char> SwapBuffer();
281 
284 
292 };
293 } // namespace winss
294 
295 #endif // LIB_WINSS_PIPE_INSTANCE_HPP_
std::vector< char > buffer
The instance byte buffer.
Definition: pipe_instance.hpp:46
virtual bool IsClosing() const
Gets if the instance is closing.
Definition: pipe_instance.cpp:61
A wrapper for a Windows HANDLE.
Definition: handle_wrapper.hpp:39
OverlappedResult
The result of the overlapped operation.
Definition: pipe_instance.hpp:30
virtual bool CreateFile(const winss::PipeName &pipe_name)
Creates a Windows named pipe client.
Definition: pipe_instance.cpp:181
virtual winss::HandleWrapper GetHandle() const
Gets the event handle for the instance.
Definition: pipe_instance.cpp:65
bool connected
Flags if the instance is connected.
Definition: pipe_instance.hpp:43
virtual bool CreateNamedPipe(const winss::PipeName &pipe_name)
Creates the Windows named pipe server.
Definition: pipe_instance.cpp:138
virtual OverlappedResult GetOverlappedResult()
Gets the last overlapped result of the instance.
Definition: pipe_instance.cpp:100
bool pending_io
Flagged if IO event is pending.
Definition: pipe_instance.hpp:44
virtual bool SetConnected()
Sets the instance to connected state.
Definition: pipe_instance.cpp:69
PipeInstance & operator=(const PipeInstance &)=delete
No copy.
bool CheckError()
Checks if error is a real error or pending IO operation.
Definition: pipe_instance.cpp:79
DWORD bytes
The bytes read or written.
Definition: pipe_instance.hpp:47
virtual ~PipeInstance()
Default destructor.
Definition: pipe_instance.cpp:261
Pipe names are based on file system paths.
Definition: pipe_name.hpp:32
Definition: case_ignore.hpp:23
virtual void DisconnectNamedPipe()
DIsconnect the client from the pipe server.
Definition: pipe_instance.cpp:209
Wait till next result.
Definition: pipe_instance.hpp:33
HANDLE handle
The event handle.
Definition: pipe_instance.hpp:42
The pipe instance which is shared between client and server.
Definition: pipe_instance.hpp:39
A pipe instance for outbound client/server.
Definition: pipe_instance.hpp:170
PipeInstance()
Creates a new pipe instance.
Definition: pipe_instance.cpp:27
bool close
Flagged if instance is closing.
Definition: pipe_instance.hpp:45
virtual void Closing()
Marks the pipe instance as closing and triggers the event.
Definition: pipe_instance.cpp:201
virtual bool Close()
Close the pipe connection.
Definition: pipe_instance.cpp:217
A pipe instance for inbound client/server.
Definition: pipe_instance.hpp:246
static const DWORD kTimeout
The pipe timeout.
Definition: pipe_instance.hpp:58
OVERLAPPED overlapped
The overlapped structure.
Definition: pipe_instance.hpp:41
CLient is ready.
Definition: pipe_instance.hpp:32
Close client.
Definition: pipe_instance.hpp:31
virtual bool IsPendingIO() const
Gets if there is pending IO operations.
Definition: pipe_instance.cpp:53
virtual bool IsConnected() const
Gets if the instance is connected.
Definition: pipe_instance.cpp:57
static const DWORD kBufferSize
The pipe buffer.
Definition: pipe_instance.hpp:57