-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork_System.cpp
178 lines (168 loc) · 4.84 KB
/
Network_System.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
//-----------------------------------------------------------------
#include "global.h"
//-----------------------------------------------------------------
///*****************************************************************
//Network_System - CONSTRUCTORS
///*****************************************************************
Network_System::Network_System(void)
{
//-------------------------------------------------------------
///Send new track info?
bNewTrack = false;
//Interface changes
bInterfaceUpdate = true;
bSpectrum_Reset = true;
///Connection
socket.setBlocking(false);
listener.setBlocking(false);
ServerPort = 50001;
State = 0;
//-------------------------------------------------------------
}
Network_System::~Network_System(void)
{
//-------------------------------------------------------------
//-------------------------------------------------------------
}
///*****************************************************************
//Network_System - StopServer
///*****************************************************************
void Network_System::StopServer(void)
{
//-------------------------------------------------------------
//Disconnect Network
listener.close();
socket.disconnect();
//-------------------------------------------------------------
}
///*****************************************************************
//Network_System - Update
///*****************************************************************
void Network_System::Update(void)
{
//-------------------------------------------------------------
//idle
if (State == 0)
{
//Do absolutley Nothing
}
//-------------------------------------------------------------
//Wait for connection
else if (State == 1)
{
// Listen to the given port for incoming connections
if (listener.listen(ServerPort) != sf::Socket::Done)
{
}
// Wait for a connection
if (listener.accept(socket) != sf::Socket::Done)
{
}
//Check for Connection and obtain IP from client
if (socket.getRemoteAddress() != sf::IpAddress::None)
{
//Ip obtained, connected
if (cBass.Stream_State != BASS_ACTIVE_STOPPED)
{
bNewTrack = true;
}
bInterfaceUpdate = true;
bSpectrum_Reset = true;
State = 2;
}
}
//-------------------------------------------------------------
//Connection Establised
else if (State == 2)
{
SendRecieveData();
}
//-------------------------------------------------------------
//Disconnect
else if (State == -1)
{
StopServer();
State = 0;
}
//-------------------------------------------------------------
}
///*****************************************************************
///Network_System - SendRecieveData
///*****************************************************************
void Network_System::SendRecieveData(void)
{
//-------------------------------------------------------------
Packet_Send.clear();
Packet_Recieve.clear();
Socket_Status = socket.receive(Packet_Recieve);
//-------------------------------------------------------------
//Monitor connection
if (Socket_Status == sf::Socket::Status::Disconnected)
{
State = -1;
}
else
{
//-------------------------------------------------------------
//Recieve
while (Packet_Recieve.getDataSize() > 0) //Always use latest packet
{
//null
bool bTest = false;
Packet_Recieve >> bTest;
Packet_Recieve.clear();
Socket_Status = socket.receive(Packet_Recieve);
}
//-------------------------------------------------------------
//Send
/////Update Rate (Oe2 logic loop target)
//Packet_Send << float(updateTimerSpeed);
//Interface changes
Packet_Send << bInterfaceUpdate;
if (bInterfaceUpdate)
{
//Spectrum Falloff speed
Packet_Send << cBass.fSpectrum_Falloff_TendTo;
//Spectrum Colours
Packet_Send << int(gSpectrum_R);
Packet_Send << int(gSpectrum_G);
Packet_Send << int(gSpectrum_B);
bInterfaceUpdate = false;
}
//Spectrum Reset
Packet_Send << bSpectrum_Reset;
bSpectrum_Reset = false;
//Play state
Packet_Send << int(cBass.Stream_State);
if (cBass.Stream_State > 0) // 0 = stopped | 1 = playing | 3 = paused
{
///Play Duration
Packet_Send << float(cBass.Current_Position);
///FFT's
for (int i = 0; i < MAX_FFT_USED; i++)
{
Packet_Send << cBass.FFT[i];
}
///Output Channels
Packet_Send << cBass.Output_Level_L;
Packet_Send << cBass.Output_Level_R;
}
///Send new track info?
Packet_Send << bNewTrack;
if (bNewTrack)
{
char Temp[128] = { 0 };
strcpy_s(Temp, gPlaylist_Entry[gPlay_CurrentId].artist.c_str());
Packet_Send << Temp;
strcpy_s(Temp, gPlaylist_Entry[gPlay_CurrentId].title.c_str());
if (gPlaylist_Entry[gPlay_CurrentId].title == "No Title")
{
strcpy_s(Temp, gPlaylist_Entry[gPlay_CurrentId].displayname.c_str());
}
Packet_Send << Temp;
bNewTrack = false;
}
socket.send(Packet_Send);
}
//-------------------------------------------------------------
}