-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
87 lines (70 loc) · 2.22 KB
/
main.cpp
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
//Trabalho Prático de Sistemas Distribuídos Para Automação 2011/02
//Gustavo Barreto Garcia 2006048133
//Rafael Gilmar Ribeiro Gurgel 2006048192
//main.c
#include <iostream>
#include <process.h>
#include <string.h>
#include "Opcclass.h"
#include "MailslotOperations.h"
#include "SocketClient.h"
#include "OPCClient.h"
typedef unsigned (WINAPI *CAST_FUNCTION)(LPVOID);
typedef unsigned *CAST_LPDWORD;
//Threads Handles
HANDLE hSocket; //Socket Client Thread
HANDLE hOPC; //OPC Client Thread
//Mailslot Handles
HANDLE hMailslot; //handle to Mailslot Server where all log info will be deposited
//Event Handles
HANDLE hSync; //To notify when mailslot client will be ready
int main (void)
{
DWORD dwThreadID; //Where the Thread ID will be deposited when created a Thread
string log;
char bigBuffer[1024];
//MailSlot Sync event
hSync = CreateEvent(NULL,TRUE,FALSE, (LPCSTR)"Synchronization");
//Creating Threads
//Create the Socket Thread, it creates thread to handle the TCP connection.
hSocket = (HANDLE) _beginthreadex(
NULL,
0,
(CAST_FUNCTION)dwSocket,
L"SocketClient",
0,
(CAST_LPDWORD)&dwThreadID);
//Create the OPC client thread, it handles the communication with the OPC server.
hOPC = (HANDLE) _beginthreadex(
NULL,
0,
(CAST_FUNCTION)dwOPC,
L"OPCClient",
0,
(CAST_LPDWORD)&dwThreadID);
//Creating the Mailslot Server
hMailslot = CreateMailslot(
(LPCSTR)"\\\\.\\mailslot\\mylogs",
0,
MAILSLOT_WAIT_FOREVER,
NULL);
if (hMailslot == INVALID_HANDLE_VALUE)
std::cout << "Create Mailslot failed" << std::endl;
else
SetEvent(hSync);
//Read Messages sent by the OPC Client thread.
while (true)
{
if (ReadSlot(hMailslot,bigBuffer) == TRUE)
cout<<bigBuffer<<endl;
}
//Wait for the Threads
WaitForSingleObject(hSocket, INFINITE);
WaitForSingleObject(hOPC, INFINITE);
//Closing all Handles
CloseHandle(hSocket);
CloseHandle(hOPC);
CloseHandle(hMailslot);
system("pause");
return 0;
}