Skip to content

Instantly share code, notes, and snippets.

@pjc0247
Created September 13, 2015 14:35
Show Gist options
  • Save pjc0247/4fc9b7f78386e175e753 to your computer and use it in GitHub Desktop.
Save pjc0247/4fc9b7f78386e175e753 to your computer and use it in GitHub Desktop.
// Merona.Client.cpp.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include "stdafx.h"
#include <string>
#include <winsock2.h>
#include <windows.h>
#include <queue>
#include <mutex>
#include <atomic>
#pragma comment (lib, "ws2_32")
namespace merona {
namespace client {
#pragma pack (push, 1)
struct packet {
public:
int size;
int packet_id;
char channel[32];
packet() :
size(sizeof(this)),
packet_id(-1){
memset(channel, 0, sizeof(channel));
}
};
#pragma pack (pop)
class connection {
public:
void connect(const std::string &host, int port) {
}
void disconnect() {
closesocket(sock);
}
void worker() {
while (true) {
int size = 0;
if (!block_recv((char*)&size, sizeof(size)))
goto on_error;
char *buf = new char[size];
if (!block_recv(buf, size - sizeof(size)))
goto on_error;
}
on_error:
}
bool block_recv(char *dst, int size) {
int target_size = size;
int received = 0;
while (target_size < received) {
int len = recv(
sock,
dst + received, size - received, 0);
if (len <= 0)
return false;
received += len;
}
return true;
}
void enqueue(const packet &p) {
std::unique_lock<std::mutex> guard(q_mutex);
q.push(p);
q_size.fetch_add(1);
}
void update() {
if (q_size.load() == 0)
return;
int cnt = 0;
while (true) {
std::unique_lock<std::mutex> guard(q_mutex);
if (q.empty())
break;
cnt += 1;
}
q_size.fetch_sub(cnt);
}
private:
SOCKET sock;
std::queue<packet> q;
std::mutex q_mutex;
std::atomic<int> q_size;
};
}
}
int main()
{
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment