1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_KASKO_TESTING_SAFE_PIPE_READER_H_
16 : #define SYZYGY_KASKO_TESTING_SAFE_PIPE_READER_H_
17 :
18 : #include <stdint.h>
19 : #include <windows.h>
20 :
21 : #include "base/macros.h"
22 : #include "base/threading/thread.h"
23 : #include "base/time/time.h"
24 : #include "base/win/scoped_handle.h"
25 :
26 m : namespace kasko {
27 m : namespace testing {
28 :
29 : // Opens an anonymous pipe that may be used for rudimentary IPC.
30 m : class SafePipeReader {
31 m : public:
32 : // Instantiates an anonymous pipe.
33 m : SafePipeReader();
34 :
35 m : ~SafePipeReader();
36 :
37 : // @returns an inheritable handle that may be used to write to the pipe.
38 m : HANDLE write_handle() { return write_handle_; }
39 :
40 : // Reads data from the anonymous pipe.
41 : // @param timeout The maximum duration to wait for the read operation to
42 : // complete.
43 : // @param length The number of bytes to read.
44 : // @param buffer The destination to read into.
45 : // @returns true upon success.
46 m : bool ReadData(base::TimeDelta timeout, size_t length, void* buffer);
47 :
48 : // @returns true if the instance is successfully initialized and ready for a
49 : // call to ReadData.
50 m : bool IsValid();
51 :
52 m : private:
53 m : base::Thread thread_;
54 m : HANDLE read_handle_;
55 m : HANDLE write_handle_;
56 :
57 m : DISALLOW_COPY_AND_ASSIGN(SafePipeReader);
58 m : };
59 :
60 m : } // namespace testing
61 m : } // namespace kasko
62 :
63 : #endif // SYZYGY_KASKO_TESTING_SAFE_PIPE_READER_H_
|