forked from bigmacd/trapreceiver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogQueueClient.cpp
More file actions
97 lines (86 loc) · 1.99 KB
/
LogQueueClient.cpp
File metadata and controls
97 lines (86 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2013 trapreceiver.com
// All rights reserved.
//
// Redistribution and use of executable software is never
// permitted without the express written permission of
// trapreceiver.com
//
// Distribution of the source is never permitted without
// the express written permission of
// trapreceiver.com
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#ifndef __QUEUECLIENT_H__
#include "LogQueueClient.h"
#endif
#include <stdio.h>
//#include <iostream.h>
LogQueueClient::LogQueueClient()
{
Connect();
}
LogQueueClient::~LogQueueClient()
{
try
{
if (mQueueHandle != INVALID_HANDLE_VALUE)
CloseHandle(mQueueHandle);
}
catch(...) {}
}
void
LogQueueClient::Put(unsigned char* message, unsigned int length)
{
unsigned long len = 0;
BOOL tryWrite = TRUE; // assume good connection
if (mQueueHandle == INVALID_HANDLE_VALUE) // didn't connect previously
if (!Connect()) // try now
tryWrite = FALSE; // still can't
if (tryWrite)
{
if (!WriteFile(mQueueHandle,
message,
length,
&len,
NULL)) // oops, used to, but cant now!
if (GetLastError() == ERROR_HANDLE_EOF) // server disconnected
{
// clean up
try {
CloseHandle(mQueueHandle);
}
catch(...) { }
mQueueHandle = INVALID_HANDLE_VALUE;
// try to connect again
if (Connect())
WriteFile(mQueueHandle,
message,
length,
&len,
NULL);
}
}
}
BOOL
LogQueueClient::Connect()
{
BOOL retVal = FALSE;
mQueueHandle = CreateFile("\\\\.\\mailslot\\trap\\guiToLog",
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (mQueueHandle != INVALID_HANDLE_VALUE)
retVal = TRUE;
else
{
int err = GetLastError();
if (err == 99)
return FALSE;
}
return retVal;
}