Преглед изворни кода

Prüfung des Erhaltungs Servers hinzugefügt

Kolja Strohm пре 6 година
родитељ
комит
f5433105af

+ 146 - 0
TestWalker/ESClient.h

@@ -0,0 +1,146 @@
+#pragma once
+#include <Klient.h>
+#include <Thread.h>
+#include <iostream>
+
+class ESClient : public Framework::Thread
+{
+private:
+    Network::Klient *k;
+    int klientId;
+    bool abmel;
+    bool trenn;
+    unsigned short port;
+    char *ip;
+    char *key;
+    unsigned char keyLen;
+    int ref;
+
+public:
+    // Konstruktor
+    ESClient( int klientId, unsigned short port, char *ip, char *key, unsigned char keyLen )
+    {
+        k = 0;
+        this->klientId = klientId;
+        this->port = port;
+        this->ip = new char[ textLength( ip ) + 1 ];
+        memcpy( this->ip, ip, textLength( ip ) + 1 );
+        this->keyLen = keyLen;
+        this->key = new char[ keyLen ];
+        memcpy( this->key, key, keyLen );
+        abmel = 0;
+        trenn = 0;
+        ref = 1;
+    }
+    // Destruktor
+    ~ESClient()
+    {
+        delete[] key;
+        if( k )
+            abmelden();
+        delete[] ip;
+        warteAufThread( 5000 );
+        ende();
+        if( k )
+            k->release();
+    }
+    // nicht constant
+    bool verbinden()
+    {
+        if( k )
+            return 1;
+        k = new Network::Klient();
+        if( k->verbinde( port, ip ) )
+        {
+            k->sende( "\0", 1 ); // Verschlüsselung Aktivieren
+            if( k->sendeEncrypted( "\1", 1 ) )
+            {
+                k->sendeEncrypted( (char*)&klientId, 4 );
+                char serverReturn = 0;
+                k->getNachrichtEncrypted( &serverReturn, 1 );
+                if( serverReturn == 3 )
+                {
+                    char byte = 0;
+                    k->getNachrichtEncrypted( &byte, 1 );
+                    char *f = new char[ byte + 1 ];
+                    f[ byte ] = 0;
+                    k->getNachrichtEncrypted( f, byte );
+                    std::cerr << "error while identifyin client Erhaltung Server returned: " << f << "\n";
+                    delete[]f;
+                    k->sendeEncrypted( "\3", 1 );
+                    k->getNachrichtEncrypted( &serverReturn, 1 );
+                    k->trenne();
+                    return 0;
+                }
+                k->setSendeKey( (char*)key, keyLen );
+                k->setEmpfangKey( (char*)key, keyLen );
+                start();
+            }
+            else
+            {
+                std::cerr << "network error while sending to Erhaltung Server\n";
+                k = k->release();
+                return 0;
+            }
+        }
+        else
+        {
+            std::cerr << "network error while connecting to Erhaltung Server\n";
+            k = k->release();
+            return 0;
+        }
+        return 1;
+    }
+
+    void abmelden()
+    {
+        abmel = 1;
+        trenn = 1;
+    }
+
+    void trennen()
+    {
+        trenn = 1;
+    }
+
+    virtual void thread()
+    {
+        while( 1 )
+        {
+            char n = 0;
+            k->getNachrichtEncrypted( &n, 1 );
+            if( n != 1 )
+            {
+                Sleep( 250 );
+                continue;
+            }
+            if( trenn )
+            {
+                if( abmel )
+                {
+                    k->sendeEncrypted( "\1", 1 );
+                    k->getNachrichtEncrypted( &n, 1 );
+                }
+                k->trenne();
+                run = 0;
+                return;
+            }
+            else
+                k->sendeEncrypted( "\0", 1 );
+        }
+    }
+
+    // Reference Counting
+    ESClient *getThis()
+    {
+        ref++;
+        return this;
+    }
+
+    ESClient *release()
+    {
+        if( !--ref )
+            delete this;
+        return 0;
+    }
+};

+ 45 - 0
TestWalker/MSClient.h

@@ -1,7 +1,9 @@
 #pragma once
 #include <Klient.h>
 #include <iostream>
+#include <Text.h>
 #include "Keys.h"
+#include "ESClient.h"
 
 #define MAIN_SERVER_PORT 5225
 #define MAIN_SERVER_IP "94.130.27.12"
@@ -126,6 +128,49 @@ public:
         }
     }
 
