BVB Source Codes

SuperSocket v1.6.6 Show ProtocolTestBase.cs Source code

Return Download SuperSocket v1.6.6: download ProtocolTestBase.cs Source code - Download SuperSocket v1.6.6 Source code - Type:.cs
  1. 锘縰sing System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using NUnit.Framework;
  11. using SuperSocket.SocketBase;
  12. using SuperSocket.SocketBase.Config;
  13. using SuperSocket.SocketBase.Logging;
  14. using SuperSocket.SocketBase.Protocol;
  15.  
  16. namespace SuperSocket.Test.Protocol
  17. {
  18.     public abstract class ProtocolTestBase
  19.     {
  20.         private TestServer m_Server;
  21.  
  22.         protected abstract IReceiveFilterFactory<StringRequestInfo> CurrentReceiveFilterFactory { get; }
  23.  
  24.         [SetUp]
  25.         public void Setup()
  26.         {
  27.             m_Server = CreateServer(CurrentReceiveFilterFactory, SocketMode.Tcp);
  28.         }
  29.  
  30.         [TearDown]
  31.         public void TearDown()
  32.         {
  33.             m_Server.Stop();
  34.         }
  35.  
  36.         private TestServer CreateServer(IReceiveFilterFactory<StringRequestInfo> receiveFilterFactory)
  37.         {
  38.             return CreateServer(receiveFilterFactory, SocketMode.Tcp);
  39.         }
  40.  
  41.         private TestServer CreateServer(IReceiveFilterFactory<StringRequestInfo> receiveFilterFactory, SocketMode mode)
  42.         {
  43.             var appServer = new TestServer();
  44.  
  45.             var serverConfig = new ServerConfig();
  46.             serverConfig.Ip = "127.0.0.1";
  47.             serverConfig.Port = 2012;
  48.             serverConfig.Mode = mode;
  49.             serverConfig.DisableSessionSnapshot = true;
  50.  
  51.             var setupResult = appServer.Setup(serverConfig, null, receiveFilterFactory, new ConsoleLogFactory(), null, null);
  52.  
  53.             Assert.IsTrue(setupResult);
  54.             Assert.IsTrue(appServer.Start());
  55.  
  56.             return appServer;
  57.         }
  58.  
  59.         protected Socket CreateClient()
  60.         {
  61.             var serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2012);
  62.             var socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  63.  
  64.             socket.Connect(serverAddress);
  65.  
  66.             return socket;
  67.         }
  68.  
  69.         protected abstract string CreateRequest(string sourceLine);
  70.  
  71.         [Test]
  72.         public void TestNormalRequest()
  73.         {
  74.             using (var socket = CreateClient())
  75.             {
  76.                 var socketStream = new NetworkStream(socket);
  77.                 using (var reader = new StreamReader(socketStream, Encoding.ASCII, true))
  78.                 using (var writer = new ConsoleWriter(socketStream, Encoding.ASCII, 1024 * 8))
  79.                 {
  80.                     reader.ReadLine();
  81.  
  82.                     var line = Guid.NewGuid().ToString();
  83.                     writer.Write(CreateRequest(line));
  84.                     writer.Flush();
  85.  
  86.                     var receivedLine = reader.ReadLine();
  87.  
  88.                     Assert.AreEqual(line, receivedLine);
  89.                 }
  90.             }
  91.         }
  92.  
  93.         [Test]
  94.         public void TestMiddleBreak()
  95.         {
  96.             for (var i = 0; i < 100; i++)
  97.             {
  98.                 using (var socket = CreateClient())
  99.                 {
  100.                     var socketStream = new NetworkStream(socket);
  101.                     using (var reader = new StreamReader(socketStream, Encoding.ASCII, true))
  102.                     using (var writer = new ConsoleWriter(socketStream, Encoding.ASCII, 1024 * 8))
  103.                     {
  104.                         reader.ReadLine();
  105.  
  106.                         var line = Guid.NewGuid().ToString();
  107.                         var sendingLine = CreateRequest(line);
  108.                         writer.Write(sendingLine.Substring(0, sendingLine.Length / 2));
  109.                         writer.Flush();
  110.  
  111.                         var s = m_Server.GetAllSessions().FirstOrDefault();
  112.                         s.Close();
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.  
  118.         [Test]
  119.         public void TestFragmentRequest()
  120.         {
  121.             using (var socket = CreateClient())
  122.             {
  123.                 var socketStream = new NetworkStream(socket);
  124.                 using (var reader = new StreamReader(socketStream, Encoding.ASCII, true))
  125.                 using (var writer = new ConsoleWriter(socketStream, Encoding.ASCII, 1024 * 8))
  126.                 {
  127.                     reader.ReadLine();
  128.  
  129.                     var line = Guid.NewGuid().ToString();
  130.                     var request = CreateRequest(line);
  131.  
  132.                     for (var i = 0; i < request.Length; i++)
  133.                     {
  134.                         writer.Write(request[i]);
  135.                         writer.Flush();
  136.                         Thread.Sleep(50);
  137.                     }
  138.  
  139.                     var receivedLine = reader.ReadLine();
  140.                     Assert.AreEqual(line, receivedLine);
  141.                 }
  142.             }
  143.         }
  144.  
  145.         [Test]
  146.         public void TestBatchRequest()
  147.         {
  148.             using (var socket = CreateClient())
  149.             {
  150.                 var socketStream = new NetworkStream(socket);
  151.                 using (var reader = new StreamReader(socketStream, Encoding.ASCII, true))
  152.                 using (var writer = new ConsoleWriter(socketStream, Encoding.ASCII, 1024 * 8))
  153.                 {
  154.                     reader.ReadLine();
  155.  
  156.                     int size = 100;
  157.  
  158.                     var lines = new string[size];
  159.  
  160.                     for (var i = 0; i < size; i++)
  161.                     {
  162.                         var line = Guid.NewGuid().ToString();
  163.                         lines[i] = line;
  164.                         var request = CreateRequest(line);
  165.                         writer.Write(request);
  166.                     }
  167.  
  168.                     writer.Flush();
  169.  
  170.                     for (var i = 0; i < size; i++)
  171.                     {
  172.                         var receivedLine = reader.ReadLine();
  173.                         Assert.AreEqual(lines[i], receivedLine);
  174.                     }
  175.                 }
  176.             }
  177.         }
  178.  
  179.         [Test]
  180.         public void TestBreakRequest()
  181.         {
  182.             using (var socket = CreateClient())
  183.             {
  184.                 var socketStream = new NetworkStream(socket);
  185.                 using (var reader = new StreamReader(socketStream, Encoding.ASCII, true))
  186.                 using (var writer = new ConsoleWriter(socketStream, Encoding.ASCII, 1024 * 8))
  187.                 {
  188.                     reader.ReadLine();
  189.  
  190.                     int size = 1000;
  191.  
  192.                     var lines = new string[size];
  193.  
  194.                     var sb = new StringBuilder();
  195.  
  196.                     for (var i = 0; i < size; i++)
  197.                     {
  198.                         var line = Guid.NewGuid().ToString();
  199.                         lines[i] = line;
  200.                         sb.Append(CreateRequest(line));
  201.                     }
  202.  
  203.                     var source = sb.ToString();
  204.  
  205.                     var rd = new Random();
  206.  
  207.                     var rounds = new List<KeyValuePair<int, int>>();
  208.  
  209.                     var rest = source.Length;
  210.  
  211.                     while (rest > 0)
  212.                     {
  213.                         if (rest == 1)
  214.                         {
  215.                             rounds.Add(new KeyValuePair<int, int>(source.Length - rest, 1));
  216.                             rest = 0;
  217.                             break;
  218.                         }
  219.  
  220.                         var thisRound = rd.Next(1, rest);
  221.                         rounds.Add(new KeyValuePair<int, int>(source.Length - rest, thisRound));
  222.                         rest -= thisRound;
  223.                     }
  224.  
  225.                     for (var i = 0; i < rounds.Count; i++)
  226.                     {
  227.                         var r = rounds[i];
  228.                         writer.Write(source.Substring(r.Key, r.Value));
  229.                         writer.Flush();
  230.                     }
  231.  
  232.                     for (var i = 0; i < size; i++)
  233.                     {
  234.                         var receivedLine = reader.ReadLine();
  235.                         Assert.AreEqual(lines[i], receivedLine);
  236.                     }
  237.                 }
  238.             }
  239.         }
  240.     }
  241. }
  242.  
downloadProtocolTestBase.cs Source code - Download SuperSocket v1.6.6 Source code
Related Source Codes/Software:
Students' basic information input program v2.0 - 1, the program is based on. net4.0 framework and d... 2016-08-19
Student information input system v2.0 - 1, the program is based on. net4.0 framework and d... 2016-08-19
Baidu handwriting input source v1.0 - Baidu handwriting input method source code, no nee... 2016-08-19
Rui bao tong v1.0 JAVA version of B2B e-commerce system - Rainbow B2B system developed using the popular JAV... 2016-08-19
HTML + javascript lottery system v2.1.1 HTA - Technology QQ:838341952 HTML+JS lottery system sta... 2016-08-19
Cloud on the CMS (SyCms) v1.95 - Add menus and blocks the system style functionalit... 2016-08-19
1.0 housing, rental housing sale platform - Rent housing rent out platform is a very typical r... 2016-08-19
Deng Xi v2.4 website help system - Deng Xi website help system is a very useful help ... 2016-08-19
Address book v1.0 FaLiang three layer - This Demo is a three layer address book source cod... 2016-08-19
Ray speed v7.0 wage query system - To modify the software to conduct a comprehensive ... 2016-08-19
MTStatusBarOverlay - A custom iOS status bar overlay seen in Apps like ... 2017-05-12
blueprint - Reverse engineer server configuration ... 2017-05-12
PullToRefresh - A simple iPhone TableViewController for adding the... 2017-05-12
mrjob - Run MapReduce jobs on Hadoop or Amazon Web Service... 2017-05-11
wax - Wax is now being maintained by alibaba ... 2017-05-11
ledisdb - a high performance NoSQL powered by Go ... 2017-05-11
d3-cloud - Create word clouds in JavaScript. ... 2017-05-11
react-makes-you-sad - Here's a flowchart to make you happy again! 2017-05-11
flat-surface-shader - Flat Surface Shader for rendering lit triangles to... 2017-05-11
flat-surface-shader - Flat Surface Shader for rendering lit triangles to... 2017-05-11

 Back to top