diff --git a/MC/api/core/Client.cpp b/MC/api/core/Client.cpp index 71157db..52a82db 100644 --- a/MC/api/core/Client.cpp +++ b/MC/api/core/Client.cpp @@ -9,11 +9,8 @@ #include "Response.h" -Client::Client(const std::string& ip, int port) { - std::thread thread([&]() { - init(ip, port); - }); - thread.join(); +Client::Client(const std::string&& ip, int port) : ip(ip), port(port) { + } void Client::init(const std::string &ip, int port) { @@ -51,10 +48,15 @@ sockaddr_in Client::addr(const std::string &ip, int port) { } void Client::run() { - event_base_dispatch(base); + std::thread thread([&]() { + init(ip, port); + printf("111"); + event_base_loop(base, EVLOOP_NO_EXIT_ON_EMPTY); + printf("111"); + }); + thread.detach(); } - Client::~Client() { bufferevent_free(bev); event_base_free(base); @@ -93,6 +95,7 @@ void Client::readcb(struct bufferevent *bev, void *ctx) { void Client::writecb(struct bufferevent *bev, void *ctx) { printf("[write]: %p\n", ctx); + fflush(stdout); } void Client::eventcb(struct bufferevent *bev, short what, void *ctx) { @@ -107,6 +110,7 @@ void Client::eventcb(struct bufferevent *bev, short what, void *ctx) { else if (what == BEV_EVENT_CONNECTED) { printf("[BEV_EVENT_ERROR]\n"); } + fflush(stdout); } diff --git a/MC/api/core/Client.h b/MC/api/core/Client.h index 889f38b..d12e841 100644 --- a/MC/api/core/Client.h +++ b/MC/api/core/Client.h @@ -12,7 +12,7 @@ class Client { public: - Client(const std::string& ip, int port); + Client(const std::string&& ip, int port); virtual ~Client(); void run(); private: @@ -30,9 +30,11 @@ public: static void eventcb(struct bufferevent *bev, short what, void *ctx); private: - event_base* base; - bufferevent* bev; - event* ev; + event_base* base{}; + bufferevent* bev{}; + event* ev{}; + std::string ip; + int port; }; diff --git a/MC/api/main.cpp b/MC/api/main.cpp index ed74bdf..82a2c12 100644 --- a/MC/api/main.cpp +++ b/MC/api/main.cpp @@ -2,126 +2,16 @@ // Created by dongl on 23-4-15. // -//#include "core/Client.h" -//#include "user/UserOperation.h" -// -//int main() { -// auto client = Client("127.0.0.1", 9999); -// client.run(); -// -// auto user = new UserOperation(&client); -// user->login("783556037", "Aa316216"); -// -// -//} +#include "core/Client.h" +#include "user/UserOperation.h" +int main() { + Client* client = new Client("127.0.0.1", 9999); + client->run(); + printf("client: %p\n", client); -// -// Created by dongl on 23-4-15. -// - -#include -#include -#include "Request.h" -#include "Response.h" -#include "event2/event.h" -#include "event2/bufferevent.h" - -void client_read_cb(struct bufferevent *bev, void *ctx); -void client_write_cb(struct bufferevent *bev, void *ctx); -void event_cb(struct bufferevent *bev, short what, void *ctx); - -int main(int argc, char **argv) { - event_base *base = event_base_new(); - bufferevent* bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE); - sockaddr_in c_sin = {0}; - memset(&c_sin, 0, sizeof(c_sin)); - c_sin = { - AF_INET, - htons(9999), - - }; - evutil_inet_pton(AF_INET, "127.0.0.1", &c_sin.sin_addr.s_addr); - - - bufferevent_setcb(bev, client_read_cb, client_write_cb, event_cb, nullptr); - bufferevent_enable(bev, EV_READ | EV_WRITE); - int ret = bufferevent_socket_connect(bev, (sockaddr *) &c_sin, sizeof(c_sin)); - if (ret == 0) { - printf("connected\n"); - fflush(stdout); - } - - auto request = new Request(mp::MP_REQUEST_LOGIN, mp::MP_REQUEST_LOGIN_ACCOUNT, "783556037", "Aa316216"); - auto packet = request->packet(); - - bufferevent_write(bev, packet.c_str(), packet.size()); - bufferevent_write(bev, packet.c_str(), packet.size()); - - delete request; - - uint8_t l; - memcpy(&l, packet.c_str(), 1); - memcpy((void *) packet.c_str(), packet.c_str() + 1, packet.size() - 1); - - char h[256]; - memcpy(h, packet.c_str(), l); - char b[257]; - strncpy(b, packet.c_str() + strlen(h), packet.size() - strlen(h)); - - fflush(stdout); - event_base_dispatch(base); - event_base_free(base); + auto user = new UserOperation(client); + user->login("783556037", "Aa316216"); } -void client_read_cb(struct bufferevent *bev, void *ctx) { - printf("[read]: "); - int i = 0; - while (i < 3) { - i++; - // read L 读包长度 - uint8_t packetLen; - size_t len = bufferevent_read(bev, &packetLen, 1); - // read V 读包头 - char data_h[256] = {0}; - bufferevent_read(bev, data_h, packetLen); - - auto mph = std::make_shared(mp::mph()); - mph->ParseFromString(data_h); - printf("mph->mpb_size: %d\n", mph->mpb_size()); - - - // read V 读包体 包头内含有包体长度 - char data_b[256] = {0}; - bufferevent_read(bev, data_b, mph->mpb_size()); - printf("data_b: %s\n", data_b); - - mp::response* resp = new mp::response(); - resp->ParseFromString(data_b); - printf("%s\n", resp->sri().sri_msg().c_str()); - } - - fflush(stdout); -} - -void client_write_cb(struct bufferevent *bev, void *ctx) { -// Bytef data[1024] = {0}; -// uint32_t len = login(2725096176, "Aa316216", data, sizeof(data)); -// bufferevent_write(bev, data, len); -// sleep(3); -} - -void event_cb(struct bufferevent *bev, short what, void *ctx) { - printf("[event]\n"); - - if (what == BEV_EVENT_ERROR) { - printf("[BEV_EVENT_ERROR]: %p\n", ctx); - } - else if (what == BEV_EVENT_EOF) { - printf("[BEV_EVENT_ERROR]: %s\n",/* strerror(EVUTIL_SOCKET_ERROR())*/ "xx"); - } - else if (what == BEV_EVENT_CONNECTED) { - printf("[BEV_EVENT_ERROR]\n"); - } -} \ No newline at end of file diff --git a/MC/gui/CMakeLists.txt b/MC/gui/CMakeLists.txt index b5dde3c..03eaeac 100644 --- a/MC/gui/CMakeLists.txt +++ b/MC/gui/CMakeLists.txt @@ -1,15 +1,15 @@ project(GUI) -include_directories(${CMAKE_SOURCE_DIR}/include/fltk) -link_directories(${CMAKE_SOURCE_DIR}/lib/fltk) +include_directories(${CMAKE_SOURCE_DIR}/include) +link_directories(${CMAKE_SOURCE_DIR}/lib) add_executable(GUI main.cpp) target_link_libraries(GUI fltk -# fltk_gl -# libfltk_forms.a -# libfltk_images.a -# libfltk_cairo.a + fltk_gl + libfltk_forms.a + libfltk_images.a + libfltk_cairo.a m X11 Xext pthread Xinerama Xfixes Xcursor Xft Xrender m fontconfig dl ) \ No newline at end of file