-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDtls-Example.cpp
More file actions
165 lines (127 loc) · 4.78 KB
/
Dtls-Example.cpp
File metadata and controls
165 lines (127 loc) · 4.78 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (C) 2016-2019 Thomas Hauck - All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
The author would be happy if changes and
improvements were reported back to him.
Author: Thomas Hauck
Email: Thomas@fam-hauck.de
*/
#include <thread>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <chrono>
#include "SocketLib/SocketLib.h"
using namespace std;
#if defined (_WIN32) || defined (_WIN64)
#include <conio.h>
#else // Linux
#include <termios.h>
#include <time.h>
void _getch()
{
struct termios t;
tcgetattr(fileno(stdin), &t);
t.c_lflag &= ~ICANON;
tcsetattr(fileno(stdin), TCSANOW, &t);
getchar();
t.c_lflag |= ICANON;
tcsetattr(fileno(stdin), TCSANOW, &t);
}
#define localtime_s(x,y) localtime_r(y,x)
#endif
void ServerThread(const bool* bStop)
{
SslUdpSocket sock;
bool bIsClosed = false;
// 3 callback function to handle the server socket events
sock.BindErrorFunction([&](BaseSocket*) { cout << "Server: socket error" << endl; });
sock.BindCloseFunction([&](BaseSocket*) { cout << "Server: socket closing" << endl; bIsClosed = true; });
sock.BindFuncBytesReceived([&](UdpSocket* pUdpSocket)
{
const size_t nAvailable = pUdpSocket->GetBytesAvailable();
auto spBuffer = make_unique<uint8_t[]>(nAvailable + 1);
string strFrom;
const size_t nRead = pUdpSocket->Read(&spBuffer[0], nAvailable, strFrom);
if (nRead > 0)
{
string strRec(nRead, 0);
copy(&spBuffer[0], &spBuffer[nRead], &strRec[0]);
stringstream strOutput;
const auto tNow = chrono::system_clock::to_time_t(chrono::system_clock::now());
struct tm stTime;
if (localtime_s(&stTime, &tNow) == 0)
strOutput << put_time(&stTime, "%a, %d %b %Y %H:%M:%S") << " - ";
strOutput << strFrom.c_str() << " - Server received: " << nRead << " Bytes, \"" << strRec << "\"" << endl;
cout << strOutput.str();
strRec = "Server echo: " + strRec;
sock.Write(&strRec[0], strRec.size(), strFrom);
}
});
sock.AddCertificate("certs/server-cert.crt", "certs/server-key.pem");
const bool bCreated = sock.CreateServerSide("0.0.0.0", 3461);
while (*bStop == false)
{
this_thread::sleep_for(chrono::milliseconds(10));
}
sock.Close();
while (bIsClosed == false)
this_thread::sleep_for(chrono::milliseconds(10));
}
void ClientThread(const bool* bStop)
{
SslUdpSocket sock;
bool bIsClosed = false;
// 4 callback function to handle the client socket events
sock.BindErrorFunction([&](BaseSocket*) { cout << "Client: socket error" << endl; });
sock.BindCloseFunction([&](BaseSocket*) { cout << "Client: socket closing" << endl; bIsClosed = true; });
sock.BindFuncBytesReceived([&](UdpSocket* pUdpSocket)
{
const size_t nAvailable = pUdpSocket->GetBytesAvailable();
auto spBuffer = make_unique<uint8_t[]>(nAvailable + 1);
string strFrom;
const size_t nRead = pUdpSocket->Read(spBuffer.get(), nAvailable, strFrom);
if (nRead > 0)
{
string strRec(nRead, 0);
copy(&spBuffer[0], &spBuffer[nRead], &strRec[0]);
stringstream strOutput;
const auto tNow = chrono::system_clock::to_time_t(chrono::system_clock::now());
struct tm stTime;
if (localtime_s(&stTime, &tNow) == 0)
strOutput << put_time(&stTime, "%a, %d %b %Y %H:%M:%S") << " - ";
strOutput << strFrom.c_str() << " - Client received: " << nRead << " Bytes, \"" << strRec << "\"" << endl;
cout << strOutput.str();
}
});
sock.BindFuncSslInitDone([&](UdpSocket* pUdpSocket)
{
sock.Write("Hallo World", 11, "127.0.0.1:3461");
});
sock.AddCertificate("certs/client-cert.crt", "certs/client-key.pem");
const bool bCreated = sock.CreateClientSide("0.0.0.0", 1911, "127.0.0.1:3461");
while (*bStop == false)
{
this_thread::sleep_for(chrono::milliseconds(10));
}
sock.Close();
while (bIsClosed == false)
this_thread::sleep_for(chrono::milliseconds(10));
}
int main(int argc, const char* argv[])
{
#if defined(_WIN32) || defined(_WIN64)
// Detect Memory Leaks
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
#endif
bool bStop = false;
thread thServer = thread(bind(ServerThread, &bStop));
// we wait a second so the server is ready
this_thread::sleep_for(chrono::milliseconds(1000));
thread thClient = thread(bind(ClientThread, &bStop));
_getch();
bStop = true;
thServer.join();
thClient.join();
return 0;
}