Skip to content
This repository has been archived by the owner on Jun 22, 2021. It is now read-only.

Commit

Permalink
Rewrite of GUI and tidy
Browse files Browse the repository at this point in the history
GUI elements are now a separate class to reduce code reuse
GUI now has clickable buttons for frequency on FC3 aircraft
  • Loading branch information
Ciaran Fisher committed Sep 3, 2015
1 parent bf4d432 commit 1b6716f
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 63 deletions.
116 changes: 112 additions & 4 deletions Plugin/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Plugin.h"
#include "ClientMetaData.h"
#include "RadioUpdate.h"
#include "RadioUpdateCommand.h"
#include "json/json.h"

#include <tchar.h>
Expand All @@ -34,7 +35,7 @@ static SimpleRadio::Plugin plugin;
namespace SimpleRadio
{
const char* Plugin::NAME = "DCS-SimpleRadio";
const char* Plugin::VERSION = "1.0.8";
const char* Plugin::VERSION = "1.0.9";
const char* Plugin::AUTHOR = "Ciribob - GitHub.com/ciribob";
const char* Plugin::DESCRIPTION = "DCS-SimpleRadio ";
const char* Plugin::COMMAND_KEYWORD = "sr";
Expand Down Expand Up @@ -69,8 +70,9 @@ namespace SimpleRadio
//read registry key
this->readSettings();


this->acceptor = thread(&Plugin::UDPListener, this);

this->udpCommandListener = thread(&Plugin::UDPCommandListener, this);
}

LPCWSTR Plugin::getConfigPath()
Expand Down Expand Up @@ -128,6 +130,11 @@ namespace SimpleRadio
{
this->acceptor.join();
}

if (this->udpCommandListener.joinable())
{
this->udpCommandListener.join();
}
}

void Plugin::setTeamSpeakFunctions(TS3Functions functions)
Expand Down Expand Up @@ -776,8 +783,7 @@ namespace SimpleRadio

SOCKADDR_IN SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
int ByteReceived = 5;
char ch = 'Y';
int ByteReceived = 0;

char ReceiveBuf[768]; //maximum UDP Packet Size
int BufLength = 768;
Expand Down Expand Up @@ -940,6 +946,108 @@ namespace SimpleRadio
// Back to the system
}

void Plugin::UDPCommandListener()
{
WSADATA wsaData;
SOCKET ReceivingSocket;

SOCKADDR_IN SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
int ByteReceived = 0;

char ReceiveBuf[768]; //maximum UDP Packet Size
int BufLength = 768;

struct ip_mreq mreq;

// Initialize Winsock version 2.2
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
this->teamspeak.logMessage("WSAStartup failed with error", LogLevel_ERROR, Plugin::NAME, 0);
}

struct sockaddr_in addr;

addr.sin_family = AF_INET;
addr.sin_port = htons(5060);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

ReceivingSocket = mksocket(&addr);

/* use setsockopt() to request that the kernel join a multicast group */

if (this->switchToUnicast == false)
{
// store this IP address in sa:
inet_pton(AF_INET, "239.255.50.10", &(mreq.imr_multiaddr.s_addr));

mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(ReceivingSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) {
this->teamspeak.logMessage("Error adding membership for Multicast - Check firewall!", LogLevel_ERROR, Plugin::NAME, 0);

}
}

while (this->listening)
{

if (recvfromTimeOutUDP(ReceivingSocket, 2, 0) > 0)
{

ByteReceived = recvfrom(ReceivingSocket, ReceiveBuf, BufLength,
0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
if (ByteReceived > 0)
{
try
{
ReceiveBuf[ByteReceived - 1] = 0; //add terminator

//only allow on FC3 aircraft
if (this->myClientData.hasRadio == false)
{
RadioUpdateCommand updateCommand = RadioUpdateCommand::deserialize(ReceiveBuf);

if (updateCommand.radio >= 0)
{
//reset all the radios
this->teamSpeakControlledClientData.radio[updateCommand.radio].frequency += updateCommand.freq;
}
}



}
catch (...)
{
//ERROR!?
}

memset(&ReceiveBuf[0], 0, sizeof(ReceiveBuf));
}

}
}


// When your application is finished receiving datagrams close the socket.
//printf("Server: Finished receiving. Closing the listening socket...\n");
if (closesocket(ReceivingSocket) != 0)
{
this->teamspeak.logMessage("Closesocket failed!", LogLevel_ERROR, Plugin::NAME, 0);
}
//printf("Server: closesocket() failed! Error code: %ld\n", WSAGetLastError());


// When your application is finished call WSACleanup.
//printf("Server: Cleaning up...\n");
if (WSACleanup() != 0)
{
//printf("Server: WSACleanup() failed! Error code: %ld\n", WSAGetLastError());
this->teamspeak.logMessage(" WSACleanup() failed!", LogLevel_ERROR, Plugin::NAME, 0);
}
// Back to the system
}

}

/* Unique name identifying this plugin */
Expand Down
4 changes: 4 additions & 0 deletions Plugin/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ namespace SimpleRadio
SOCKET mksocket(struct sockaddr_in *addr);

