I tested in both Mac and Windows with the following (starting from published vanilla templates on both platforms):
template.cpp -
// Includes, namespace and prototypes
#include "template.h"
#include <string>
using namespace AGK;
app App;
unsigned int networkId;
std::string outputText;
std::string networkID;
void doOpen()
{
// open the network
networkId = agk::HostNetwork("NetworkName", "ClientName", 1025);
// convert the id to char*
char* id = agk::Str((int)networkId);
// make it a string
networkID = id;
// clean up
delete[] id;
// add to the message
outputText = "Network Opened new id=" + networkID + "\n" + outputText;
}
void doClose()
{
// close the network
agk::CloseNetwork(networkId);
networkId = 0;
// add to message
outputText = "Network Closed for id=" + networkID + "\n" + outputText;
// clear network id string
networkID = "";
}
// Begin app, called once at the start
void app::Begin( void )
{
outputText = "App started\n";
doOpen();
}
// Main loop, called every frame
void app::Loop ( void )
{
// check for button pressed
if (agk::GetPointerReleased() == 1)
{
if (networkId != 0) doClose();
else doOpen();
}
// output status
agk::Print(outputText.c_str());
agk::Sync();
}
// Called when the app ends
void app::End ( void )
{
}
At first, it appeared to work just fine, no crashes no matter how many times I clicked.
But when I tried in on the Mac, since it was started from Xcode it displayed output not seen in the app itself, it showed the following messages in the 'All Output' window each time the app opened the network connection:
Failed to bind listening socket
Failed to set broadcast packet address: 1
There were no messages on the close network operation. It is entirely possible that my Mac Mini isn't properly configured to be a host as AppGameKit sees it.
But, being curious, I rebuilt the Windows version in Debug configuration and ran the debugger.
The first open and close produced nothing unusual. Then I sped up the time between the open and close click and got the message (the first line appears a lot while stuff is happening:
The thread 'Win32 thread' (0x9bc) has exited with code 0 (0x0).
> Forcing a thread to terminate, this may cause a crash...The thread 'Win32 thread' (0x1748) has exited with code 0 (0x0).
This would appear to indicate that something wasn't right and that a crash was possible.
But I still couldn't get either one to actually crash.
However, the original Tier 1 version would reliably crash with only a very few clicks. That one did the close immediately after the open.
displayText$ = "Click to create and close a network." + Chr(10)
do
if (GetPointerReleased())
networkId = HostNetwork("TestNetwork", "ClientName", 1025)
CloseNetwork(networkId)
displayText$ = displayText$ + "Network " + Str(networkId) + " opened and closed." + Chr(10)
endif
Print(displayText$)
Sync()
loop
When I changed the Tier 1 to be like the Tier 2, I couldn't get it to crash.
displayText$ = "Click to create and close a network." + Chr(10)
networkID = 0
do
if (GetPointerReleased())
if networkID = 0
networkID = HostNetwork("TestNetwork", "ClientName", 1025)
displayText$ = "Network " + Str(networkId) + " opened" + Chr(10) + displayText$
else
CloseNetwork(networkID)
displayText$ = "Network " + Str(networkId) + " closed." + Chr(10) + displayText$
networkID = 0
endif
endif
Print(displayText$)
Sync()
loop
But, given the messages showing up in the debug logs, there does appear to be something strange. So, I will officially confirm a maybe bug.
Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master