+    ESClient *createErhaltungServerClient()
+    {
+        connect();
+        if( !k )
+        {
+            std::cerr << "no connection to Main Server\n";
+            return 0;
+        }
+        k->sendeEncrypted( "\6\x8", 2 );
+        char byte = 0;
+        k->getNachrichtEncrypted( &byte, 1 );
+        if( byte == 2 )
+        {
+            unsigned char lsIp[ 4 ];
+            k->getNachrichtEncrypted( (char *)lsIp, 4 );
+            unsigned short lsPort = 0;
+            k->getNachrichtEncrypted( (char*)&lsPort, 2 );
+            k->sendeEncrypted( "\3", 1 );
+            k->getNachrichtEncrypted( &byte, 1 );
+            k->trenne();
+            Framework::Text ipT;
+            ipT += (int)lsIp[ 0 ];
+            ipT += ".";
+            ipT += (int)lsIp[ 1 ];
+            ipT += ".";
+            ipT += (int)lsIp[ 2 ];
+            ipT += ".";
+            ipT += (int)lsIp[ 3 ];
+            int l = 0;
+            return new ESClient( cId, port, ipT, key, keyLen );
+        }
+        else if( byte == 3 )
+        {
+            k->getNachrichtEncrypted( &byte, 1 );
+            char *f = new char[ byte + 1 ];
+            f[ byte ] = 0;
+            k->getNachrichtEncrypted( f, byte );
+            std::cerr << "error while requesting ErhaltungServer server returned: " << f << "\n";
+            delete[]f;
+        }
+        return 0;
+    }
+
     bool unregister()
     {
         connect();

+ 8 - 1
TestWalker/Start.cpp

@@ -23,8 +23,15 @@ public:
 protected:
     std::streamsize xsputn( const charT * pChar, std::streamsize n )
     {
+        Framework::Text tmp( pChar );
         tf->lockZeichnung();
-        tf->zText()->append( pChar );
+        if( tmp.getText()[ 0 ] == 1 && tf->zText()->getText()[ tf->zText()->getLength() - 2 ] == 1 )
+        {
+            int anz = tf->zText()->anzahlVon( 1 );
+            tf->zText()->ersetzen( tf->zText()->positionVon( 1, anz - 2 ), tf->zText()->getLength(), tmp );
+        }
+        else
+            tf->zText()->append( pChar );
         tf->setRender();
         tf->updateVScroll();
         if( tf->zText()->anzahlVon( '\n' ) > 1000 )

+ 1 - 0
TestWalker/TestWalker.vcxproj

@@ -137,6 +137,7 @@ copy "..\..\..\..\..\allgemein\Network\x64\Debug\Network.dll" Network.dll</Comma
     <ClCompile Include="TestThread.cpp" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="ESClient.h" />
     <ClInclude Include="Keys.h" />
     <ClInclude Include="MSClient.h" />
     <ClInclude Include="Test.h" />

+ 3 - 0
TestWalker/TestWalker.vcxproj.filters

@@ -38,5 +38,8 @@
     <ClInclude Include="Keys.h">
       <Filter>Headerdateien\Allgemein</Filter>
     </ClInclude>
+    <ClInclude Include="ESClient.h">
+      <Filter>Headerdateien\Allgemein</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>

+ 3 - 3
Tests/ConnectTest/ConnectTest.vcxproj

@@ -70,8 +70,8 @@
   </ImportGroup>
   <PropertyGroup Label="UserMacros" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
-    <IncludePath>..\..\..\..\..\..\Allgemein\Network\Network;$(IncludePath)</IncludePath>
-    <LibraryPath>..\..\..\..\..\..\Allgemein\Network\x64\Debug;$(LibraryPath)</LibraryPath>
+    <IncludePath>..\..\..\..\..\..\Allgemein\Network\Network;..\..\..\..\..\..\Allgemein\Framework;$(IncludePath)</IncludePath>
+    <LibraryPath>..\..\..\..\..\..\Allgemein\Network\x64\Debug;..\..\..\..\..\..\Allgemein\Framework\x64\Debug;$(LibraryPath)</LibraryPath>
   </PropertyGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
     <ClCompile>
@@ -88,7 +88,7 @@
       <Outputs>Kopieren;%(Outputs)</Outputs>
     </CustomBuildStep>
     <Link>
-      <AdditionalDependencies>Network.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>Network.lib;Framework.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">

+ 23 - 2
Tests/ConnectTest/Test.cpp

@@ -14,19 +14,40 @@ void ConnectTest::init()
 
 void ConnectTest::run()
 {
+    bool ok = 1;
     MSClient msc;
+    std::cout << "\x1...Main Server...\x1\n";
     if( !msc.registerSSL() )
     {
+        ok = 0;
         std::cerr << "client registration failed " __FILE__ ":" << __LINE__ << "\n";
         return;
     }
+    std::cout << "\x1...Erhaltung Server...\x1\n";
+    ESClient *esc = msc.createErhaltungServerClient();
+    if( !esc )
+    {
+        ok = 0;
+        std::cerr << "no Erhaltung Server available " __FILE__ ":" << __LINE__ << "\n";
+    }
+    else
+    {
+        if( !esc->verbinden() )
+        {
+            ok = 0;
+            std::cerr << "no connection to Erhaltung Server " __FILE__ ":" << __LINE__ << "\n";
+        }
+    }
+
     // TODO
+    std::cout << "\x1...Abmeldung beim Server...\x1\n";
+    esc->release();
     if( !msc.unregister() )
     {
+        ok = 0;
         std::cerr << "client unregistration failed " __FILE__ ":" << __LINE__ << "\n";
-        return;
     }
-    ok = 1;
+    this->ok = ok;
 }
 
 bool ConnectTest::failed()