pipe_unix.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) 2010 Google Inc.
  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. #ifndef SIMPLE_IPC_PIPE_UNIX_H_
  15. #define SIMPLE_IPC_PIPE_UNIX_H_
  16. #include "os_includes.h"
  17. #include "ipc_constants.h"
  18. class PipePair {
  19. public:
  20. // unused bool so matches windows PipePair constructor
  21. // which allows inheriting
  22. PipePair(bool unused = false);
  23. int fd1() const { return fd_[0]; }
  24. int fd2() const { return fd_[1]; }
  25. private:
  26. int fd_[2];
  27. };
  28. class PipeUnix {
  29. public:
  30. PipeUnix();
  31. bool OpenClient(int fd);
  32. bool OpenServer(int fd);
  33. bool Write(const void* buf, size_t sz);
  34. bool Read(void* buf, size_t* sz);
  35. bool IsConnected() const { return fd_ != -1; }
  36. private:
  37. int fd_;
  38. };
  39. class PipeTransport : public PipeUnix {
  40. public:
  41. static const size_t kBufferSz = 4096;
  42. size_t Send(const void* buf, size_t sz) {
  43. return Write(buf, sz) ? ipc::RcOK : ipc::RcErrTransportWrite;
  44. }
  45. char* Receive(size_t* size);
  46. private:
  47. IPCCharVector buf_;
  48. };
  49. #endif // SIMPLE_IPC_PIPE_UNIX_H_