aboutsummaryrefslogtreecommitdiff
path: root/test/04Network/main.cpp
blob: 571708f049f9f92ca0cd7f663969e016def36b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <functional>
#include "jin.h"

using namespace std;
using namespace jin::core;
using namespace jin::graphics;
using namespace jin::input;
using namespace jin::audio;
using namespace jin::time;
using namespace jin::thread;
using namespace jin::net;

Timers timers;

void onEvent(jin::input::Event* e)
{
    static Game* game = Game::get();
    if (e->type == EventType::QUIT)
        game->stop();
}

void onUpdate(int ms)
{
    timers.update(ms);
}

void onDraw()
{

}

void netWorkThread(Thread* t)
{
    Thread::Variant v = t->demand(1);
    Socket* socket = (Socket*)v.pointer;
    char buffer[2048] = {0};
    while (true)
    {
        Socket* client = socket->accept();
        if (client != nullptr)
        {
            client->receive(buffer, sizeof(buffer));
            cout << buffer;
        }
    }
}

int main(int argc, char* argv[])
{
    Game* game = Game::get();
    Game::Setting setting;
    setting.eventHandler = onEvent;
    setting.updater = onUpdate;
    setting.drawer = onDraw;
    setting.loader = nullptr;
    game->init(&setting);

    Window* wnd = Window::get();
    Window::Setting wndSetting;
    wndSetting.width = 400;
    wndSetting.height = 300;
    wndSetting.title = "test";
    wndSetting.fps = 60;
    wndSetting.vsync = false;
    wndSetting.fullscreen = false;
    wndSetting.resizable = true;
    wnd->init(&wndSetting);

    Net::get()->init();

    // server
    Socket socket(SocketType::TCP, 6438);

    Thread t("Receive Network Data", netWorkThread);
    t.start();
    t.send(1, &socket);
    
    game->run();

    game->quit();
    wnd->quit();

    return 0;
}