This has the odd quirk related to networkQueue()
This commit is contained in:
+18
-6
@@ -26,15 +26,12 @@ Lobby::Lobby() {
|
|||||||
serverList.push_back({"bar",{0,0}});
|
serverList.push_back({"bar",{0,0}});
|
||||||
serverList.push_back({"foobar",{0,0}});
|
serverList.push_back({"foobar",{0,0}});
|
||||||
|
|
||||||
if (!(queueThread = SDL_CreateThread(networkQueue, nullptr))) {
|
BeginQueueThread();
|
||||||
throw(runtime_error("Failed to create the network thread"));
|
|
||||||
}
|
|
||||||
|
|
||||||
BroadcastNetwork();
|
BroadcastNetwork();
|
||||||
}
|
}
|
||||||
|
|
||||||
Lobby::~Lobby() {
|
Lobby::~Lobby() {
|
||||||
SDL_KillThread(queueThread);
|
EndQueueThread();
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
cout << "leaving Lobby" << endl;
|
cout << "leaving Lobby" << endl;
|
||||||
#endif
|
#endif
|
||||||
@@ -99,7 +96,9 @@ void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
|||||||
BroadcastNetwork();
|
BroadcastNetwork();
|
||||||
}
|
}
|
||||||
else if (joinButton.MouseButtonUp(button) == Button::State::HOVER) {
|
else if (joinButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
//TODO: join a server
|
if (selectedServer) {
|
||||||
|
ConnectToServer(selectedServer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
else if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
SetNextScene(SceneList::MAINMENU);
|
SetNextScene(SceneList::MAINMENU);
|
||||||
@@ -196,4 +195,17 @@ void Lobby::PushServer(BroadcastResponse& bcast) {
|
|||||||
entry.name = bcast.name;
|
entry.name = bcast.name;
|
||||||
entry.address = netUtil->GetInPacket()->address;
|
entry.address = netUtil->GetInPacket()->address;
|
||||||
serverList.push_back(entry);
|
serverList.push_back(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::ConnectToServer(ServerEntry* server) {
|
||||||
|
/* _attempt_ to connect to a server
|
||||||
|
*/
|
||||||
|
if (!server) {
|
||||||
|
throw(runtime_error("No server received"));
|
||||||
|
}
|
||||||
|
Packet p;
|
||||||
|
p.type = PacketType::JOIN_REQUEST;
|
||||||
|
snprintf(p.joinRequest.playerHandle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
|
||||||
|
snprintf(p.joinRequest.playerAvatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar"));
|
||||||
|
netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet));
|
||||||
}
|
}
|
||||||
+1
-3
@@ -46,6 +46,7 @@ protected:
|
|||||||
int HandlePacket(Packet p);
|
int HandlePacket(Packet p);
|
||||||
void BroadcastNetwork();
|
void BroadcastNetwork();
|
||||||
void PushServer(BroadcastResponse&);
|
void PushServer(BroadcastResponse&);
|
||||||
|
void ConnectToServer(ServerEntry*);
|
||||||
|
|
||||||
//services
|
//services
|
||||||
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
|
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
|
||||||
@@ -61,9 +62,6 @@ protected:
|
|||||||
SDL_Rect listBox;
|
SDL_Rect listBox;
|
||||||
std::vector<ServerEntry> serverList;
|
std::vector<ServerEntry> serverList;
|
||||||
ServerEntry* selectedServer = nullptr;
|
ServerEntry* selectedServer = nullptr;
|
||||||
|
|
||||||
//threads
|
|
||||||
SDL_Thread* queueThread = nullptr;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,15 +5,22 @@
|
|||||||
|
|
||||||
#include "SDL/SDL_thread.h"
|
#include "SDL/SDL_thread.h"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
static SDL_sem* lock = SDL_CreateSemaphore(1);
|
static SDL_sem* lock = SDL_CreateSemaphore(1);
|
||||||
|
static SDL_Thread* queueThread = nullptr;
|
||||||
|
|
||||||
static std::deque<Packet> queue;
|
static std::deque<Packet> queue;
|
||||||
|
|
||||||
int networkQueue(void*) {
|
static bool running = false;
|
||||||
|
|
||||||
|
static int networkQueue(void*) {
|
||||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||||
for(;;) {
|
//this line is the fix for the quirk
|
||||||
|
// std::cout << "thread running" << std::endl;
|
||||||
|
while(running) {
|
||||||
SDL_SemWait(lock);
|
SDL_SemWait(lock);
|
||||||
while(netUtil->Receive()) {
|
while(netUtil->Receive()) {
|
||||||
Packet p;
|
Packet p;
|
||||||
@@ -23,6 +30,26 @@ int networkQueue(void*) {
|
|||||||
SDL_SemPost(lock);
|
SDL_SemPost(lock);
|
||||||
SDL_Delay(10);
|
SDL_Delay(10);
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BeginQueueThread() {
|
||||||
|
if (!(queueThread = SDL_CreateThread(networkQueue, nullptr))) {
|
||||||
|
throw(std::runtime_error("Failed to create the network thread"));
|
||||||
|
}
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EndQueueThread() {
|
||||||
|
running = false;
|
||||||
|
SDL_WaitThread(queueThread, nullptr);
|
||||||
|
queueThread = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void KillQueueThread() {
|
||||||
|
running = false;
|
||||||
|
SDL_KillThread(queueThread);
|
||||||
|
queueThread = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Packet peekNetworkPacket() {
|
Packet peekNetworkPacket() {
|
||||||
|
|||||||
@@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#include "packet_type.hpp"
|
#include "packet_type.hpp"
|
||||||
|
|
||||||
int networkQueue(void*);
|
void BeginQueueThread();
|
||||||
|
void EndQueueThread();
|
||||||
|
void KillQueueThread();
|
||||||
Packet peekNetworkPacket();
|
Packet peekNetworkPacket();
|
||||||
Packet popNetworkPacket();
|
Packet popNetworkPacket();
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ struct BroadcastResponse {
|
|||||||
|
|
||||||
struct JoinRequest {
|
struct JoinRequest {
|
||||||
PacketType type;
|
PacketType type;
|
||||||
|
char playerHandle[PACKET_STRING_SIZE];
|
||||||
|
char playerAvatar[PACKET_STRING_SIZE];
|
||||||
//TODO: player data
|
//TODO: player data
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -56,9 +56,7 @@ void ServerApplication::Init() {
|
|||||||
netUtil->Open(configUtil->Int("server.port"), sizeof(Packet));
|
netUtil->Open(configUtil->Int("server.port"), sizeof(Packet));
|
||||||
|
|
||||||
//create the threads
|
//create the threads
|
||||||
if (!(queueThread = SDL_CreateThread(networkQueue, nullptr))) {
|
BeginQueueThread();
|
||||||
throw(runtime_error("Failed to create the network thread"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//output the server information
|
//output the server information
|
||||||
cout << configUtil->String("server.name") << endl;
|
cout << configUtil->String("server.name") << endl;
|
||||||
@@ -86,7 +84,7 @@ void ServerApplication::Proc() {
|
|||||||
|
|
||||||
void ServerApplication::Quit() {
|
void ServerApplication::Quit() {
|
||||||
//close the threads
|
//close the threads
|
||||||
SDL_KillThread(queueThread);
|
EndQueueThread();
|
||||||
|
|
||||||
//clean up the services
|
//clean up the services
|
||||||
netUtil->Close();
|
netUtil->Close();
|
||||||
@@ -133,9 +131,9 @@ int ServerApplication::HandlePacket(Packet p) {
|
|||||||
// case PacketType::BROADCAST_RESPONSE:
|
// case PacketType::BROADCAST_RESPONSE:
|
||||||
// //
|
// //
|
||||||
// break;
|
// break;
|
||||||
// case PacketType::JOIN_REQUEST:
|
case PacketType::JOIN_REQUEST:
|
||||||
// //
|
HandleConnection(p.joinRequest);
|
||||||
// break;
|
break;
|
||||||
// case PacketType::JOIN_RESPONSE:
|
// case PacketType::JOIN_RESPONSE:
|
||||||
// //
|
// //
|
||||||
// break;
|
// break;
|
||||||
@@ -167,4 +165,8 @@ void ServerApplication::Broadcast(BroadcastRequest& bcast) {
|
|||||||
snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil->CString("server.name"));
|
snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil->CString("server.name"));
|
||||||
//TODO version information
|
//TODO version information
|
||||||
netUtil->Send(&netUtil->GetInPacket()->address, &p, sizeof(Packet));
|
netUtil->Send(&netUtil->GetInPacket()->address, &p, sizeof(Packet));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerApplication::HandleConnection(JoinRequest& request) {
|
||||||
|
//
|
||||||
}
|
}
|
||||||
@@ -48,6 +48,7 @@ private:
|
|||||||
//network loop
|
//network loop
|
||||||
int HandlePacket(Packet p);
|
int HandlePacket(Packet p);
|
||||||
void Broadcast(BroadcastRequest&);
|
void Broadcast(BroadcastRequest&);
|
||||||
|
void HandleConnection(JoinRequest&);
|
||||||
|
|
||||||
//services
|
//services
|
||||||
ConfigUtility* configUtil = nullptr;
|
ConfigUtility* configUtil = nullptr;
|
||||||
@@ -60,9 +61,6 @@ private:
|
|||||||
std::map<int, PlayerData> players;
|
std::map<int, PlayerData> players;
|
||||||
|
|
||||||
bool running = false;
|
bool running = false;
|
||||||
|
|
||||||
//threads
|
|
||||||
SDL_Thread* queueThread = nullptr;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user