Merge pull request #799 from winsvega/winpipe

Winpipe
This commit is contained in:
Bob Summerwill 2016-08-02 22:49:00 -07:00 committed by GitHub
commit 4fbe4a1cc4
6 changed files with 199 additions and 30 deletions

View File

@ -13,6 +13,8 @@
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
The Implementation originally from https://msdn.microsoft.com/en-us/library/windows/desktop/aa365592(v=vs.85).aspx
*/
/** @file RPCSession.cpp
* @author Dimtiry Khokhlov <dimitry@ethdev.com>
@ -32,6 +34,21 @@ using namespace dev;
IPCSocket::IPCSocket(string const& _path): m_path(_path)
{
m_socket = CreateFile(
m_path.c_str(), // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attribute
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
if (m_socket == INVALID_HANDLE_VALUE)
BOOST_FAIL("Error creating IPC socket object");
#if defined(_WIN32)
#else
if (_path.length() >= sizeof(sockaddr_un::sun_path))
BOOST_FAIL("Error opening IPC: socket path is too long!");
@ -58,10 +75,43 @@ IPCSocket::IPCSocket(string const& _path): m_path(_path)
BOOST_FAIL("Error connecting to IPC socket: " << _path);
m_fp = fdopen(m_socket, "r");
#endif
}
string IPCSocket::sendRequest(string const& _req)
{
#if defined(_WIN32)
string returnStr;
DWORD cbWritten;
BOOL fSuccess = WriteFile(
m_socket, // pipe handle
_req.c_str(), // message
_req.size(), // message length
&cbWritten, // bytes written
NULL); // not overlapped
if (!fSuccess)
BOOST_FAIL("WriteFile to pipe failed");
DWORD cbRead;
TCHAR chBuf[c_buffsize];
// Read from the pipe.
fSuccess = ReadFile(
m_socket, // pipe handle
chBuf, // buffer to receive reply
c_buffsize,// size of buffer
&cbRead, // number of bytes read
NULL); // not overlapped
returnStr += chBuf;
if (!fSuccess)
BOOST_FAIL("ReadFile from pipe failed");
cerr << "."; //Output for log activity
return returnStr;
#else
send(m_socket, _req.c_str(), _req.length(), 0);
char c;
@ -74,6 +124,7 @@ string IPCSocket::sendRequest(string const& _req)
break;
}
return response;
#endif
}
RPCSession& RPCSession::instance(const string& _path)

View File

@ -19,15 +19,37 @@
* @date 2016
*/
#include <string>
#include <stdio.h>
#include <map>
#if defined(_WIN32)
#include <windows.h>
#include "libdevcore/UndefMacros.h"
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#endif
#include <string>
#include <stdio.h>
#include <map>
#include <json/value.h>
#include <boost/test/unit_test.hpp>
#if defined(_WIN32)
const int c_buffsize = 5120000; //because windows pipe is broken and wont work as in examples. use larger buffer limit to receive whole package in one call
class IPCSocket : public boost::noncopyable
{
public:
IPCSocket(std::string const& _path);
std::string sendRequest(std::string const& _req);
~IPCSocket() { CloseHandle(m_socket); }
std::string const& path() const { return m_path; }
private:
std::string m_path;
HANDLE m_socket;
};
#else
class IPCSocket: public boost::noncopyable
{
public:
@ -41,8 +63,8 @@ private:
FILE *m_fp;
std::string m_path;
int m_socket;
};
#endif
class RPCSession: public boost::noncopyable
{

51
test/TestHelper.cpp Normal file
View File

@ -0,0 +1,51 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file TestHelper.h
* @author Marko Simovic <markobarko@gmail.com>
* @date 2014
*/
#include <boost/test/framework.hpp>
#include "TestHelper.h"
using namespace std;
using namespace dev::test;
Options::Options(int argc, char** argv)
{
tArgc = 0;
tArgv = new char*[argc];
for (size_t i = 0; i < argc; i++)
{
string arg = argv[i];
if (arg == "--ipc" && i + 1 < argc)
{
ipcPath = argv[i + 1];
i++;
}
else
{
tArgv[i] = argv[i];
tArgc++;
}
}
}
Options const& Options::get(int argc, char** argv)
{
static Options instance(argc, argv);
return instance;
}

View File

@ -102,5 +102,20 @@ namespace test
} \
while (0)
class Options
{
public:
std::string ipcPath;
int tArgc;
char **tArgv;
/// Get reference to options
/// The first time used, options are parsed with argc, argv
static Options const& get(int argc = 0, char** argv = 0);
private:
Options(int argc, char** argv = 0);
Options(Options const&) = delete;
};
}
}

View File

@ -25,17 +25,60 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4535) // calling _set_se_translator requires /EHa
#endif
#include <boost/test/included/unit_test.hpp>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#pragma GCC diagnostic pop
#define BOOST_TEST_NO_MAIN
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4535) // calling _set_se_translator requires /EHa
#endif
#include <boost/test/included/unit_test.hpp>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#include <test/TestHelper.h>
using namespace boost::unit_test;
#pragma GCC diagnostic pop
#include <stdlib.h>
#include <boost/version.hpp>
#include "TestHelper.h"
using namespace boost::unit_test;
std::vector<char*> parameters;
static std::ostringstream strCout;
std::streambuf* oldCoutStreamBuf;
std::streambuf* oldCerrStreamBuf;
//Custom Boost Initialization
test_suite* fake_init_func(int argc, char* argv[])
{
//Required for boost. -nowarning
(void)argc;
(void)argv;
return 0;
}
//Custom Boost Unit Test Main
int main(int argc, char* argv[])
{
//Initialize options before boost reads it
dev::test::Options const& opt = dev::test::Options::get(argc, argv);
return unit_test_main(fake_init_func, opt.tArgc, opt.tArgv);
}
/*
#else
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4535) // calling _set_se_translator requires /EHa
#endif
#include <boost/test/included/unit_test.hpp>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#pragma GCC diagnostic pop
#include <test/TestHelper.h>
using namespace boost::unit_test;
#endif*/

View File

@ -25,8 +25,6 @@
#include <libdevcore/CommonIO.h>
#include <test/libsolidity/SolidityExecutionFramework.h>
using namespace std;
using namespace dev;
using namespace dev::solidity;
@ -39,24 +37,13 @@ namespace // anonymous
string getIPCSocketPath()
{
string ipcPath;
size_t argc = boost::unit_test::framework::master_test_suite().argc;
char** argv = boost::unit_test::framework::master_test_suite().argv;
for (size_t i = 0; i < argc; i++)
{
string arg = argv[i];
if (arg == "--ipc" && i + 1 < argc)
{
ipcPath = argv[i + 1];
i++;
}
}
string ipcPath = dev::test::Options::get().ipcPath;
if (ipcPath.empty())
if (auto path = getenv("ETH_TEST_IPC"))
ipcPath = path;
if (ipcPath.empty())
BOOST_FAIL("ERROR: ipcPath not set! (use --ipc <path> or the environment variable ETH_TEST_IPC)");
return ipcPath;
}