コード例 #1
0
ファイル: Program.cs プロジェクト: SariSultan/scs
        static void Main()
        {
            var serverPublicKey =
                new X509Certificate2(@"C:\Users\node\Desktop\scs\SSLSamples\CertificateFiles\Server\publicKey.cer");

            Console.WriteLine("Press enter to connect to server and call methods...");
            Console.ReadLine();

            //Create a client that can call methods of Calculator Service that is running on local computer and 10083 TCP port
            //Since IScsServiceClient is IDisposible, it closes connection at the end of the using block
            //using (var client = ScsServiceClientBuilder.CreateClient<ICalculatorService>(new ScsTcpEndPoint("127.0.0.1", 10083)))
            using (var client = SslScsServiceClientBuilder.CreateSslClient <ICalculatorService>(new ScsTcpEndPoint("127.0.0.1", 10083)
                                                                                                , serverPublicKey, "127.0.0.1", SslScsAuthMode.ServerAuth, null, null))
            {
                //Connect to the server
                client.Connect();

                //Call a remote method of server
                var division = client.ServiceProxy.Divide(42, 3);

                //Write the result to the screen
                Console.WriteLine("Result: " + division);
            }

            Console.WriteLine("Press enter to stop client application");
            Console.ReadLine();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: SariSultan/scs
        static void Main()
        {
            var serverPublicKey =
                new X509Certificate2(@"C:\Users\node\Desktop\scs\SSLSamples\CertificateFiles\Server\publicKey.cer");
            //Create a client to connecto to phone book service on local server and 10048 TCP port.
            //var client = ScsServiceClientBuilder.CreateClient<IPhoneBookService>(new ScsTcpEndPoint("127.0.0.1", 10048));
            var client = SslScsServiceClientBuilder.CreateSslClient <IPhoneBookService>(new ScsTcpEndPoint("127.0.0.1", 10048)
                                                                                        , serverPublicKey
                                                                                        , "127.0.0.1"
                                                                                        , SslScsAuthMode.ServerAuth
                                                                                        , null, null);

            Console.WriteLine("Press enter to connect to phone book service...");
            Console.ReadLine();

            //Connect to the server
            client.Connect();

            var person1 = new PhoneBookRecord {
                Name = "Halil ibrahim", Phone = "5881112233"
            };
            var person2 = new PhoneBookRecord {
                Name = "John Nash", Phone = "58833322211"
            };

            //Add some persons
            client.ServiceProxy.AddPerson(person1);
            client.ServiceProxy.AddPerson(person2);

            //Search for a person
            var person = client.ServiceProxy.FindPerson("Halil");

            if (person != null)
            {
                Console.WriteLine("Person is found:");
                Console.WriteLine(person);
            }
            else
            {
                Console.WriteLine("Can not find person!");
            }

            Console.WriteLine();
            Console.WriteLine("Press enter to disconnect from phone book service...");
            Console.ReadLine();

            //Disconnect from server
            client.Disconnect();
        }
コード例 #3
0
        static void Main()
        {
            Console.WriteLine("Press enter to connect to server and call " + Consts.MethodCallCount + " methods...");
            Console.ReadLine();

            //using (var client = ScsServiceClientBuilder.CreateClient<ICalculatorService>(new ScsTcpEndPoint("127.0.0.1", 10083)))
            using (var client = SslScsServiceClientBuilder.CreateSslClient <ICalculatorService>(new ScsTcpEndPoint("127.0.0.1", 10083), Consts.ServerPublicKey, "127.0.0.1", SslScsAuthMode.ServerAuth, null, null))
            {
                client.Connect();

                var stopwatch = Stopwatch.StartNew();
                for (var i = 0; i < Consts.MethodCallCount; i++)
                {
                    var division = client.ServiceProxy.Add(2, 3);
                }

                stopwatch.Stop();
                Console.WriteLine(Consts.MethodCallCount + " remote method call made in " + stopwatch.Elapsed.TotalMilliseconds.ToString("0.000") + " ms.");
            }

            Console.WriteLine("Press enter to stop client application");
            Console.ReadLine();
        }
コード例 #4
0
        static void Main()
        {
            var serverPublicKey =
                new X509Certificate2(@"C:\Users\node\Desktop\scs\SSLSamples\CertificateFiles\Server\publicKey.cer");

            Console.WriteLine("Press any key to add person.");
            Console.ReadLine();

            //Create a client to connecto to phone book service on local server and 10048 TCP port.
            //var client = ScsServiceClientBuilder.CreateClient<IPhoneBookService>(new ScsTcpEndPoint("127.0.0.1", 10048));
            var client = SslScsServiceClientBuilder.CreateSslClient <IPhoneBookService>(new ScsTcpEndPoint("127.0.0.1", 10048)
                                                                                        , serverPublicKey
                                                                                        , "127.0.0.1"
                                                                                        , SslScsAuthMode.ServerAuth
                                                                                        , null, null);

            //Directly call a method (it automatically connects, calls and disconnects)
            client.ServiceProxy.AddPerson(new PhoneBookRecord {
                Name = "Halil ibrahim", Phone = "5881112233"
            });

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }
コード例 #5
0
        private void Scan()
        {
            try
            {
                BlockUI();
                using (
                    //var client = ScsServiceClientBuilder.CreateClient<INetworkBVPSService>(
                    //    new ScsTcpEndPoint(Program.ServerIp, Program.ServerPort)
                    //)
                    var client = SslScsServiceClientBuilder.CreateSslClient <INetworkBVPSService>(
                        new ScsTcpEndPoint(Program.ServerIp, Program.ServerPort)
                        , Program.ServerPublicKey
                        , Program.ServerIp
                        , SslScsAuthMode.ServerAuth
                        , null
                        )
                    )
                {
                    client.Timeout        = 10 * 60 * 1000; //timeout 10 minutes
                    client.ConnectTimeout = 5 * 1000;       //Time to connect // 5 seconds
                    client.Connect();
                    var details = new RequiredCommunicationDetails
                    {
                        DeviceID  = devIdTxtBox.Text,
                        TimeStamp = DateTime.Now
                    };

                    var requiredDetails = new RequiredCommunicationDetailsPacket
                    {
                        DeviceID             = devIdTxtBox.Text,
                        CommunicationDetails = details
                    };
                    requiredDetails.PrepareForSending(authKeyTxtBox.Text);

                    UpdateProgress($"Connecting...");
                    var verResult = client.ServiceProxy.VerifyConnection(requiredDetails);

                    if (verResult.Item1)
                    {
                        UpdateProgress($"Connection Verified Successfully.");
                        // MessageBox.Show(@"Connection verified successfully!");
                        Program.FileLogger.Log($"Verified to the server successfully", LogMsgType.Warning);

                        Program.DeviceId = devIdTxtBox.Text;
                        Program.AuthKey  = authKeyTxtBox.Text;
                    }
                    else
                    {
                        UpdateProgress($"Connection Verification failed.");
                        MessageBox.Show($@"Not Verified, error ={verResult.Item2}");
                        Program.FileLogger.Log($"Could not verify to the server, the issue was {verResult.Item2}",
                                               LogMsgType.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                UpdateProgress($"Error");
                MessageBox.Show(@"Exception [Cannot connect to the server] " + ex.Message);
                Program.FileLogger.Log(@"exception [most probably server is not running] " + ex.Message,
                                       LogMsgType.Exception);
            }

            if (!Program.IsServerVerified)
            {
                EnableUI();
                return;
            }
            try
            {
                using (
                    //var client = ScsServiceClientBuilder.CreateClient<INetworkBVPSService>(
                    //    new ScsTcpEndPoint(Program.ServerIp, Program.ServerPort))
                    //)
                    var client = SslScsServiceClientBuilder.CreateSslClient <INetworkBVPSService>(
                        new ScsTcpEndPoint(Program.ServerIp, Program.ServerPort)
                        , Program.ServerPublicKey
                        , Program.ServerIp
                        , SslScsAuthMode.ServerAuth
                        , null
                        )
                    )
                {
                    client.Timeout        = 10 * 1000; //timeout 1 minutes
                    client.ConnectTimeout = 5 * 1000;  //Time to connect // 5 seconds
                    client.Connect();
                    var details = new RequiredCommunicationDetails
                    {
                        DeviceID        = Program.DeviceId,
                        TimeStamp       = DateTime.Now,
                        ScanningDetails = RequiredDetailsHelper.GetRequiredScanningDetails()
                    };


                    var requiredDetails = new RequiredCommunicationDetailsPacket
                    {
                        DeviceID             = Program.DeviceId,
                        CommunicationDetails = details
                    };
                    requiredDetails.PrepareForSending(Program.AuthKey);


                    var verResult = client.ServiceProxy.ScanHost(requiredDetails);


                    switch (verResult.ScanningResult)
                    {
                    case ScanningReturnResult.InvalidAuthentication:
                        var msg = $"Scanning Refused by the server, due to ${ScanningReturnResult.InvalidAuthentication} [{verResult.Message}], ID={Program.DeviceId} \n You might have been blocked by DoS protection, kindly let us know.";
                        LogHelper.LogMessageToAll(Program.FileLogger, null, msg,
                                                  LogMsgType.Warning);
                        MessageBox.Show(msg);
                        return;

                        break;

                    case ScanningReturnResult.Error:
                        MessageBox.Show(@"UNKNOWN ERROR, try again later!");
                        return;

                        break;

                    case ScanningReturnResult.Success:
                        LogHelper.LogMessageToAll(Program.FileLogger, null,
                                                  $"Scan result received succesfully for Device ID={Program.DeviceId} \n Received Response from Server {verResult.Message}",
                                                  LogMsgType.Debug);
                        GetResult(verResult.JobId);

                        break;

                    case ScanningReturnResult.ServerBusyJobRefused:
                        var msg2 = $"Scanning Refused by the server, due to ${ScanningReturnResult.ServerBusyJobRefused}, ID={Program.DeviceId}";
                        LogHelper.LogMessageToAll(Program.FileLogger, null, msg2
                                                  ,
                                                  LogMsgType.Warning);
                        MessageBox.Show(msg2);
                        return;

                        break;

                    case ScanningReturnResult.StillScanning:
                        LogHelper.LogMessageToAll(Program.FileLogger, null,
                                                  $"Scanning in progress by the server, due to ${ScanningReturnResult.StillScanning}, ID={Program.DeviceId}",
                                                  LogMsgType.Warning);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"exception [most probably server is not running] " + ex);
                Program.FileLogger.Log(@"exception [most probably server is not running] " + ex.Message,
                                       LogMsgType.Exception);
            }
            finally
            {
                EnableUI();
            }
            // rtb.AppendText($"Required {stopwatch.ElapsedMilliseconds}ms to finish");
        }
コード例 #6
0
        private void GetResult(Guid verResultJobId)
        {
            bool gotIt    = false;
            int  waitTime = 1000;
            int  cnt      = 0;

            do
            {
                Thread.Sleep(waitTime);
                UpdateProgress(cnt++ % 2 == 0 ? $"Getting result..." : $"Please wait...");

                try
                {
                    using (var client = SslScsServiceClientBuilder.CreateSslClient <INetworkBVPSService>(
                               new ScsTcpEndPoint(Program.ServerIp, Program.ServerPort)
                               , Program.ServerPublicKey
                               , Program.ServerIp
                               , SslScsAuthMode.ServerAuth
                               , null
                               ))
                    {
                        client.Timeout = 10 * 60 * 1000;//timeout 10 minutes
                        var details = new RequiredCommunicationDetails
                        {
                            JobId           = verResultJobId,
                            DeviceID        = Program.DeviceId,
                            TimeStamp       = DateTime.Now,
                            ScanningDetails = RequiredDetailsHelper.GetRequiredScanningDetails()
                        };


                        var requiredDetails = new RequiredCommunicationDetailsPacket
                        {
                            DeviceID             = Program.DeviceId,
                            CommunicationDetails = details
                        };

                        requiredDetails.PrepareForSending(Program.AuthKey);

                        client.Connect();
                        var verResult = client.ServiceProxy.GetResult(requiredDetails);


                        switch (verResult.ScanningResult)
                        {
                        case ScanningReturnResult.InvalidAuthentication:
                            LogHelper.LogMessageToAll(Program.FileLogger, null, $"Scanning Refused by the server, due to ${ScanningReturnResult.InvalidAuthentication}, ID={Program.DeviceId}", LogMsgType.Warning);
                            break;

                        case ScanningReturnResult.Error:
                            MessageBox.Show(@"UNKNOWN ERROR, try again later!");
                            gotIt = true;
                            break;

                        case ScanningReturnResult.Success:
                            LogHelper.LogMessageToAll(Program.FileLogger, null, $"Scan result received succesfully for Device ID={Program.DeviceId} \n Received Response from Server {verResult.Message}", LogMsgType.Debug);
                            var report = ReportingHelper.GetCompleteReport(verResult.CompressedHTML);
                            var dial   = new SaveFileDialog();
                            dial.FileName = "Report.html";
                            dial.Filter   = "html files (*.html)|*.html";
                            BeginInvoke((Action)(() =>
                            {
                                if (dial.ShowDialog() == DialogResult.OK)
                                {
                                    File.WriteAllText(dial.FileName, report);
                                    LogHelper.LogMessageToAll(Program.FileLogger, null, $"User saved the report to {dial.FileName}", LogMsgType.Warning);
                                    Process.Start(dial.FileName);
                                }
                                else
                                {
                                    LogHelper.LogMessageToAll(Program.FileLogger, null, $"User selected not to save the report", LogMsgType.Warning);
                                }
                            }), null);


                            gotIt = true;
                            break;

                        case ScanningReturnResult.StillScanning:
                            waitTime *= 2;
                            break;

                        case ScanningReturnResult.ServerBusyJobRefused:
                            LogHelper.LogMessageToAll(Program.FileLogger, null, $"Scanning Refused by the server, due to ${ScanningReturnResult.ServerBusyJobRefused}, ID={Program.DeviceId}", LogMsgType.Warning);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(@"exception [most probably server is not running] " + ex);
                    Program.FileLogger.Log(@"exception [most probably server is not running] " + ex.Message,
                                           LogMsgType.Exception);
                }
            } while (!gotIt);
            EnableUI();
        }