Avionics
Dropship Simulator
RakNet.cpp
Go to the documentation of this file.
1 #include "RakNet.h"
2 #include "Extensions.h"
3 
4 #include "D:/Rise Base/RakNet/Source/EmailSender.h"
5 
6 #include <thread>
7 
8 #include "RakNetUserPackets.h"
9 
10 namespace Devices
11 {
12  void RakNet::Initialize(Logger* prmLogger, RakNetConfig* prmConfig, Bus* prmBus)
13  {
14  logger = prmLogger;
15  config = prmConfig;
16  bus = prmBus;
17 
18  rak4Peers = RakPeerInterface::GetInstance();
19 
20  SocketDescriptor socketdesc(static_cast<USHORT>(config->clientPort), config->hostAddress.c_str()); // INADDR_ANY
21 
22  int result = rak4Peers->Startup(config->maxPeer + 1, &socketdesc, 1);
23  if (result == StartupResult::RAKNET_STARTED)
24  {
25  rak4Peers->SetMaximumIncomingConnections(static_cast<USHORT>(config->maxPeer));
26  char msg[99];
27  sprintf_s(msg, 99, "RakNet::ctor Ready to accept peer connection(s) on port: %i", config->clientPort);
28  logger->Log(msg);
29  sprintf_s(msg, 99, "RakNet::ctor MTU size: %i", rak4Peers->GetMTUSize(serverC));
30  logger->Log(msg);
31  }
32  else
33  {
34  logger->Log("RakNet::ctor Failed to host for peer connections!", Logger::Error, result);
35  return;
36  }
37 
38  SendEmail("Avionics startup!");
39  }
40 
42  {
43  if (!config || !config->enabled) return;
44 
45  logger->Log("RakNet::dtor...");
46  if (rak4Peers && rak4Peers->IsActive())
47  {
48  rak4Peers->Shutdown(500, 0);
49  }
50  ::RakNet::RakPeerInterface::DestroyInstance(rak4Peers);
51  }
52 
53  void RakNet::FrameMove(float fElapsed)
54  {
55  static float timeSinceLastEmail = 60.0f;
56  timeSinceLastEmail += fElapsed;
57 
58  for (UINT i = 0; i < bus->commandStream.size(); i++)
59  {
60  Command command = bus->commandStream.at(i);
61  if (command.delay != 0.0f) continue;
62 
63  if (command.name == "SendEmail" && timeSinceLastEmail >= 60.0f)
64  {
65  SendEmail(command.svalue);
66  bus->commandStream.erase(bus->commandStream.begin() + i);
67  }
68  else if (command.name == "Display-Reset")
69  {
71  bus->commandStream.erase(bus->commandStream.begin() + i);
72  }
73  else if (command.name == "Display-Event")
74  {
75  SEvents cs;
76  cs.type = ID_USER_PACKET_ENUM + 3;
77  cs.eventType = command.ivalue;
78 
79  if (command.ivalue == 3) // stop music
80  bus->FlightFreeze = false;
81  else if (command.ivalue == 2) // start music
82  bus->FlightFreeze = true;
83 
84  SPacketSetting setting;
85  setting.bytes = sizeof(SEvents);
86  setting.priority = PacketPriority::HIGH_PRIORITY;
87  setting.reliability = PacketReliability::RELIABLE;
88  cs.Send(rak4Peers, setting);
89 
90  bus->commandStream.erase(bus->commandStream.begin() + i);
91  }
92  }
93 
94  if (rak4Peers)
95  {
96  while ((p = rak4Peers->Receive()) != nullptr)
97  {
98  if (p->data[0] == ID_NEW_INCOMING_CONNECTION)
99  {
100  char msg[199];
101  sprintf_s(msg, 199, "RakNet::RakNetMessageHandler ID_NEW_INCOMING_CONNECTION: %s", p->systemAddress.ToString());
102  logger->Log(msg);
103  displayC = p->systemAddress;
104  SendInitialData();
105  }
106  else if (p->systemAddress == displayC)
107  {
109  }
110  else if (p->systemAddress == serverC)
111  {
112  }
113  rak4Peers->DeallocatePacket(p);
114  }
115 
116  static float timeSinceControlUpdate = 0.0f;
117  timeSinceControlUpdate += fElapsed;
118  if (timeSinceControlUpdate >= 0.1f)
119  {
120  timeSinceControlUpdate = 0.0f;
121  // send certain data from bus?
123  SControlSurfaces cs;
124  cs.type = ID_USER_PACKET_ENUM + 0;
125  memcpy_s(cs.discreet, sizeof(cs.discreet), bus->DiscreetSurfacePostion, sizeof(bus->DiscreetSurfacePostion));
127  cs.weight = bus->GrossWeight();
128 
131 
132  SPacketSetting setting;
133  setting.bytes = sizeof(SControlSurfaces);
134  setting.priority = PacketPriority::MEDIUM_PRIORITY;
135  setting.reliability = PacketReliability::UNRELIABLE_SEQUENCED;
136  cs.Send(rak4Peers, setting);
137  }
138  }
139  }
140 
142  {
143  char msg[199];
144 
145  switch (p->data[0])
146  {
147  case ID_CONNECTION_LOST:
148  sprintf_s(msg, 199, "RakNet::RakNetMessageHandler ID_CONNECTION_LOST: %s", p->systemAddress.ToString());
149  logger->Log(msg);
150  displayC = UNASSIGNED_SYSTEM_ADDRESS;
151  break;
152  case ID_DISCONNECTION_NOTIFICATION:
153  sprintf_s(msg, 199, "RakNet::RakNetMessageHandler ID_DISCONNECTION_NOTIFICATION: %s", p->systemAddress.ToString());
154  logger->Log(msg);
155  displayC = UNASSIGNED_SYSTEM_ADDRESS;
156  break;
157  case ID_USER_PACKET_ENUM + 2:
159  //peer->Send((const char*)p->data, p->length, HIGH_PRIORITY, RELIABLE_ORDERED, 0, p->guid, true);
160 
161  SFromDisplay* cs = reinterpret_cast<SFromDisplay*>(p->data);
162  bus->AngleOfAttack = cs->aoa;
163  bus->IndicatedAirspeed = cs->ias;
164  bus->FlightPathAngle = cs->fpa;
165  // we need yaw rate or relative wind information left and right
166  bus->Location = cs->location;
168  //bus->HeadingMagnetic = cs->heading; /// @TODO need magnetic variation? GPS is TRUE, magnetic is deprecated
169  bus->HeadingTrue = cs->heading;
173 
174  bus->DisplayFps = cs->fps;
175 
176  // we have no idea where we are... except altitude (which is exact above a reference line)
177  // so we need lat/long ...
178 
179  break;
180  }
181  }
182 
183  //void RakNet::SendToPeer(SystemAddress dwTo, void* pData, DWORD dwSize, PacketReliability reliability, bool broadcast)
184  //{
185  // /// @todo got to be a better way with vectors and std::strings
186  // char newdata[32768]; // 28676
187  // newdata[0] = (unsigned char)ID_USER_PACKET_ENUM;
188  // memcpy(&newdata[1], pData, dwSize);
189  // rak4Peers->Send(newdata, dwSize + 1, MEDIUM_PRIORITY, reliability, newdata[0], dwTo, broadcast);
190  //}
191 
197  void RakNet::SendEmail(std::string subject, std::string body, int severity)
198  {
199  if (!config->sendEmails)
200  {
201  char msg[999];
202  sprintf_s(msg, 999, "RakNet::SendEmail Disabled, would have sent email: %s", subject.c_str());
203  logger->Log(msg);
204  return;
205  }
206 
207  char msg[999];
208  sprintf_s(msg, 999, "RakNet::SendEmail Sending message: %s", subject.c_str());
209  logger->Log(msg);
210 
211  std::thread first(&RakNet::AsyncEmailSender, this, subject, body, severity);
212  first.detach();
213  logger->Log("RakNet::SendEmail Detached as asynchronous thread...");
214  }
215 
216  void RakNet::AsyncEmailSender(std::string subject, std::string body, int severity) const
217  {
218  std::string tempTo = config->recipientEmail;
219  Library::StringUtils::Replace(tempTo, "%i", std::to_string(severity));
220 
221  EmailSender* emailSender = EmailSender::GetInstance();
222  const char* sendResult = emailSender->Send(
223  config->smtpServer.c_str(),
224  static_cast<USHORT>(config->smtpServerPort),
225  config->senderEmail.c_str(),
226  tempTo.c_str(),
227  config->senderName.c_str(),
228  config->recipientName.c_str(),
229  subject.c_str(),
230  body.c_str(),
231  nullptr,
232  config->useSsl,
233  config->password.c_str());
234  if (sendResult != nullptr)
235  {
236  char msg[999];
237  sprintf_s(msg, 999, "RakNet::AsyncEmailSender Send failed: %s", sendResult);
238  logger->Log(msg, Logger::Error);
239  }
240  else
241  {
242  logger->Log("RakNet::AsyncEmailSender Sent successfully!");
243  }
244  EmailSender::DestroyInstance(emailSender);
245  }
246 
248  {
249  SInitialData cs;
250  cs.type = ID_USER_PACKET_ENUM + 1;
251  cs.reset = true;
252 
253  SPacketSetting setting;
254  setting.bytes = sizeof(SInitialData);
255  setting.priority = PacketPriority::HIGH_PRIORITY;
256  setting.reliability = PacketReliability::RELIABLE;
257  cs.Send(rak4Peers, setting);
258  }
259 }
ID_USER_PACKET_ENUM + 2.
float EngineThrustOutput[enginesC]
(9) Thrust/power of each engine—primary flight crew reference;
Definition: Bus.h:55
void SendEmail(std::string subject, std::string body="", int severity=0)
Definition: RakNet.cpp:197
float PressureAltitude
Definition: Bus.h:40
void RakNetMessageHandlerPeer()
Definition: RakNet.cpp:141
SystemAddress displayC
Definition: RakNet.h:39
std::vector< Command > commandStream
Definition: Bus.h:20
Packet * p
Definition: RakNet.h:40
Definition: Logger.h:5
RakNetConfig * config
Definition: RakNet.h:34
Bus * bus
Definition: RakNet.h:35
char svalue[64]
Definition: Command.h:24
float IndicatedAirspeed
(3) Indicated Airspeed in km per second?
Definition: Bus.h:42
D3DXVECTOR3 sumOfForces
bool reset
eventually this will be location, orientation, etc
float PitchAttitude
(6) Pitch attitude;
Definition: Bus.h:49
okay, the portable keyboard numbers don&#39;t work like the outside keypad because the outside keypad is ...
Definition: Analog.cpp:3
ID_USER_PACKET_ENUM + 3.
void Initialize(Logger *, RakNetConfig *, Bus *)
Definition: RakNet.cpp:12
float HeadingTrue
(4) Heading–primary flight crew reference (if selectable, record discrete, true or magnetic); ...
Definition: Bus.h:44
std::string recipientEmail
Definition: RakNet.h:25
Definition: Bus.h:12
std::string name
command name
Definition: Command.h:11
D3DXVECTOR3 NormalAcceleration
(5) vertical (11) longitudinal (18) lateral
Definition: Bus.h:47
std::string smtpServer
Definition: RakNet.h:19
float RollAttitude
(7) Roll attitude;
Definition: Bus.h:51
std::string senderName
Definition: RakNet.h:21
float GrossWeight() const
pounds
Definition: Bus.h:281
float DiscreetSurfacePostion[4]
Definition: Bus.h:59
void AsyncEmailSender(std::string subject, std::string body="", int severity=0) const
Definition: RakNet.cpp:216
unsigned char type
float FlightPathAngle
Definition: Bus.h:275
bool FlightFreeze
Definition: Bus.h:290
std::string hostAddress
Definition: RakNet.h:27
void FrameMove(float)
Definition: RakNet.cpp:53
Definition: Command.h:5
void Send(RakNet::RakPeerInterface *rak4Peers, SPacketSetting setting) const
std::string recipientName
Definition: RakNet.h:24
float DisplayFps
Definition: Bus.h:296
ID_USER_PACKET_ENUM + 1.
float AngleOfAttack
Definition: Bus.h:150
void Log(const char *msg, Level level=Info, int errorCode=0)
These have to be in this order.
Definition: Logger.cpp:16
ID_USER_PACKET_ENUM + 0.
float engineThrustPounds[2]
D3DXVECTOR3 location
int ivalue
Definition: Command.h:21
std::string senderEmail
Definition: RakNet.h:22
Logger * logger
Definition: RakNet.h:33
std::string password
Definition: RakNet.h:23
PacketReliability reliability
float delay
wait number of seconds before executing command
Definition: Command.h:8
void SendInitialData() const
Definition: RakNet.cpp:247
static bool Replace(std::string &str, const std::string &from, const std::string &to)
Definition: Extensions.h:24
PacketPriority priority
RakPeerInterface * rak4Peers
Definition: RakNet.h:37
D3DXVECTOR3 Location
Definition: Bus.h:359
SystemAddress serverC
Definition: RakNet.h:38