WinPipeServer.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file WinPipeServer.cpp
  15. * @authors:
  16. * Arkadiy Paronyan <arkadiy@ethdev.com>
  17. * @date 2015
  18. */
  19. #if defined(_WIN32)
  20. #include "WinPipeServer.h"
  21. #include <windows.h>
  22. #include <libdevcore/Guards.h>
  23. using namespace std;
  24. using namespace jsonrpc;
  25. using namespace dev;
  26. int const c_bufferSize = 1024;
  27. WindowsPipeServer::WindowsPipeServer(string const& _appId):
  28. IpcServerBase("\\\\.\\pipe\\" + _appId + ".ipc")
  29. {
  30. }
  31. void WindowsPipeServer::CloseConnection(HANDLE _socket)
  32. {
  33. ::CloseHandle(_socket);
  34. }
  35. size_t WindowsPipeServer::Write(HANDLE _connection, std::string const& _data)
  36. {
  37. DWORD written = 0;
  38. ::WriteFile(_connection, _data.data(), _data.size(), &written , nullptr);
  39. return written;
  40. }
  41. size_t WindowsPipeServer::Read(HANDLE _connection, void* _data, size_t _size)
  42. {
  43. DWORD read;
  44. ::ReadFile(_connection, _data, _size, &read, nullptr);
  45. return read;
  46. }
  47. void WindowsPipeServer::Listen()
  48. {
  49. while (m_running)
  50. {
  51. HANDLE socket = CreateNamedPipe(
  52. m_path.c_str(),
  53. PIPE_ACCESS_DUPLEX,
  54. PIPE_READMODE_BYTE |
  55. PIPE_WAIT,
  56. PIPE_UNLIMITED_INSTANCES,
  57. c_bufferSize,
  58. c_bufferSize,
  59. 0,
  60. nullptr);
  61. DEV_GUARDED(x_sockets)
  62. m_sockets.insert(socket);
  63. if (ConnectNamedPipe(socket, nullptr) != 0)
  64. {
  65. std::thread handler([this, socket](){ GenerateResponse(socket); });
  66. handler.detach();
  67. }
  68. else
  69. {
  70. DEV_GUARDED(x_sockets)
  71. m_sockets.erase(socket);
  72. }
  73. }
  74. }
  75. #endif