pipe_unix.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. PipePair();
  21. int fd1() const { return fd_[0]; }
  22. int fd2() const { return fd_[1]; }
  23. private:
  24. int fd_[2];
  25. };
  26. class PipeUnix {
  27. public:
  28. PipeUnix();
  29. bool OpenClient(int fd);
  30. bool OpenServer(int fd);
  31. bool Write(const void* buf, size_t sz);
  32. bool Read(void* buf, size_t* sz);
  33. bool IsConnected() const { return fd_ != -1; }
  34. private:
  35. int fd_;
  36. };
  37. class PipeTransport : public PipeUnix {
  38. public:
  39. static const size_t kBufferSz = 4096;
  40. size_t Send(const void* buf, size_t sz) {
  41. return Write(buf, sz) ? ipc::RcOK : ipc::RcErrTransportWrite;
  42. }
  43. char* Receive(size_t* size);
  44. private:
  45. IPCCharVector buf_;
  46. };
  47. #endif // SIMPLE_IPC_PIPE_UNIX_H_