void UDPListener();

void UDPCommandListener();

volatile bool debug;

volatile bool listening;
std::thread acceptor;

std::thread udpCommandListener;

bool allowNonPlayers;

bool switchToUnicast;
Expand Down
3 changes: 3 additions & 0 deletions Plugin/Plugin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;PLUGIN_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)DSPFilters\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -256,6 +257,7 @@
<ClInclude Include="public_rare_definitions.h" />
<ClInclude Include="RadioInformation.h" />
<ClInclude Include="RadioUpdate.h" />
<ClInclude Include="RadioUpdateCommand.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="ts3_functions.h" />
<ClInclude Include="DCSPosition.h" />
Expand All @@ -267,6 +269,7 @@
<ClCompile Include="Plugin.cpp" />
<ClCompile Include="DCSPosition.cpp" />
<ClCompile Include="RadioUpdate.cpp" />
<ClCompile Include="RadioUpdateCommand.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Plugin.rc" />
Expand Down
6 changes: 6 additions & 0 deletions Plugin/Plugin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<ClInclude Include="RadioInformation.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="RadioUpdateCommand.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Plugin.cpp">
Expand All @@ -83,6 +86,9 @@
<ClCompile Include="RadioUpdate.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RadioUpdateCommand.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Plugin.rc">
Expand Down
35 changes: 35 additions & 0 deletions Plugin/RadioUpdateCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "RadioUpdateCommand.h"
#include "json/json.h"

namespace SimpleRadio
{

RadioUpdateCommand::RadioUpdateCommand()
{
}


RadioUpdateCommand::~RadioUpdateCommand()
{
}

const RadioUpdateCommand RadioUpdateCommand::deserialize(const std::string& document)
{
RadioUpdateCommand data;
Json::Reader reader;
Json::Value root;

bool success = reader.parse(document, root, false);
if (success == true)
{
data.freq = root["freq"].asDouble();
data.radio = root["radio"].asInt();
}
else
{
throw std::string("Failed to parse Update Command");
}

return data;
}
}
17 changes: 17 additions & 0 deletions Plugin/RadioUpdateCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include <string>
namespace SimpleRadio
{
class RadioUpdateCommand
{
public:

double freq;
int radio;

RadioUpdateCommand();
~RadioUpdateCommand();

static const RadioUpdateCommand deserialize(const std::string& document);
};
}
28 changes: 7 additions & 21 deletions RadioGui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,21 @@
mc:Ignorable="d"
ResizeMode="CanMinimize"
Topmost="True"

Title="DCS-SimpleRadio" Height="290" Width="120">


Title="DCS-SimpleRadio" Height="325" Width="120">

<WrapPanel>
<Label x:Name="radio1Label" Content="No Radio" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="10" Width="110" Margin="2"/>
<TextBlock x:Name="radio1Frequency" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Width="90"
Margin="2"
Background="Black" Foreground="Orange" Text="000.00AM"/>
<Ellipse x:Name="radio1Active" Fill="#FF9900" HorizontalAlignment="Left" Height="12" Stroke="Black" VerticalAlignment="Center" Width="12" Margin="2" />

<Slider x:Name="radio1Volume" Width="110" Margin="2" Maximum="100" IsEnabled="False" />
<local:RadioControlGroup x:Name="radio1" />

<Label x:Name="radio2Label" Content="No Radio" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="10" Width="110" Margin="2"/>
<local:RadioControlGroup x:Name="radio2"/>

<TextBlock x:Name="radio2Frequency" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Width="90"
Margin="2"
Background="Black" Foreground="Orange"><Run Text="000.00"/><Run Text="AM"/></TextBlock>
<Ellipse x:Name="radio2Active" Fill="#FF9900" HorizontalAlignment="Left" Height="12" Stroke="Black" VerticalAlignment="Center" Width="12" Margin="2" />
<local:RadioControlGroup x:Name="radio3"/>

<Slider x:Name="radio2Volume" Width="110" Margin="2" Maximum="100" IsEnabled="False"/>

<Label x:Name="radio3Label" Content="No Radio" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="10" Width="110" Margin="2"/>

<TextBlock x:Name="radio3Frequency" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Width="90"
Margin="2"
Background="Black" Foreground="Orange"><Run Text="000.00"/><Run Text="AM"/></TextBlock>
<Ellipse x:Name="radio3Active" Fill="#FF9900" HorizontalAlignment="Left" Height="12" Stroke="Black" VerticalAlignment="Center" Width="12" Margin="2" />

<Slider x:Name="radio3Volume" Width="110" Margin="2" Maximum="100" IsEnabled="False"/>


<Ellipse x:Name="statusIndicator" Fill="#FF9900" HorizontalAlignment="Left" Height="12" Stroke="Black" VerticalAlignment="Center" Width="12" Margin="2" />
<Label x:Name="statusLabel" Content="Not in Game" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="10" Width="90" Margin="2" Height="20" Padding="2"/>
Expand Down
Loading

0 comments on commit 1b6716f

Please sign in to comment.