-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamHandler.cpp
186 lines (166 loc) · 4.35 KB
/
StreamHandler.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "stdafx.h"
#include "StreamHandler.h"
#include "AppData.h"
#include "WorldManager.h"
#include "StreamingManager.h"
namespace BlinkAnalysis
{
StreamHandler::StreamHandler(TcpClient^ ClientSocket, int ClientNumber) {
ContinueProcess = false;
this->ClientSocket = ClientSocket;
this->ClientNumber = ClientNumber;
ascii = gcnew ASCIIEncoding();
frames = new std::queue<std::string>();
sync = gcnew Object();
}
// start listening for client commands
void StreamHandler::Start() {
ContinueProcess = true ;
ClientThread = gcnew Thread ( gcnew ThreadStart(this, &StreamHandler::Process) ) ;
ClientThread->Start() ;
}
void StreamHandler::Stop() {
ContinueProcess = false ;
if ( ClientThread && ClientThread->IsAlive )
ClientThread->Join() ;
if (StreamThread && StreamThread->IsAlive)
StreamThread->Join();
}
bool StreamHandler::Alive() {
return ( ClientThread && ClientThread->IsAlive );
}
void StreamHandler::addFrame(std::string frame)
{
Monitor::Enter(sync);
frames->push(frame);
Monitor::Exit(sync);
if (!StreamThread)
{
StreamThread = gcnew Thread(gcnew ThreadStart(this, &StreamHandler::Stream));
StreamThread->Start();
}
}
void StreamHandler::Stream()
{
while (streamData)
{
int count = frames->size();
if (count > 0)
{
try
{
std::string str = frames->front();
array<Byte>^ response = gcnew array<Byte>(str.size());
System::Runtime::InteropServices::Marshal::Copy(IntPtr(&str[0]), response, 0, str.size());
ClientSocket->GetStream()->Write(response, 0, response->Length);
Monitor::Enter(sync);
frames->pop();
Monitor::Exit(sync);
}
catch(Exception^ ex)
{
Console::WriteLine("Exception add frame: {0}", ex->Message);
}
}
}
}
// can only check if client alive by trying to send it something
bool StreamHandler::TestConnection()
{
try
{
array<Byte>^ tmp = gcnew array<Byte>(1);
ClientSocket->GetStream()->Write(tmp, 0, 0);
return ClientSocket->Connected;
}
catch (IOException^)
{
Console::WriteLine( "Conection is broken!");
return false;
}
catch(InvalidOperationException^)
{
Console::WriteLine( "Conection is broken!");
return false;
}
return false;
}
void StreamHandler::streamHeader()
{
try
{
ClientHandler* client = AppData::getInstance()->getClient();
if (!client) return;
String^ header = "<StaticData>\n";
header += gcnew String(AppData::getInstance()->getRigidBodyStaticData().c_str());
header += gcnew String(WorldManager::getInstance()->getWorldStaticData().c_str());
header += "</StaticData>\n";
array<Byte>^ response = ascii->GetBytes(header);
ClientSocket->GetStream()->Write(response, 0, response->Length);
}
catch(Exception^ ex)
{
Console::WriteLine("Exception stream header: {0}", ex->Message);
}
}
// main thread
// listens for commands from client
void StreamHandler::Process() {
// Incoming data from the client.
String^ data;
int bufferSize = ClientSocket->ReceiveBufferSize;
// Data buffer for incoming data.
array<Byte>^ bytes = gcnew array<Byte>(bufferSize);
if ( ClientSocket ) {
NetworkStream^ networkStream = ClientSocket->GetStream();
ClientSocket->ReceiveTimeout = 1000 ; // 1000 miliseconds
while ( ContinueProcess ) {
Array::Clear(bytes,0,bytes->Length);
try {
// this will throw a socketException if the client does not send
// any data within the ReceiveTimeout timeframe
int BytesRead = networkStream->Read(bytes, 0, bufferSize);
if ( BytesRead > 0 ) {
data = ascii->GetString(bytes, 0, BytesRead);
for (int i = 0; i < data->Length; i++)
{
switch (data[i])
{
case 'q':
ContinueProcess = false;
break;
case 's':
streamData = true;
break;
case 'd':
streamData = false;
break;
case 'h':
streamHeader();
break;
}
}
}
else
{
// client disconnected
if(!TestConnection())
break ;
}
}
catch ( SocketException^ ) {
Console::WriteLine( "Conection is broken!");
break ;
}
catch ( IOException^ ) {
if(!TestConnection())
break ;
} // Timeout
Thread::Sleep(200);
} // while ( ContinueProcess )
networkStream->Close();
ClientSocket->Close();
streamData = false;
}
} // Process()
}