Merge pull request #1669 from ethereum/ipc-cleanup

Don't rely on new lines in IPC RPC (soltest)
This commit is contained in:
chriseth 2017-02-10 10:37:43 +01:00 committed by GitHub
commit af8e31b023
2 changed files with 23 additions and 32 deletions

View File

@ -18,6 +18,7 @@
*/ */
/** @file RPCSession.cpp /** @file RPCSession.cpp
* @author Dimtiry Khokhlov <dimitry@ethdev.com> * @author Dimtiry Khokhlov <dimitry@ethdev.com>
* @author Alex Beregszaszi
* @date 2016 * @date 2016
*/ */
@ -73,17 +74,13 @@ IPCSocket::IPCSocket(string const& _path): m_path(_path)
if (connect(m_socket, reinterpret_cast<struct sockaddr const*>(&saun), sizeof(struct sockaddr_un)) < 0) if (connect(m_socket, reinterpret_cast<struct sockaddr const*>(&saun), sizeof(struct sockaddr_un)) < 0)
BOOST_FAIL("Error connecting to IPC socket: " << _path); BOOST_FAIL("Error connecting to IPC socket: " << _path);
m_fp = fdopen(m_socket, "r");
if (!m_fp)
BOOST_FAIL("Error opening IPC socket: " << _path);
#endif #endif
} }
string IPCSocket::sendRequest(string const& _req) string IPCSocket::sendRequest(string const& _req)
{ {
#if defined(_WIN32) #if defined(_WIN32)
string returnStr; // Write to the pipe.
DWORD cbWritten; DWORD cbWritten;
BOOL fSuccess = WriteFile( BOOL fSuccess = WriteFile(
m_socket, // pipe handle m_socket, // pipe handle
@ -92,39 +89,33 @@ string IPCSocket::sendRequest(string const& _req)
&cbWritten, // bytes written &cbWritten, // bytes written
NULL); // not overlapped NULL); // not overlapped
if (!fSuccess) if (!fSuccess || (_req.size() != cbWritten))
BOOST_FAIL("WriteFile to pipe failed"); BOOST_FAIL("WriteFile to pipe failed");
DWORD cbRead;
TCHAR chBuf[c_buffsize];
// Read from the pipe. // Read from the pipe.
DWORD cbRead;
fSuccess = ReadFile( fSuccess = ReadFile(
m_socket, // pipe handle m_socket, // pipe handle
chBuf, // buffer to receive reply m_readBuf, // buffer to receive reply
c_buffsize,// size of buffer sizeof(m_readBuf), // size of buffer
&cbRead, // number of bytes read &cbRead, // number of bytes read
NULL); // not overlapped NULL); // not overlapped
returnStr += chBuf;
if (!fSuccess) if (!fSuccess)
BOOST_FAIL("ReadFile from pipe failed"); BOOST_FAIL("ReadFile from pipe failed");
return returnStr; return string(m_readBuf, m_readBuf + cbRead);
#else #else
send(m_socket, _req.c_str(), _req.length(), 0); if (send(m_socket, _req.c_str(), _req.length(), 0) != (ssize_t)_req.length())
BOOST_FAIL("Writing on IPC failed");
char c; ssize_t ret = recv(m_socket, m_readBuf, sizeof(m_readBuf), 0);
string response;
while ((c = fgetc(m_fp)) != EOF) // Also consider closed socket an error.
{ if (ret <= 0)
if (c != '\n') BOOST_FAIL("Reading on IPC failed");
response += c;
else return string(m_readBuf, m_readBuf + ret);
break;
}
return response;
#endif #endif
} }

View File

@ -35,7 +35,6 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#if defined(_WIN32) #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 class IPCSocket : public boost::noncopyable
{ {
public: public:
@ -47,7 +46,8 @@ public:
private: private:
std::string m_path; std::string m_path;
HANDLE m_socket; HANDLE m_socket;
TCHAR m_readBuf[512000];
}; };
#else #else
class IPCSocket: public boost::noncopyable class IPCSocket: public boost::noncopyable
@ -55,14 +55,14 @@ class IPCSocket: public boost::noncopyable
public: public:
IPCSocket(std::string const& _path); IPCSocket(std::string const& _path);
std::string sendRequest(std::string const& _req); std::string sendRequest(std::string const& _req);
~IPCSocket() { close(m_socket); fclose(m_fp); } ~IPCSocket() { close(m_socket); }
std::string const& path() const { return m_path; } std::string const& path() const { return m_path; }
private: private:
FILE *m_fp;
std::string m_path; std::string m_path;
int m_socket; int m_socket;
char m_readBuf[512000];
}; };
#endif #endif