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