Browse Source

server crash when receiving messages, especially when message is empty. need investigation!

Ray-works 1 year ago
parent
commit
2b01103799
5 changed files with 107 additions and 80 deletions
  1. 23 80
      client.cpp
  2. 1 0
      client.hpp
  3. 55 0
      data.cpp
  4. 28 0
      data.hpp
  5. BIN
      server.exe

+ 23 - 80
client.cpp

@@ -76,92 +76,35 @@ void Client::startWorkThread(Server *server){
76 76
     this->setPort(clientPort);
77 77
 
78 78
     cout << "[New Connection] " << clientIpAddress << " (" << clientPort << ")" << endl;
79
-
80
-    // do {
81
-    //     char *headerBuffer = new char[5];
82
-    //     int result = recv(this->getSocket(), headerBuffer, 5, MSG_WAITALL);
83
-
84
-    //     if (result > 0) {
85
-    //         unsigned int opcode = (unsigned char) headerBuffer[0]; // value: 0-255 
86
-    //         const unsigned int dataLength = *(unsigned int *)(headerBuffer + 1);
87
-
88
-    //         cout << "[Header] We received " << result << " bytes in total." << endl;
89
-    //         cout << "Opcode is: " << opcode << " and dataLength (which will be send) is: " << dataLength << endl;
90
-
91
-    //         char *dataBuffer = new char[dataLength];
92
-
93
-    //         int result = recv(this->getSocket(), dataBuffer, dataLength, MSG_WAITALL);
94
-    //         if(result > 0){
95
-    //             cout << "[Data] We received " << result << "bytes in total." << endl;
96
-
97
-    //             if(opcode == 55){
98
-    //                 cout << "ohhh opcode 55 received!" << endl;
99
-    //                 cout << dataBuffer << endl;
100
-    //             }
101
-
102
-    //             // for(Client *client : server->getClients()){
103
-    //             //     if(client->getSocket() != this->getSocket()){
104
-    //             //         if(client->getSocket() != SOCKET_ERROR && client->getSocket() != INVALID_SOCKET){
105
-    //             //             int sendResult = send(client->getSocket(), dataBuffer, result, 0);
106
-    //             //             if (sendResult == SOCKET_ERROR) {
107
-    //             //                 cout << "send() Error: " << WSAGetLastError() << endl;
108
-    //             //             }
109
-    //             //         }
110
-    //             //     }
111
-    //             // }
112
-    //         }
113
-    //         delete[] dataBuffer;
114
-    //     } else  {
115
-    //         switch(WSAGetLastError()){
116
-    //             case WSAECONNRESET: {
117
-    //                 cout << "Connection reset by peer." << endl; break;
118
-    //             }
119
-    //             case WSAECONNABORTED: {
120
-    //                 cout << "Software caused connection abort." << endl; break;
121
-    //             }
122
-    //             case WSAESHUTDOWN: {
123
-    //                 cout << "The client socket has been shut down." << endl; break;
124
-    //             }
125
-    //             case WSAETIMEDOUT: {
126
-    //                 cout << "The connection has been dropped because of a network failure or because the peer system failed to respond." << endl; break;
127
-    //             }
128
-    //         }
129
-
130
-    //         closesocket(this->getSocket());
131
-    //         this->setSocket(SOCKET_ERROR);
132
-    //     }
133
-        
134
-    //     delete[] headerBuffer;
135
-    // } while (this->getSocket() != INVALID_SOCKET && this->getSocket() != SOCKET_ERROR);
136
-
137 79
     do {
138 80
         char dataBuffer[MSG_BUFFER_SIZE];
81
+        memset((void *)dataBuffer, 0, MSG_BUFFER_SIZE);
139 82
         int result = recv(this->getSocket(), dataBuffer, sizeof(dataBuffer), 0);
140 83
         if (result > 0) {
141
-            char *token = strtok(dataBuffer, "\x01");
142
-            if(token != NULL){
143
-                if(strcmp(token, "msg") == 0){
144
-                    // do something
145
-                    cout << "cmd: " << token << endl;
146
-                
147
-                    token = strtok(NULL, "\x01");
148
-                    cout << token << endl;
149
-                }                
84
+            cout << "content of dataBuffer: " << dataBuffer << endl;
85
+            Data *data = new Data(dataBuffer);
86
+
87
+            if(strcmp(data->getCmd(), "su") == 0){
88
+                string name = data->getContext();
89
+                this->setName(name);
90
+            }
91
+
92
+            if(strcmp(data->getCmd(), "msg") == 0){
93
+                for(Client *client : server->getClients()){
94
+                    if(client->getSocket() != this->getSocket()){
95
+                        if(client->getSocket() != SOCKET_ERROR && client->getSocket() != INVALID_SOCKET){
96
+                            string msg = this->getName() + ": " + data->getContext();
97
+                            cout << msg << endl;
98
+                            int sendResult = send(client->getSocket(), msg.c_str(), msg.size(), 0);
99
+                            if(sendResult == SOCKET_ERROR){
100
+                                cout << "send() Error: " << WSAGetLastError() << endl;
101
+                            }
102
+                        }
103
+                    }
104
+                }
150 105
             }
151 106
 
152
-            // unsigned int opcode = (unsigned char) dataBuffer[0]; // value: 0-255 
153
-            // const unsigned int dataLength = *(unsigned int *)(dataBuffer + 1);
154
-
155
-            // for(Client *client : server->getClients()){
156
-            //     if(client->getSocket() != this->getSocket()){
157
-            //         if(client->getSocket() != SOCKET_ERROR && client->getSocket() != INVALID_SOCKET){
158
-            //             int sendResult = send(client->getSocket(), dataBuffer, result, 0);
159
-            //             if (sendResult == SOCKET_ERROR) {
160
-            //                 cout << "send() Error: " << WSAGetLastError() << endl;
161
-            //             }
162
-            //         }
163
-            //     }
164
-            // }
107
+            delete data;
165 108
         } else  {
166 109
             switch(WSAGetLastError()){
167 110
                 case WSAECONNRESET: {

+ 1 - 0
client.hpp

@@ -7,6 +7,7 @@
7 7
 #include <Ws2tcpip.h>
8 8
 #include <iostream>
9 9
 #include <thread>
10
+#include "data.hpp"
10 11
 #define MSG_BUFFER_SIZE 4096
11 12
 using namespace std;
12 13
 

+ 55 - 0
data.cpp

@@ -0,0 +1,55 @@
1
+#include "data.hpp"
2
+char *Data::getBuffer(){
3
+    return this->buffer;
4
+}
5
+
6
+void Data::setBuffer(char *buffer){
7
+    this->buffer = buffer;
8
+}
9
+
10
+const char *Data::getCmd() const{
11
+    return this->cmd;
12
+}
13
+
14
+void Data::setCmd(char *cmd){
15
+    this->cmd = cmd;
16
+}
17
+
18
+const char *Data::getContext() const{
19
+    return this->context;
20
+}
21
+
22
+void Data::setContext(char *context){
23
+    this->context = context;
24
+}
25
+
26
+Data::Data(): Data(new char[0]){}
27
+
28
+Data::Data(char *buffer){
29
+    this->setBuffer(buffer);
30
+    this->parse();
31
+}
32
+
33
+Data::~Data(){
34
+    delete this->getBuffer();
35
+    delete this->getCmd();
36
+    delete this->getContext();
37
+}
38
+
39
+void Data::parse(){
40
+    char *token = strtok(this->getBuffer(), "\x01");
41
+
42
+    if(token != NULL){
43
+        char *cmd = new char[strlen(token)];
44
+        strcpy(cmd, token);
45
+        this->setCmd(token);
46
+
47
+        token = strtok(NULL, "\x01");
48
+        if(token != NULL){
49
+            char *context = new char[strlen(token)];
50
+            strcpy(context, token);
51
+            this->setContext(context);
52
+        }
53
+    }
54
+    delete token;
55
+}

+ 28 - 0
data.hpp

@@ -0,0 +1,28 @@
1
+#pragma once
2
+#include <string>
3
+#include <Ws2tcpip.h>
4
+using namespace std;
5
+class Data {
6
+    private:
7
+        char *buffer;
8
+        char *cmd;
9
+        char *context;
10
+
11
+    public:
12
+        char *getBuffer();
13
+        void setBuffer(char *buffer);
14
+        
15
+        const char *getCmd() const;
16
+        void setCmd(char *cmd);
17
+
18
+        const char *getContext() const;
19
+        void setContext(char *context);
20
+
21
+        Data();
22
+        Data(char *buffer);
23
+        Data(Data &) = delete;
24
+        Data(Data &&) = delete;
25
+        ~Data();
26
+
27
+        void parse();
28
+};

BIN
server.exe