Skip to content

Commit

Permalink
fix(ipc): try to reconnect when failed
Browse files Browse the repository at this point in the history
  • Loading branch information
nameoverflow authored and Prcuvu committed Feb 24, 2018
1 parent 784679c commit 3c286b6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
19 changes: 7 additions & 12 deletions WeaselIPC/PipeChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ using namespace weasel;
using namespace std;
using namespace boost;

#define _ThrowLastError throw GetLastError()

#define _ThrowLastError throw ::GetLastError()
#define _ThrowCode(__c) throw __c
#define _ThrowIfNot(__c) { DWORD err; if ((err = ::GetLastError()) != __c) throw __c; }

PipeChannelBase::PipeChannelBase(std::wstring &pn_cmd, size_t bs = 4 * 1024, SECURITY_ATTRIBUTES *s = NULL)
: cmd_name(pn_cmd),
Expand Down Expand Up @@ -74,18 +75,13 @@ void PipeChannelBase::_Reconnect(HANDLE pipe)

HANDLE PipeChannelBase::_TryConnect(const wchar_t *name)
{
DWORD connectErr;
auto pipe = ::CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if (!_Invalid(pipe)) {
// connected to the pipe
return pipe;
}
// being busy is not really an error since we just need to wait.
if ((connectErr = ::GetLastError()) != ERROR_PIPE_BUSY) {

throw connectErr; // otherwise, pipe creation fails
}
_ThrowIfNot(ERROR_PIPE_BUSY);
// All pipe instances are busy
return INVALID_HANDLE_VALUE;
}
Expand All @@ -100,7 +96,7 @@ size_t PipeChannelBase::_WritePipe(HANDLE p, size_t s, char *b)
return lwritten;
}

void PipeChannelBase::_FinalizePipe(HANDLE pipe)
void PipeChannelBase::_FinalizePipe(HANDLE &pipe)
{
if (!_Invalid(pipe)) {
DisconnectNamedPipe(pipe);
Expand All @@ -115,9 +111,8 @@ void PipeChannelBase::_Receive(HANDLE pipe, LPVOID msg, size_t rec_len)
DWORD lread;
BOOL success = ::ReadFile(pipe, msg, rec_len, &lread, NULL);
if (!success) {
if (GetLastError() != ERROR_MORE_DATA) {
_ThrowLastError;
}
_ThrowIfNot(ERROR_MORE_DATA);

memset(buffer.get(), 0, buff_size);
success = ::ReadFile(pipe, buffer.get(), buff_size, &lread, NULL);
if (!success) {
Expand Down
3 changes: 2 additions & 1 deletion WeaselIPC/WeaselClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void ClientImpl::Disconnect()
{
if (_Active())
EndSession();
channel.Disconnect();
}

void ClientImpl::ShutdownServer()
Expand Down Expand Up @@ -192,7 +193,7 @@ LRESULT ClientImpl::_SendMessage(WEASEL_IPC_COMMAND Msg, DWORD wParam, DWORD lPa
PipeMessage req{ Msg, wParam, lParam };
return channel.Transact(req);
}
catch (...) {
catch (DWORD ex) {
return 0;
}
}
Expand Down
2 changes: 1 addition & 1 deletion WeaselIPCServer/WeaselServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ void PipeServer::Listen(ServerHandler const &handler)
_ProcessPipeThread(pipe, handler);
});
}
catch (...) {
catch (DWORD ex) {
_FinalizePipe(pipe);
}
boost::this_thread::interruption_point();
Expand Down
1 change: 1 addition & 0 deletions WeaselTSF/WeaselTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ void WeaselTSF::_EnsureServerConnected()
{
if (!m_client.Echo())
{
m_client.Disconnect();
m_client.Connect(NULL);
m_client.StartSession();
}
Expand Down
8 changes: 5 additions & 3 deletions include/PipeChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ namespace weasel {
PipeChannelBase(PipeChannelBase &&r);
~PipeChannelBase();

bool Connect() { return _Ensure(); }
bool Connected() const { return !_Invalid(msg_pipe); }
void Disconnect() { _FinalizePipe(msg_pipe); }

protected:

/* To ensure connection before operation */
Expand All @@ -26,7 +30,7 @@ namespace weasel {
/* Try to connect for one time */
HANDLE _TryConnect(const wchar_t *name);
size_t _WritePipe(HANDLE p, size_t s, char *b);
void _FinalizePipe(HANDLE pipe);
void _FinalizePipe(HANDLE &pipe);
void _Receive(HANDLE pipe, LPVOID msg, size_t rec_len);
/* Try to get a connection from client */
HANDLE _ConnectServerPipe(std::wstring &pn);
Expand Down Expand Up @@ -127,8 +131,6 @@ namespace weasel {
return handler((LPWSTR)buffer.get(), buff_size * sizeof(char) / sizeof(wchar_t));
}

bool Connect() { return _Ensure(); }
bool Connected() const { return !_Invalid(msg_pipe); }

protected:

Expand Down

0 comments on commit 3c286b6

Please sign in to comment.