Glitch with threading
This commit is contained in:
+20
-6
@@ -26,10 +26,17 @@ 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}});
|
||||||
|
|
||||||
|
queueThread = SDL_CreateThread(networkQueue, nullptr);
|
||||||
|
|
||||||
|
if (!queueThread) {
|
||||||
|
throw(runtime_error("Failed to create the network thread"));
|
||||||
|
}
|
||||||
|
|
||||||
BroadcastNetwork();
|
BroadcastNetwork();
|
||||||
}
|
}
|
||||||
|
|
||||||
Lobby::~Lobby() {
|
Lobby::~Lobby() {
|
||||||
|
SDL_KillThread(queueThread);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
cout << "leaving Lobby" << endl;
|
cout << "leaving Lobby" << endl;
|
||||||
#endif
|
#endif
|
||||||
@@ -44,7 +51,13 @@ void Lobby::FrameStart() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Lobby::Update(double delta) {
|
void Lobby::Update(double delta) {
|
||||||
Receive();
|
try {
|
||||||
|
//process all packets on the network queue
|
||||||
|
while(HandlePacket(popNetworkPacket()));
|
||||||
|
}
|
||||||
|
catch(exception& e) {
|
||||||
|
cerr << "Network Error: " << e.what() << endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lobby::FrameEnd() {
|
void Lobby::FrameEnd() {
|
||||||
@@ -126,11 +139,12 @@ void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
|
|||||||
//Utilities
|
//Utilities
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void Lobby::Receive() {
|
int Lobby::HandlePacket(Packet p) {
|
||||||
Packet p;
|
|
||||||
while(netUtil->Receive()) {
|
|
||||||
memcpy(&p, netUtil->GetInData(), sizeof(Packet));
|
|
||||||
switch(p.type) {
|
switch(p.type) {
|
||||||
|
case PacketType::NONE:
|
||||||
|
//DO NOTHING
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
case PacketType::PING:
|
case PacketType::PING:
|
||||||
//quick pong
|
//quick pong
|
||||||
p.type = PacketType::PONG;
|
p.type = PacketType::PONG;
|
||||||
@@ -169,7 +183,7 @@ void Lobby::Receive() {
|
|||||||
default:
|
default:
|
||||||
throw(runtime_error("Failed to recognize the packet type"));
|
throw(runtime_error("Failed to recognize the packet type"));
|
||||||
}
|
}
|
||||||
}
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lobby::BroadcastNetwork() {
|
void Lobby::BroadcastNetwork() {
|
||||||
|
|||||||
+6
-1
@@ -4,6 +4,7 @@
|
|||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
#include "service_locator.hpp"
|
#include "service_locator.hpp"
|
||||||
#include "packet_type.hpp"
|
#include "packet_type.hpp"
|
||||||
|
#include "network_queue.hpp"
|
||||||
|
|
||||||
#include "config_utility.hpp"
|
#include "config_utility.hpp"
|
||||||
#include "surface_manager.hpp"
|
#include "surface_manager.hpp"
|
||||||
@@ -11,6 +12,8 @@
|
|||||||
#include "button.hpp"
|
#include "button.hpp"
|
||||||
#include "raster_font.hpp"
|
#include "raster_font.hpp"
|
||||||
|
|
||||||
|
#include "SDL/SDL_thread.h"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -40,7 +43,7 @@ protected:
|
|||||||
void KeyUp(SDL_KeyboardEvent const&);
|
void KeyUp(SDL_KeyboardEvent const&);
|
||||||
|
|
||||||
//utilities
|
//utilities
|
||||||
void Receive();
|
int HandlePacket(Packet p);
|
||||||
void BroadcastNetwork();
|
void BroadcastNetwork();
|
||||||
void PushServer(BroadcastResponse&);
|
void PushServer(BroadcastResponse&);
|
||||||
|
|
||||||
@@ -58,6 +61,8 @@ protected:
|
|||||||
SDL_Rect listBox;
|
SDL_Rect listBox;
|
||||||
std::vector<ServerEntry> serverList;
|
std::vector<ServerEntry> serverList;
|
||||||
ServerEntry* selectedServer = nullptr;
|
ServerEntry* selectedServer = nullptr;
|
||||||
|
|
||||||
|
SDL_Thread* queueThread = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,15 +1,37 @@
|
|||||||
#include "network_queue.hpp"
|
#include "network_queue.hpp"
|
||||||
|
#include "service_locator.hpp"
|
||||||
|
|
||||||
|
#include "udp_network_utility.hpp"
|
||||||
|
|
||||||
#include "SDL/SDL_thread.h"
|
#include "SDL/SDL_thread.h"
|
||||||
|
|
||||||
#include <deque>
|
#include <deque>
|
||||||
|
|
||||||
|
static SDL_sem* lock = SDL_CreateSemaphore(1);
|
||||||
|
|
||||||
static std::deque<Packet> queue;
|
static std::deque<Packet> queue;
|
||||||
|
|
||||||
int networkQueue(void*) {
|
int networkQueue(void*) {
|
||||||
//
|
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||||
|
Packet p;
|
||||||
|
for(;;) {
|
||||||
|
SDL_SemWait(lock);
|
||||||
|
while(netUtil->Receive()) {
|
||||||
|
memcpy(&p, netUtil->GetInData(), sizeof(Packet));
|
||||||
|
queue.push_back(p);
|
||||||
|
}
|
||||||
|
SDL_SemPost(lock);
|
||||||
|
SDL_Delay(10);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Packet getPacket() {
|
Packet popNetworkPacket() {
|
||||||
//
|
SDL_SemWait(lock);
|
||||||
|
Packet p;
|
||||||
|
if (queue.size() > 0) {
|
||||||
|
Packet p = queue[0];
|
||||||
|
queue.pop_front();
|
||||||
|
}
|
||||||
|
SDL_SemPost(lock);
|
||||||
|
return p;
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,6 @@
|
|||||||
#include "packet_type.hpp"
|
#include "packet_type.hpp"
|
||||||
|
|
||||||
int networkQueue(void*);
|
int networkQueue(void*);
|
||||||
Packet getPacket();
|
Packet popNetworkPacket();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user