main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include <SDL_events.h>
  2. #include <SDL_mouse.h>
  3. #include <SDL_mutex.h>
  4. #include <SDL_video.h>
  5. #include <asio/socket_base.hpp>
  6. #include <cstdint>
  7. #include <iostream>
  8. #include <condition_variable>
  9. #include <asio.hpp>
  10. #include <mutex>
  11. #include "ui.h"
  12. using asio::ip::udp;
  13. int32_t bytesToInt(const char *bytes) {
  14. int32_t result = uint8_t(bytes[0]) << 24 |
  15. uint8_t(bytes[1]) << 16 |
  16. uint8_t(bytes[2]) << 8 |
  17. uint8_t(bytes[3]) << 0;
  18. return result;
  19. }
  20. class Client {
  21. public:
  22. Client(struct Application *a, asio::io_context& ioContext, uint16_t udpPort)
  23. : app(a),
  24. socket(ioContext),
  25. udpSocket(ioContext, udp::endpoint(udp::v4(), udpPort)) {
  26. udpSocket.set_option(asio::socket_base::broadcast(true));
  27. // socket.connect(serverEndpoint);
  28. }
  29. void start() {
  30. udpReceive();
  31. // receiveHeader();
  32. }
  33. void disconnect() {
  34. socket.close();
  35. udpSocket.close();
  36. std::cout << "Disconnected from the server" << std::endl;
  37. }
  38. private:
  39. void udpReceive() {
  40. udpSocket.async_receive_from(
  41. asio::buffer(udpData, 256), senderEndpoint,
  42. [this](std::error_code ec, std::size_t length) {
  43. if (!ec && length > 0) {
  44. std::string msg(udpData, length);
  45. std::cout << "Received message: " << msg << std::endl;
  46. std::cout << "Sender IP: " << senderEndpoint.address().to_string() << std::endl;
  47. if (msg == "StreamControlTag") {
  48. std::cout << "Connect to server" << std::endl;
  49. asio::ip::tcp::endpoint serverEndpoint(senderEndpoint.address(), 8031);
  50. socket.connect(serverEndpoint);
  51. receiveHeader();
  52. }
  53. }
  54. }
  55. );
  56. }
  57. void receiveHeader() {
  58. std::cout << "start Received: Header" << std::endl;
  59. asio::async_read(socket, asio::buffer(headerBuffer), [this](std::error_code ec, std::size_t length) {
  60. if (!ec) {
  61. std::size_t messageLength = parseHeader();
  62. receiveMessage(messageLength);
  63. }
  64. });
  65. }
  66. std::size_t parseHeader() {
  67. return bytesToInt(headerBuffer.data());
  68. }
  69. void receiveMessage(std::size_t messageLength) {
  70. bodyBuffer.resize(messageLength);
  71. asio::async_read(socket, asio::buffer(bodyBuffer), [this](std::error_code ec, std::size_t length) {
  72. if (!ec) {
  73. std::cout << "Received: Message" << std::endl;
  74. parseMessage();
  75. receiveHeader(); // Continue to receive the next message
  76. }
  77. });
  78. }
  79. void parseMessage() {
  80. // Parse the received message according to the specified format
  81. std::size_t offset = 0;
  82. // Extracting the message components
  83. uint8_t messageType = (uint8_t)(bodyBuffer[offset]);
  84. offset += 1;
  85. if (messageType == 1) {
  86. int32_t gameRound = bytesToInt(&bodyBuffer[offset]);
  87. offset += 4;
  88. int32_t player1NameLength = bytesToInt(&bodyBuffer[offset]);
  89. offset += 4;
  90. std::string player1Name(&bodyBuffer[offset], player1NameLength);
  91. offset += player1NameLength;
  92. int32_t player2NameLength = bytesToInt(&bodyBuffer[offset]);
  93. offset += 4;
  94. std::string player2Name(&bodyBuffer[offset], player2NameLength);
  95. offset += player2NameLength;
  96. int32_t player1Score = bytesToInt(&bodyBuffer[offset]);
  97. offset += 4;
  98. int32_t player2Score = bytesToInt(&bodyBuffer[offset]);
  99. offset += 4;
  100. updateApplication(app, player1Name.c_str(), player2Name.c_str(), gameRound, player1Score, player2Score);
  101. }
  102. }
  103. private:
  104. struct Application *app;
  105. asio::ip::udp::socket udpSocket;
  106. udp::endpoint senderEndpoint;
  107. char udpData[256];
  108. asio::ip::tcp::socket socket;
  109. std::array<char, 4> headerBuffer;
  110. std::vector<char> bodyBuffer;
  111. };
  112. int main() {
  113. struct Application application, *app = &application;
  114. if (initApplication(app)) {
  115. printf("[Error]: initialize application failure: %s\n", app->errorString);
  116. return -1;
  117. }
  118. printf("width: %d, height: %d\n", app->winWidth, app->winHeight);
  119. std::mutex cv_mutex;
  120. std::condition_variable cv;
  121. asio::io_context ioContext;
  122. bool quit = false;
  123. bool network = true;
  124. SDL_Event event;
  125. std::thread clientThread = std::thread([&]() {
  126. try {
  127. constexpr uint16_t udpPort = 8032;
  128. /// asio::ip::tcp::endpoint serverEndpoint(asio::ip::make_address("192.168.1.80"), 8031);
  129. while (!quit) {
  130. std::unique_lock<std::mutex> lk(cv_mutex);
  131. std::cout << "start Client Loop" << std::endl;
  132. while (network) { // 使用 lambda 检查条件
  133. if (quit) break;
  134. // Client client(app, ioContext, serverEndpoint, udpPort);
  135. Client client(app, ioContext, udpPort);
  136. client.start();
  137. ioContext.run();
  138. client.disconnect();
  139. }
  140. cv.wait(lk, [&]{ return network; }); // 等待直到 network 为 true
  141. }
  142. } catch (const std::exception& e) {
  143. std::cerr << "Exception: " << e.what() << std::endl;
  144. }
  145. });
  146. SDL_ShowCursor(SDL_DISABLE);
  147. while (!quit) {
  148. SDL_LockMutex(app->mutex);
  149. while (SDL_PollEvent(&event)) {
  150. if (event.type == SDL_QUIT) {
  151. quit = true;
  152. network = true;
  153. } else if (event.type == SDL_MOUSEBUTTONDOWN) {
  154. if (event.button.button == SDL_BUTTON_RIGHT) {
  155. quit = true;
  156. network = true;
  157. }
  158. } else if (event.type == SDL_WINDOWEVENT) {
  159. if (event.type == SDL_WINDOWEVENT_FOCUS_LOST) {
  160. quit = true;
  161. network = true;
  162. }
  163. } else if (event.type == SDL_KEYDOWN) {
  164. switch(event.key.keysym.scancode) {
  165. case SDL_SCANCODE_O:
  166. network = !network;
  167. if (network == false) {
  168. ioContext.stop();
  169. updateApplicationMode(app, 1);
  170. updateApplication(app, "", "", 0, 0, 0);
  171. } else {
  172. ioContext.restart();
  173. updateApplicationMode(app, 0);
  174. updateApplication(app, "", "", 0, 0, 0);
  175. cv.notify_all();
  176. }
  177. break;
  178. default:
  179. if (!network) {
  180. switch(event.key.keysym.scancode) {
  181. case SDL_SCANCODE_C:
  182. updateApplication(app, "", "", 0, 0, 0);
  183. break;
  184. case SDL_SCANCODE_S:
  185. updateApplication(app, "", "", app->roundNum, app->p2Num, app->p1Num);
  186. app->p1Win = 0;
  187. app->p2Win = 0;
  188. break;
  189. case SDL_SCANCODE_KP_1:
  190. if (app->p1Num == 99) {
  191. app->p1Num = 0;
  192. } else {
  193. ++app->p1Num;
  194. }
  195. updateApplication(app, "", "", app->roundNum, app->p1Num, app->p2Num);
  196. app->p1Win = 1;
  197. app->p2Win = 0;
  198. break;
  199. case SDL_SCANCODE_KP_2:
  200. if (app->p1Num > 0) {
  201. --app->p1Num;
  202. }
  203. updateApplication(app, "", "", app->roundNum, app->p1Num, app->p2Num);
  204. break;
  205. case SDL_SCANCODE_KP_3:
  206. updateApplication(app, "", "", app->roundNum, 0, app->p2Num);
  207. app->p1Win = 0;
  208. app->p2Win = 0;
  209. break;
  210. case SDL_SCANCODE_KP_4:
  211. if (app->p2Num == 99) {
  212. app->p2Num = 0;
  213. } else {
  214. ++app->p2Num;
  215. }
  216. updateApplication(app, "", "", app->roundNum, app->p1Num, app->p2Num);
  217. app->p1Win = 0;
  218. app->p2Win = 1;
  219. break;
  220. case SDL_SCANCODE_KP_5:
  221. if (app->p2Num > 0) {
  222. --app->p2Num;
  223. }
  224. updateApplication(app, "", "", app->roundNum, app->p1Num, app->p2Num);
  225. break;
  226. case SDL_SCANCODE_KP_6:
  227. updateApplication(app, "", "", app->roundNum, app->p1Num, 0);
  228. app->p1Win = 0;
  229. app->p2Win = 0;
  230. break;
  231. case SDL_SCANCODE_KP_7:
  232. if (app->roundNum == 99) {
  233. app->roundNum = 0;
  234. } else {
  235. ++app->roundNum;
  236. }
  237. updateApplication(app, "", "", app->roundNum, app->p1Num, app->p2Num);
  238. break;
  239. case SDL_SCANCODE_KP_8:
  240. if (app->roundNum > 0) {
  241. --app->roundNum;
  242. }
  243. updateApplication(app, "", "", app->roundNum, app->p1Num, app->p2Num);
  244. break;
  245. case SDL_SCANCODE_KP_9:
  246. updateApplication(app, "", "", 0, app->p1Num, app->p2Num);
  247. break;
  248. default:
  249. break;
  250. }
  251. }
  252. }
  253. }
  254. }
  255. if (app->updateRequire) {
  256. drawApplication(app);
  257. app->updateRequire = 0;
  258. }
  259. SDL_UnlockMutex(app->mutex);
  260. SDL_Delay(166);
  261. }
  262. // 停止客户端并等待线程结束
  263. ioContext.stop();
  264. cv.notify_all();
  265. if (clientThread.joinable())
  266. clientThread.join();
  267. termApplication(app);
  268. return 0;
  269. }