client.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "client.hpp"
  2. #include "server.hpp"
  3. const SOCKET &Client::getSocket() const{
  4. return this->socket;
  5. }
  6. void Client::setSocket(SOCKET const &socket){
  7. this->socket = socket;
  8. }
  9. const sockaddr_in &Client::getSocketInfo(){
  10. return this->socketInfo;
  11. }
  12. const string &Client::getName() const{
  13. return this->name;
  14. }
  15. void Client::setName(string const &name){
  16. this->name = name;
  17. }
  18. const string &Client::getIP() const{
  19. return this->ip;
  20. }
  21. void Client::setIP(string const &ip){
  22. this->ip = ip;
  23. }
  24. const u_short &Client::getPort() const{
  25. return this->port;
  26. }
  27. void Client::setPort(int const &port){
  28. this->port = htons(port);
  29. }
  30. thread *Client::getWorkThread(){
  31. return this->workThread;
  32. }
  33. void Client::setWorkThread(thread *workThread){
  34. this->workThread = workThread;
  35. }
  36. Client::Client(): Client(0){}
  37. Client::Client(SOCKET const &socket){
  38. this->setSocket(socket);
  39. this->setName("Unknown");
  40. this->setIP("0.0.0.0");
  41. this->setPort(0);
  42. }
  43. Client::~Client(){
  44. closesocket(this->getSocket());
  45. this->getWorkThread()->join();
  46. delete this->getWorkThread();
  47. }
  48. Client::operator string(){
  49. return this->getIP() + " (" + to_string(this->getPort()) + ")";
  50. }
  51. void Client::startWorkThread(Server *server){
  52. char *clientIpAddress = inet_ntoa(this->getSocketInfo().sin_addr);
  53. int clientPort = ntohs(this->getSocketInfo().sin_port);
  54. if(strcmp("0", clientIpAddress) == 0 || clientPort == 0){
  55. cout << "No valid ip address and/or port! Disconnecting..." << endl;
  56. closesocket(this->getSocket());
  57. return;
  58. }
  59. this->setIP(clientIpAddress);
  60. this->setPort(clientPort);
  61. cout << "[New Connection] " << clientIpAddress << " (" << clientPort << ")" << endl;
  62. // do {
  63. // char *headerBuffer = new char[5];
  64. // int result = recv(this->getSocket(), headerBuffer, 5, MSG_WAITALL);
  65. // if (result > 0) {
  66. // unsigned int opcode = (unsigned char) headerBuffer[0]; // value: 0-255
  67. // const unsigned int dataLength = *(unsigned int *)(headerBuffer + 1);
  68. // cout << "[Header] We received " << result << " bytes in total." << endl;
  69. // cout << "Opcode is: " << opcode << " and dataLength (which will be send) is: " << dataLength << endl;
  70. // char *dataBuffer = new char[dataLength];
  71. // int result = recv(this->getSocket(), dataBuffer, dataLength, MSG_WAITALL);
  72. // if(result > 0){
  73. // cout << "[Data] We received " << result << "bytes in total." << endl;
  74. // if(opcode == 55){
  75. // cout << "ohhh opcode 55 received!" << endl;
  76. // cout << dataBuffer << endl;
  77. // }
  78. // // for(Client *client : server->getClients()){
  79. // // if(client->getSocket() != this->getSocket()){
  80. // // if(client->getSocket() != SOCKET_ERROR && client->getSocket() != INVALID_SOCKET){
  81. // // int sendResult = send(client->getSocket(), dataBuffer, result, 0);
  82. // // if (sendResult == SOCKET_ERROR) {
  83. // // cout << "send() Error: " << WSAGetLastError() << endl;
  84. // // }
  85. // // }
  86. // // }
  87. // // }
  88. // }
  89. // delete[] dataBuffer;
  90. // } else {
  91. // switch(WSAGetLastError()){
  92. // case WSAECONNRESET: {
  93. // cout << "Connection reset by peer." << endl; break;
  94. // }
  95. // case WSAECONNABORTED: {
  96. // cout << "Software caused connection abort." << endl; break;
  97. // }
  98. // case WSAESHUTDOWN: {
  99. // cout << "The client socket has been shut down." << endl; break;
  100. // }
  101. // case WSAETIMEDOUT: {
  102. // cout << "The connection has been dropped because of a network failure or because the peer system failed to respond." << endl; break;
  103. // }
  104. // }
  105. // closesocket(this->getSocket());
  106. // this->setSocket(SOCKET_ERROR);
  107. // }
  108. // delete[] headerBuffer;
  109. // } while (this->getSocket() != INVALID_SOCKET && this->getSocket() != SOCKET_ERROR);
  110. do {
  111. char dataBuffer[MSG_BUFFER_SIZE];
  112. int result = recv(this->getSocket(), dataBuffer, sizeof(dataBuffer), 0);
  113. if (result > 0) {
  114. char *token = strtok(dataBuffer, "\x01");
  115. if(token != NULL){
  116. if(strcmp(token, "msg") == 0){
  117. // do something
  118. cout << "cmd: " << token << endl;
  119. token = strtok(NULL, "\x01");
  120. cout << token << endl;
  121. }
  122. }
  123. // unsigned int opcode = (unsigned char) dataBuffer[0]; // value: 0-255
  124. // const unsigned int dataLength = *(unsigned int *)(dataBuffer + 1);
  125. // for(Client *client : server->getClients()){
  126. // if(client->getSocket() != this->getSocket()){
  127. // if(client->getSocket() != SOCKET_ERROR && client->getSocket() != INVALID_SOCKET){
  128. // int sendResult = send(client->getSocket(), dataBuffer, result, 0);
  129. // if (sendResult == SOCKET_ERROR) {
  130. // cout << "send() Error: " << WSAGetLastError() << endl;
  131. // }
  132. // }
  133. // }
  134. // }
  135. } else {
  136. switch(WSAGetLastError()){
  137. case WSAECONNRESET: {
  138. cout << "Connection reset by peer." << endl; break;
  139. }
  140. case WSAECONNABORTED: {
  141. cout << "Software caused connection abort." << endl; break;
  142. }
  143. case WSAESHUTDOWN: {
  144. cout << "The client socket has been shut down." << endl; break;
  145. }
  146. case WSAETIMEDOUT: {
  147. cout << "The connection has been dropped because of a network failure or because the peer system failed to respond." << endl; break;
  148. }
  149. }
  150. closesocket(this->getSocket());
  151. this->setSocket(SOCKET_ERROR);
  152. }
  153. } while (this->getSocket() != INVALID_SOCKET && this->getSocket() != SOCKET_ERROR);
  154. closesocket(this->getSocket());
  155. }