Browse Source

Ping pong und exit frames vom WebSocket Protokoll implementiert

Kolja Strohm 5 years ago
parent
commit
a08a1a0391
2 changed files with 60 additions and 8 deletions
  1. 57 8
      Network/WebSocket.cpp
  2. 3 0
      Network/WebSocket.h

+ 57 - 8
Network/WebSocket.cpp

@@ -1,5 +1,4 @@
 #include "WebSocket.h"
 #include "WebSocket.h"
-#include <Random.h>
 #include "HttpRequest.h"
 #include "HttpRequest.h"
 #include <iostream>
 #include <iostream>
 
 
@@ -32,6 +31,7 @@ __declspec( dllexport ) WebSocketClient::WebSocketClient( const char *path, cons
     this->host = host;
     this->host = host;
     this->port = port;
     this->port = port;
     lastPingFrame = 0;
     lastPingFrame = 0;
+    nextClose = 0;
 }
 }
 
 
 WebSocketClient::~WebSocketClient()
 WebSocketClient::~WebSocketClient()
@@ -58,7 +58,6 @@ __declspec( dllexport ) bool WebSocketClient::connect()
         '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
         '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
         'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0 };
         'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0 };
     __int64 numKeyChars = strlen( allowedKeyChars );
     __int64 numKeyChars = strlen( allowedKeyChars );
-    RandomGenerator gen;
     if( !klient )
     if( !klient )
     {
     {
         Text message = "GET ";
         Text message = "GET ";
@@ -130,10 +129,10 @@ __declspec( dllexport ) bool WebSocketClient::send( __int64 size, const char *da
     f.dataLength = size;
     f.dataLength = size;
     f.data = new char[ f.dataLength ];
     f.data = new char[ f.dataLength ];
     memcpy( f.data, data, f.dataLength );
     memcpy( f.data, data, f.dataLength );
-    f.key[ 0 ] = 1;
-    f.key[ 1 ] = 2;
-    f.key[ 2 ] = 3;
-    f.key[ 3 ] = 4;
+    f.key[ 0 ] = (unsigned char)(gen.rand() * 256);
+    f.key[ 1 ] = (unsigned char)(gen.rand() * 256);
+    f.key[ 2 ] = (unsigned char)(gen.rand() * 256);
+    f.key[ 3 ] = (unsigned char)(gen.rand() * 256);
     c.lock();
     c.lock();
     queue->add( f );
     queue->add( f );
     c.unlock();
     c.unlock();
@@ -232,9 +231,44 @@ __declspec( dllexport ) void WebSocketClient::thread()
                 }
                 }
                 first = 0;
                 first = 0;
             } while( !m.fin );
             } while( !m.fin );
-            if( callback )
+            if( m.opcode == 0x9 )
+            {
+                m.opcode = 0xA;
+                m.key[ 0 ] = (unsigned char)( gen.rand() * 256 );
+                m.key[ 1 ] = (unsigned char)( gen.rand() * 256 );
+                m.key[ 2 ] = (unsigned char)( gen.rand() * 256 );
+                m.key[ 3 ] = (unsigned char)( gen.rand() * 256 );
+                queue->add( m );
+            }
+            else if( m.opcode != 0xA )
+            {
+                delete[] m.data;
+            }
+            else if( m.opcode == 0x8 )
+            {
+                if( nextClose )
+                {
+                    delete[] m.data;
+                    c2.lock();
+                    klient->trenne();
+                    klient = klient->release();
+                    c2.unlock();
+                    return;
+                }
+                else
+                {
+                    m.key[ 0 ] = (unsigned char)( gen.rand() * 256 );
+                    m.key[ 1 ] = (unsigned char)( gen.rand() * 256 );
+                    m.key[ 2 ] = (unsigned char)( gen.rand() * 256 );
+                    m.key[ 3 ] = (unsigned char)( gen.rand() * 256 );
+                    queue->add( m );
+                    nextClose = 1;
+                }
+            }
+            else if( callback )
+            {
                 callback( this, m.dataLength, m.data, m.opcode == 1 ? TEXT : BINARY );
                 callback( this, m.dataLength, m.data, m.opcode == 1 ? TEXT : BINARY );
-            delete[] m.data;
+            }
         }
         }
         else
         else
         {
         {
@@ -319,6 +353,21 @@ __declspec( dllexport ) void WebSocketClient::thread()
                 delete[] f.data;
                 delete[] f.data;
                 c.lock();
                 c.lock();
                 queue->remove( 0 );
                 queue->remove( 0 );
+                if( f.opcode == 0x8 )
+                {
+                    if( nextClose )
+                    {
+                        c2.lock();
+                        klient->trenne();
+                        klient = klient->release();
+                        c2.unlock();
+                        return;
+                    }
+                    else
+                    {
+                        nextClose = 1;
+                    }
+                }
             }
             }
             c.unlock();
             c.unlock();
         }
         }

+ 3 - 0
Network/WebSocket.h

@@ -3,6 +3,7 @@
 #include <functional>
 #include <functional>
 #include <Thread.h>
 #include <Thread.h>
 #include <Critical.h>
 #include <Critical.h>
+#include <Random.h>
 #include "Klient.h"
 #include "Klient.h"
 
 
 namespace Network
 namespace Network
@@ -39,6 +40,8 @@ namespace Network
             unsigned short port;
             unsigned short port;
             Critical c;
             Critical c;
             Critical c2;
             Critical c2;
+            RandomGenerator gen;
+            bool nextClose;
 
 
         public:
         public:
             // Erstellt ein neues Websocket
             // Erstellt ein neues Websocket