Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Last active December 18, 2015 13:29
Show Gist options
  • Save Ratstail91/5790773 to your computer and use it in GitHub Desktop.
Save Ratstail91/5790773 to your computer and use it in GitHub Desktop.
I want to push a network packet onto the deque in networkQueue(), and pop the first packet in getPacket()
#include "network_queue.hpp"
#include "service_locator.hpp"
#include "udp_network_utility.hpp"
#include "SDL/SDL_thread.h"
#include <deque>
static SDL_sem* lock = SDL_CreateSemaphore(1);
static std::deque<Packet> queue;
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 popNetworkPacket() {
Packet p;
SDL_SemWait(lock);
if (queue.size() > 0) {
Packet p = queue[0];
queue.pop_front();
}
SDL_SemPost(lock);
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment