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 <Random.h>
 #include "HttpRequest.h"
 #include <iostream>
 
@@ -32,6 +31,7 @@ __declspec( dllexport ) WebSocketClient::WebSocketClient( const char *path, cons
     this->host = host;
     this->port = port;
     lastPingFrame = 0;
+    nextClose = 0;
 }
 
 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',
         'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0 };
     __int64 numKeyChars = strlen( allowedKeyChars );
-    RandomGenerator gen;
     if( !klient )
     {
         Text message = "GET ";
@@ -130,10 +129,10 @@ __declspec( dllexport ) bool WebSocketClient::send( __int64 size, const char *da
     f.dataLength = size;
     f.data = new char[ 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();
     queue->add( f );
     c.unlock();
@@ -232,9 +231,44 @@ __declspec( dllexport ) void WebSocketClient::thread()
                 }
                 first = 0;
             } 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 );
-            delete[] m.data;
+            }
         }
         else
         {
@@ -319,6 +353,21 @@ __declspec( dllexport ) void WebSocketClient::thread()
                 delete[] f.data;
                 c.lock();
                 queue->remove( 0 );
+                if( f.opcode == 0x8 )
+                {
+                    if( nextClose )
+                    {
+                        c2.lock();
+                        klient->trenne();
+                        klient = klient->release();
+                        c2.unlock();
+                        return;
+                    }
+                    else
+                    {
+                        nextClose = 1;
+                    }
+                }
             }
             c.unlock();
         }

+ 3 - 0
Network/WebSocket.h

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