static byte[] ChunkedReadResponse(Stream sm) { ArraySegmentList <byte> arraySegmentList = new ArraySegmentList <byte>(); int chunked = GetChunked(sm); while (chunked > 0) { byte[] buff = new byte[chunked]; try { int inread = sm.Read(buff, 0, buff.Length); while (inread < buff.Length) { inread += sm.Read(buff, inread, buff.Length - inread); } arraySegmentList.Add(new ArraySegment <byte>(buff)); if (sm.ReadByte() != -1)//读取段末尾的\r\n { sm.ReadByte(); } } catch (Exception) { break; } chunked = GetChunked(sm); } return(arraySegmentList.ToArray()); }
public void TestIndexAccess() { ArraySegmentList <char> source = new ArraySegmentList <char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); char[] exptected = "I lovHell".ToCharArray(); for (int i = source.Count - 1; i >= 0; i--) { Assert.AreEqual(exptected[i], source[i]); } for (int i = 0; i < source.Count; i++) { Assert.AreEqual(exptected[i], source[i]); } source.RemoveSegmentAt(0); source.RemoveSegmentAt(0); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); for (int i = 0; i < source.Count; i++) { Assert.AreEqual(exptected[i], source[i], i + " is expected!"); } }
public void TestIndexAccess2() { ArraySegmentList <char> sourceA = new ArraySegmentList <char>(); List <char> sourceB = new List <char>(); char[] element = null; for (var i = 0; i < 100; i++) { element = Guid.NewGuid().ToString().ToCharArray(); sourceA.AddSegment(element, 0, element.Length); sourceB.AddRange(element); } Random rd = new Random(); for (int i = 0; i < 1000; i++) { int index = rd.Next(0, sourceA.Count - 1); Assert.AreEqual(sourceB[index], sourceA[index]); } int testCount = 10000; GC.Collect(); Stopwatch watch = new Stopwatch(); watch.Start(); char tt = ' '; for (var i = 0; i < testCount; i++) { int index = rd.Next(0, sourceA.Count - 1); tt = sourceA[index]; } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); watch.Reset(); GC.Collect(); watch.Start(); for (var i = 0; i < testCount; i++) { int index = rd.Next(0, sourceA.Count - 1); tt = sourceB[index]; } Console.Write(tt); watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); }
public void TestDecodeMaskPerformance() { ArraySegmentList source = new ArraySegmentList(); for (var i = 0; i < 100; i++) { var element = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()); source.AddSegment(element, 0, element.Length); } byte[] mask = new byte[] { source[5], source[100], source[150], source[200] }; int testCount = 1000; GC.Collect(); Stopwatch watch = new Stopwatch(); watch.Start(); for (var i = 0; i < testCount; i++) { source.DecodeMask(mask, 100, source.Count - 100); } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); watch.Reset(); GC.Collect(); watch.Start(); for (var i = 0; i < testCount; i++) { var data = source.ToArrayData(100, source.Count - 100); DecodeMask(data, mask, 0, data.Length); } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); watch.Reset(); GC.Collect(); watch.Start(); for (var i = 0; i < testCount; i++) { DecodeMask(source, mask, 100, source.Count - 100); } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); }
public void TestIndexAccess() { ArraySegmentList<char> source = new ArraySegmentList<char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); char[] exptected = "I lovHell".ToCharArray(); for (int i = source.Count - 1; i >= 0; i--) { Assert.AreEqual(exptected[i], source[i]); } for (int i = 0; i < source.Count; i++) { Assert.AreEqual(exptected[i], source[i]); } source.RemoveSegmentAt(0); source.RemoveSegmentAt(0); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); for (int i = 0; i < source.Count; i++) { Assert.AreEqual(exptected[i], source[i], i + " is expected!"); } }
public UserReader(UserReader previousCommandReader) { m_OPTypeLength = 1; m_OPCodeLength = 1; m_ParaCountLength = 1; m_PerParaCountLength = 2; this.m_BufferSegments = previousCommandReader.BufferSegments; }
public void TestEndsWith() { ArraySegmentList <char> source = new ArraySegmentList <char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); Assert.AreEqual(true, source.EndsWith("Hell".ToCharArray())); }
private void AddSegment(byte[] segment, int offset, int length, bool toBeCopied) { if (m_Buffer == null) { m_Buffer = new ArraySegmentList(); } m_Buffer.AddSegment(segment, offset, length, toBeCopied); }
protected void AddArraySegment( ArraySegmentList segments, byte[] buffer, int offset, int length, bool isReusableBuffer) { segments.AddSegment(buffer, offset, length, isReusableBuffer); }
internal UserReader(UserClientEngine engine) { m_OPTypeLength = 1; m_OPCodeLength = 1; m_ParaCountLength = 1; m_PerParaCountLength = 2; this.Engine = engine; this.m_BufferSegments = new ArraySegmentList(); }
public void TestCount() { ArraySegmentList<char> source = new ArraySegmentList<char>(new List<ArraySegmentItem<char>> { new ArraySegmentItem<char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem<char>("Hello world!".ToCharArray(), 0, 4) }); Assert.AreEqual(9, source.Count); }
public void TestEndsWith() { ArraySegmentList <char> source = new ArraySegmentList <char>(new List <ArraySegmentItem <char> > { new ArraySegmentItem <char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem <char>("Hello world!".ToCharArray(), 0, 4) }); Assert.AreEqual(true, source.EndsWith("Hell".ToCharArray())); }
public void TestCount() { ArraySegmentList <char> source = new ArraySegmentList <char>(new List <ArraySegmentItem <char> > { new ArraySegmentItem <char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem <char>("Hello world!".ToCharArray(), 0, 4) }); Assert.AreEqual(9, source.Count); }
public void TestEndsWith() { ArraySegmentList<char> source = new ArraySegmentList<char>(new List<ArraySegmentItem<char>> { new ArraySegmentItem<char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem<char>("Hello world!".ToCharArray(), 0, 4) }); Assert.AreEqual(true, source.EndsWith("Hell".ToCharArray())); }
public void TestToArray() { ArraySegmentList <char> source = new ArraySegmentList <char>(new List <ArraySegment <char> > { new ArraySegment <char>("I love you,".ToCharArray(), 0, 5), new ArraySegment <char>("Hello world!".ToCharArray(), 0, 4) }); char[] exptected = "I lovHell".ToCharArray(); Assert.AreEqual(exptected, source.ToArrayData()); }
public void TestIndexOf() { ArraySegmentList <char> source = new ArraySegmentList <char>(); source.AddSegment("I love you,".ToCharArray(), 3, 7); source.AddSegment("Hello world!".ToCharArray(), 0, 4); //string exptected = "ove youHell"; Assert.AreEqual(-1, source.IndexOf('I')); Assert.AreEqual(9, source.IndexOf('l')); Assert.AreEqual(0, source.IndexOf('o')); Assert.AreEqual(1, source.IndexOf('v')); }
public void TestToArray() { ArraySegmentList <char> source = new ArraySegmentList <char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); char[] exptected = "I lovHell".ToCharArray(); Assert.AreEqual(exptected, source.ToArrayData()); Assert.AreEqual("He", new string(source.ToArrayData(5, 2))); Assert.AreEqual("ovHe", new string(source.ToArrayData(3, 4))); }
public void TestIndexOf() { ArraySegmentList <char> source = new ArraySegmentList <char>(new List <ArraySegmentItem <char> > { new ArraySegmentItem <char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem <char>("Hello world!".ToCharArray(), 0, 4) }); //string exptected = "I lovHell"; Assert.AreEqual(0, source.IndexOf('I')); Assert.AreEqual(2, source.IndexOf('l')); Assert.AreEqual(3, source.IndexOf('o')); Assert.AreEqual(4, source.IndexOf('v')); }
public void TestIndexOf() { ArraySegmentList<char> source = new ArraySegmentList<char>(new List<ArraySegmentItem<char>> { new ArraySegmentItem<char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem<char>("Hello world!".ToCharArray(), 0, 4) }); //string exptected = "I lovHell"; Assert.AreEqual(0, source.IndexOf('I')); Assert.AreEqual(2, source.IndexOf('l')); Assert.AreEqual(3, source.IndexOf('o')); Assert.AreEqual(4, source.IndexOf('v')); }
public override void MessageTransferTest() { Socket socket; Stream stream; Handshake(SubProtocol, out socket, out stream); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.Append(Guid.NewGuid().ToString()); } string messageSource = sb.ToString(); Random rd = new Random(); ArraySegmentList receivedBuffer = new ArraySegmentList(); for (int i = 0; i < 10; i++) { int startPos = rd.Next(0, messageSource.Length - 2); int endPos = rd.Next(startPos + 1, Math.Min(messageSource.Length - 1, startPos + 1 + 100)); string currentCommand = messageSource.Substring(startPos, endPos - startPos); Console.WriteLine("Client:" + currentCommand); stream.Write(new byte[] { WebSocketConstant.StartByte }, 0, 1); byte[] data = m_Encoding.GetBytes("ECHO " + currentCommand); stream.Write(data, 0, data.Length); stream.Write(new byte[] { WebSocketConstant.EndByte }, 0, 1); stream.Flush(); int requredCount = m_Encoding.GetByteCount(currentCommand) + 2; Console.WriteLine("Require:" + requredCount); ReceiveMessage(stream, receivedBuffer, requredCount); Assert.AreEqual(requredCount, receivedBuffer.Count); Assert.AreEqual(WebSocketConstant.StartByte, receivedBuffer[0]); Assert.AreEqual(WebSocketConstant.EndByte, receivedBuffer[receivedBuffer.Count - 1]); Assert.AreEqual(currentCommand, m_Encoding.GetString(receivedBuffer.ToArrayData(1, receivedBuffer.Count - 2))); receivedBuffer.ClearSegements(); } socket.Shutdown(SocketShutdown.Both); socket.Close(); }
public void TestIndexAccess() { ArraySegmentList <char> source = new ArraySegmentList <char>(new List <ArraySegmentItem <char> > { new ArraySegmentItem <char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem <char>("Hello world!".ToCharArray(), 0, 4) }); char[] exptected = "I lovHell".ToCharArray(); for (int i = 0; i < source.Count; i++) { Assert.AreEqual(exptected[i], source[i]); } }
public void TestIndexAccess() { ArraySegmentList<char> source = new ArraySegmentList<char>(new List<ArraySegmentItem<char>> { new ArraySegmentItem<char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem<char>("Hello world!".ToCharArray(), 0, 4) }); char[] exptected = "I lovHell".ToCharArray(); for (int i = 0; i < source.Count; i++) { Assert.AreEqual(exptected[i], source[i]); } }
public void TestSearchPerformance() { ArraySegmentList <char> sourceA = new ArraySegmentList <char>(); List <char> sourceB = new List <char>(); char[] element = null; for (var i = 0; i < 100; i++) { element = Guid.NewGuid().ToString().ToCharArray(); sourceA.AddSegment(element, 0, element.Length); sourceB.AddRange(element); } char[] mark = element.Take(4).ToArray(); int testCount = 1000; GC.Collect(); Stopwatch watch = new Stopwatch(); watch.Start(); for (var i = 0; i < testCount; i++) { sourceA.SearchMark(mark); } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); watch.Reset(); GC.Collect(); watch.Start(); for (var i = 0; i < testCount; i++) { sourceB.SearchMark(mark); } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); }
protected void ReceiveMessage(Stream stream, ArraySegmentList commandBuffer, int predictCount) { byte[] buffer = new byte[1024]; int thisRead = 0; int left = predictCount; while ((thisRead = stream.Read(buffer, 0, Math.Min(left, buffer.Length))) > 0) { Console.WriteLine("Current read: {0}", thisRead); commandBuffer.AddSegment(buffer, 0, thisRead, true); left -= thisRead; if (left <= 0) break; } }
public virtual void MessageTransferTest() { Socket socket; Stream stream; Handshake(out socket, out stream); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.Append(Guid.NewGuid().ToString()); } string messageSource = sb.ToString(); Random rd = new Random(); ArraySegmentList<byte> receivedBuffer = new ArraySegmentList<byte>(); for (int i = 0; i < 10; i++) { int startPos = rd.Next(0, messageSource.Length - 2); int endPos = rd.Next(startPos + 1, messageSource.Length - 1); string currentCommand = messageSource.Substring(startPos, endPos - startPos); Console.WriteLine("Client:" + currentCommand); stream.Write(new byte[] { WebSocketConstant.StartByte }, 0, 1); byte[] data = Encoding.UTF8.GetBytes(currentCommand); stream.Write(data, 0, data.Length); stream.Write(new byte[] { WebSocketConstant.EndByte }, 0, 1); stream.Flush(); ReceiveMessage(stream, receivedBuffer, data.Length + 2); Assert.AreEqual(data.Length + 2, receivedBuffer.Count); Assert.AreEqual(WebSocketConstant.StartByte, receivedBuffer[0]); Assert.AreEqual(WebSocketConstant.EndByte, receivedBuffer[receivedBuffer.Count - 1]); Assert.AreEqual(currentCommand, Encoding.UTF8.GetString(receivedBuffer.ToArrayData(1, receivedBuffer.Count - 2))); receivedBuffer.ClearSegements(); } socket.Shutdown(SocketShutdown.Both); socket.Close(); }
public void TestOutOfIndex() { ArraySegmentList <char> source = new ArraySegmentList <char>(new List <ArraySegmentItem <char> > { new ArraySegmentItem <char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem <char>("Hello world!".ToCharArray(), 0, 4) }); Assert.Throws <IndexOutOfRangeException>(delegate { char currentChar = source[-1]; }); Assert.Throws <IndexOutOfRangeException>(delegate { char currentChar = source[10]; }); }
public void TestOutOfIndex() { ArraySegmentList<char> source = new ArraySegmentList<char>(new List<ArraySegmentItem<char>> { new ArraySegmentItem<char>("I love you,".ToCharArray(), 0, 5), new ArraySegmentItem<char>("Hello world!".ToCharArray(), 0, 4) }); Assert.Throws<IndexOutOfRangeException>(delegate { char currentChar = source[-1]; }); Assert.Throws<IndexOutOfRangeException>(delegate { char currentChar = source[10]; }); }
protected bool DetectEndMark(byte[] buffer, int thisRead, byte[] endMark, byte[] lastData, ref int lastDataSzie) { if (thisRead >= endMark.Length) { if (buffer.EndsWith(0, thisRead, endMark)) { return(true); } Array.Copy(buffer, thisRead - endMark.Length - 1, lastData, 0, endMark.Length); lastDataSzie = endMark.Length; } else { if (lastDataSzie + thisRead < lastData.Length) { Array.Copy(buffer, 0, lastData, lastDataSzie, thisRead); lastDataSzie = lastDataSzie + thisRead; } else { ArraySegmentList <byte> source = new ArraySegmentList <byte>(new List <ArraySegmentItem <byte> > { new ArraySegmentItem <byte>(lastData, 0, lastDataSzie), new ArraySegmentItem <byte>(buffer, 0, thisRead) }); if (source.EndsWith(endMark)) { return(true); } for (int i = 0; i < endMark.Length; i++) { lastData[i] = source[source.Count - endMark.Length + i]; } lastDataSzie = endMark.Length; } } return(false); }
public void TestCount() { ArraySegmentList<char> source = new ArraySegmentList<char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); Assert.AreEqual(9, source.Count); source.RemoveSegmentAt(0); Assert.AreEqual(4, source.Count); source.RemoveSegmentAt(0); Assert.AreEqual(0, source.Count); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); Assert.AreEqual(9, source.Count); }
public void TestRemoveSegment() { ArraySegmentList <char> source = new ArraySegmentList <char>(); source.AddSegment(new ArraySegment <char>("I love you,".ToCharArray(), 0, 5)); source.AddSegment(new ArraySegment <char>("Hello world!".ToCharArray(), 0, 4)); Assert.AreEqual(9, source.Count); Assert.AreEqual(2, source.SegmentCount); source.RemoveSegmentAt(1); Assert.AreEqual(5, source.Count); Assert.AreEqual(1, source.SegmentCount); char[] exptected = "I lov".ToCharArray(); for (var i = 0; i < exptected.Length; i++) { Assert.AreEqual(exptected[i], source[i]); } }
public void TestCount() { ArraySegmentList <char> source = new ArraySegmentList <char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); Assert.AreEqual(9, source.Count); source.RemoveSegmentAt(0); Assert.AreEqual(4, source.Count); source.RemoveSegmentAt(0); Assert.AreEqual(0, source.Count); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); Assert.AreEqual(9, source.Count); }
public void TestEcho() { Socket socket; Stream stream; Handshake(SubProtocol, out socket, out stream); StringBuilder sb = new StringBuilder(); sb.Append("ECHO"); var parameters = new List<string>(); for (int i = 0; i < 3; i++) { var p = Guid.NewGuid().ToString(); sb.Append(" "); sb.Append(p); parameters.Add(p); } stream.Write(new byte[] { WebSocketConstant.StartByte }, 0, 1); byte[] data = m_Encoding.GetBytes(sb.ToString()); stream.Write(data, 0, data.Length); stream.Write(new byte[] { WebSocketConstant.EndByte }, 0, 1); stream.Flush(); var receivedBuffer = new ArraySegmentList(); foreach (var p in parameters) { ReceiveMessage(stream, receivedBuffer, Encoding.UTF8.GetBytes(p).Length + 2); string rp = m_Encoding.GetString(receivedBuffer.ToArrayData(1, receivedBuffer.Count - 2)); Console.WriteLine(rp); Assert.AreEqual(p, rp); receivedBuffer.ClearSegements(); } socket.Shutdown(SocketShutdown.Both); socket.Close(); }
public void TestOutOfIndex() { ArraySegmentList<char> source = new ArraySegmentList<char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); char currentChar = ' '; Assert.Throws<IndexOutOfRangeException>(delegate { currentChar = source[-1]; }); Assert.Throws<IndexOutOfRangeException>(delegate { currentChar = source[10]; }); source.RemoveSegmentAt(0); Assert.Throws<IndexOutOfRangeException>(delegate { currentChar = source[4]; }); source.RemoveSegmentAt(0); Assert.Throws<IndexOutOfRangeException>(delegate { currentChar = source[0]; }); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); Assert.Throws<IndexOutOfRangeException>(delegate { currentChar = source[10]; }); Console.Write(currentChar); }
public void TestEcho() { Socket socket; Stream stream; Handshake(SubProtocol, out socket, out stream); StringBuilder sb = new StringBuilder(); sb.Append("ECHO"); var parameters = new List <string>(); for (int i = 0; i < 3; i++) { var p = Guid.NewGuid().ToString(); sb.Append(" "); sb.Append(p); parameters.Add(p); } stream.Write(new byte[] { WebSocketConstant.StartByte }, 0, 1); byte[] data = m_Encoding.GetBytes(sb.ToString()); stream.Write(data, 0, data.Length); stream.Write(new byte[] { WebSocketConstant.EndByte }, 0, 1); stream.Flush(); var receivedBuffer = new ArraySegmentList(); foreach (var p in parameters) { ReceiveMessage(stream, receivedBuffer, Encoding.UTF8.GetBytes(p).Length + 2); string rp = m_Encoding.GetString(receivedBuffer.ToArrayData(1, receivedBuffer.Count - 2)); Console.WriteLine(rp); Assert.AreEqual(p, rp); receivedBuffer.ClearSegements(); } socket.Shutdown(SocketShutdown.Both); socket.Close(); }
public void TestOutOfIndex() { ArraySegmentList <char> source = new ArraySegmentList <char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); char currentChar = ' '; Assert.Throws <IndexOutOfRangeException>(delegate { currentChar = source[-1]; }); Assert.Throws <IndexOutOfRangeException>(delegate { currentChar = source[10]; }); source.RemoveSegmentAt(0); Assert.Throws <IndexOutOfRangeException>(delegate { currentChar = source[4]; }); source.RemoveSegmentAt(0); Assert.Throws <IndexOutOfRangeException>(delegate { currentChar = source[0]; }); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); Assert.Throws <IndexOutOfRangeException>(delegate { currentChar = source[10]; }); Console.Write(currentChar); }
public void TestAddSegment() { ArraySegmentList<char> source = new ArraySegmentList<char>(); Assert.AreEqual(0, source.Count); source.AddSegment("I love you,".ToCharArray(), 0, 5); Assert.AreEqual(5, source.Count); Assert.AreEqual(1, source.SegmentCount); source.AddSegment("Hello world!".ToCharArray(), 0, 4); Assert.AreEqual(9, source.Count); Assert.AreEqual(2, source.SegmentCount); char[] exptected = "I lovHell".ToCharArray(); for (var i = 0; i < exptected.Length; i++) { Assert.AreEqual(exptected[i], source[i]); } }
public void TestDecode() { ArraySegmentList source = new ArraySegmentList(); StringBuilder sb = new StringBuilder(); List<int> countArray = new List<int>(); int from, to; Random rd = new Random(); from = rd.Next(0, 19); to = rd.Next(from + 1, 20); for (var i = 0; i < 20; i++) { var line = Guid.NewGuid().ToString(); var element = Encoding.UTF8.GetBytes(line); if (i >= from && i <= to) { countArray.Add(source.Count); Console.WriteLine(source.Count); sb.Append(line); source.AddSegment(element, 0, element.Length); countArray.Add(source.Count); Console.WriteLine(source.Count); } else { source.AddSegment(element, 0, element.Length); } } Assert.AreEqual(sb.ToString(), source.Decode(Encoding.UTF8, countArray[0], countArray[countArray.Count - 1] - countArray[0])); }
public void TestDecode() { ArraySegmentList source = new ArraySegmentList(); StringBuilder sb = new StringBuilder(); List <int> countArray = new List <int>(); int from, to; Random rd = new Random(); from = rd.Next(0, 19); to = rd.Next(from + 1, 20); for (var i = 0; i < 20; i++) { var line = Guid.NewGuid().ToString(); var element = Encoding.UTF8.GetBytes(line); if (i >= from && i <= to) { countArray.Add(source.Count); Console.WriteLine(source.Count); sb.Append(line); source.AddSegment(element, 0, element.Length); countArray.Add(source.Count); Console.WriteLine(source.Count); } else { source.AddSegment(element, 0, element.Length); } } Assert.AreEqual(sb.ToString(), source.Decode(Encoding.UTF8, countArray[0], countArray[countArray.Count - 1] - countArray[0])); }
public void TestSearchPerformance() { ArraySegmentList<char> sourceA = new ArraySegmentList<char>(); List<char> sourceB = new List<char>(); char[] element = null; for (var i = 0; i < 100; i++) { element = Guid.NewGuid().ToString().ToCharArray(); sourceA.AddSegment(element, 0, element.Length); sourceB.AddRange(element); } char[] mark = element.Take(4).ToArray(); int testCount = 1000; GC.Collect(); Stopwatch watch = new Stopwatch(); watch.Start(); for (var i = 0; i < testCount; i++) { sourceA.SearchMark(mark); } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); watch.Reset(); GC.Collect(); watch.Start(); for (var i = 0; i < testCount; i++) { sourceB.SearchMark(mark); } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); }
public void TestToArray() { ArraySegmentList<char> source = new ArraySegmentList<char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); char[] exptected = "I lovHell".ToCharArray(); Assert.AreEqual(exptected, source.ToArrayData()); Assert.AreEqual("He", new string(source.ToArrayData(5, 2))); Assert.AreEqual("ovHe", new string(source.ToArrayData(3, 4))); }
public void TestEndsWith() { ArraySegmentList<char> source = new ArraySegmentList<char>(); source.AddSegment("I love you,".ToCharArray(), 0, 5); source.AddSegment("Hello world!".ToCharArray(), 0, 4); Assert.AreEqual(true, source.EndsWith("Hell".ToCharArray())); }
/// <summary> /// Initializes a new instance of the <see cref="ReaderBase"/> class. /// </summary> /// <param name="previousCommandReader">The previous command reader.</param> public ReaderBase(ReaderBase previousCommandReader) { m_BufferSegments = previousCommandReader.BufferSegments; }
public void TestIndexAccess2() { ArraySegmentList<char> sourceA = new ArraySegmentList<char>(); List<char> sourceB = new List<char>(); char[] element = null; for (var i = 0; i < 100; i++) { element = Guid.NewGuid().ToString().ToCharArray(); sourceA.AddSegment(element, 0, element.Length); sourceB.AddRange(element); } Random rd = new Random(); for (int i = 0; i < 1000; i++) { int index = rd.Next(0, sourceA.Count - 1); Assert.AreEqual(sourceB[index], sourceA[index]); } int testCount = 10000; GC.Collect(); Stopwatch watch = new Stopwatch(); watch.Start(); for (var i = 0; i < testCount; i++) { int index = rd.Next(0, sourceA.Count - 1); var tt = sourceA[index]; } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); watch.Reset(); GC.Collect(); watch.Start(); for (var i = 0; i < testCount; i++) { int index = rd.Next(0, sourceA.Count - 1); var tt = sourceB[index]; } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); }
/// <summary> /// Resolves the specified data to TRequestInfo. /// </summary> /// <param name="data">The data.</param> /// <returns></returns> protected abstract TRequestInfo Resolve(ArraySegmentList data);
public WebSocketDataFrame(ArraySegmentList data) { m_InnerData = data; m_InnerData.ClearSegements(); }
public override void MessageTransferTest() { Socket socket; Stream stream; Handshake(out socket, out stream); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.Append(Guid.NewGuid().ToString()); } string messageSource = sb.ToString(); Random rd = new Random(); ArraySegmentList receivedBuffer = new ArraySegmentList(); for (int i = 0; i < 100; i++) { int startPos = rd.Next(0, messageSource.Length - 2); int endPos = rd.Next(startPos + 1, messageSource.Length - 1); string currentCommand = messageSource.Substring(startPos, endPos - startPos); Console.WriteLine("Client:" + currentCommand); var data = Encoding.UTF8.GetBytes(currentCommand); Console.WriteLine("Client Length:" + data.Length); int dataLen = SendMessage(stream, 1, data); stream.Flush(); ReceiveMessage(stream, receivedBuffer, dataLen); Assert.AreEqual(dataLen, receivedBuffer.Count); Assert.AreEqual(0x01, receivedBuffer[0] & 0x01); Assert.AreEqual(0x80, receivedBuffer[0] & 0x80); Assert.AreEqual(0x00, receivedBuffer[1] & 0x80); int skip = 2; if (data.Length < 126) Assert.AreEqual(data.Length, (int)(receivedBuffer[1] & 0x7F)); else if (data.Length < 65536) { Assert.AreEqual(126, (int)(receivedBuffer[1] & 0x7F)); var sizeData = receivedBuffer.ToArrayData(2, 2); Assert.AreEqual(data.Length, (int)sizeData[0] * 256 + (int)sizeData[1]); skip += 2; } else { Assert.AreEqual(127, (int)(receivedBuffer[1] & 0x7F)); var sizeData = receivedBuffer.ToArrayData(2, 8); long len = 0; int n = 1; for (int k = 7; k >= 0; k--) { len += (int)sizeData[k] * n; n *= 256; } Assert.AreEqual(data.Length, len); skip += 8; } Assert.AreEqual(currentCommand, Encoding.UTF8.GetString(receivedBuffer.ToArrayData(skip, data.Length))); receivedBuffer.ClearSegements(); } //socket.Shutdown(SocketShutdown.Both); //socket.Close(); }
/// <summary> /// Initializes a new instance of the <see cref="ReaderBase"/> class. /// </summary> /// <param name="websocket">The websocket.</param> public ReaderBase(WebSocket websocket) { WebSocket = websocket; m_BufferSegments = new ArraySegmentList(); }
public virtual void MessageBatchTransferTest() { Socket socket; Stream stream; Handshake(out socket, out stream); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.Append(Guid.NewGuid().ToString()); } string messageSource = sb.ToString(); Random rd = new Random(); ArraySegmentList receivedBuffer = new ArraySegmentList(); for (int i = 0; i < 10; i++) { var sentMessages = new string[10]; var sentLengths = new int[sentMessages.Length]; for (int j = 0; j < sentMessages.Length; j++) { int startPos = rd.Next(0, messageSource.Length - 2); int endPos = rd.Next(startPos + 1, Math.Min(messageSource.Length - 1, startPos + 1 + 5)); string currentCommand = messageSource.Substring(startPos, endPos - startPos); sentMessages[j] = currentCommand; Console.WriteLine("Client:" + currentCommand); stream.Write(new byte[] { WebSocketConstant.StartByte }, 0, 1); byte[] data = m_Encoding.GetBytes(currentCommand); sentLengths[j] = data.Length + 2; stream.Write(data, 0, data.Length); stream.Write(new byte[] { WebSocketConstant.EndByte }, 0, 1); } stream.Flush(); for (var j = 0; j < sentMessages.Length; j++) { Console.WriteLine("Expected: " + sentLengths[j]); ReceiveMessage(stream, receivedBuffer, sentLengths[j]); string message = m_Encoding.GetString(receivedBuffer.ToArrayData(1, receivedBuffer.Count - 2)); Console.WriteLine("E:" + sentMessages[j]); Console.WriteLine("A:" + message); Assert.AreEqual(WebSocketConstant.StartByte, receivedBuffer[0]); Assert.AreEqual(WebSocketConstant.EndByte, receivedBuffer[receivedBuffer.Count - 1]); Assert.AreEqual(sentMessages[j], message); Assert.AreEqual(sentLengths[j], receivedBuffer.Count); receivedBuffer.ClearSegements(); Console.WriteLine("Passed " + j); } } socket.Shutdown(SocketShutdown.Both); socket.Close(); }
public override void MessageBatchTransferTest() { Socket socket; Stream stream; Handshake(out socket, out stream); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.Append(Guid.NewGuid().ToString()); } string messageSource = sb.ToString(); Random rd = new Random(); ArraySegmentList receivedBuffer = new ArraySegmentList(); for (int i = 0; i < 10; i++) { var sentMessages = new string[10]; var sentMessageSizes = new int[10]; var sentLengths = new int[sentMessages.Length]; for (int j = 0; j < sentMessages.Length; j++) { int startPos = rd.Next(0, messageSource.Length - 2); int endPos = rd.Next(startPos + 1, Math.Min(messageSource.Length - 1, startPos + 1 + 5)); string currentCommand = messageSource.Substring(startPos, endPos - startPos); sentMessages[j] = currentCommand; Console.WriteLine("Client:" + currentCommand); byte[] data = Encoding.UTF8.GetBytes(currentCommand); Console.WriteLine("Client Length:" + data.Length); sentMessageSizes[j] = data.Length; int dataLen = SendMessage(stream, 1, data); sentLengths[j] = dataLen; } stream.Flush(); for (var j = 0; j < sentMessages.Length; j++) { Console.WriteLine("Expected: " + sentLengths[j]); ReceiveMessage(stream, receivedBuffer, sentLengths[j]); int mlen = sentMessageSizes[j]; Assert.AreEqual(sentLengths[j], receivedBuffer.Count); Assert.AreEqual(0x01, receivedBuffer[0] & 0x01); Assert.AreEqual(0x80, receivedBuffer[0] & 0x80); Assert.AreEqual(0x00, receivedBuffer[1] & 0x80); int skip = 2; if (mlen < 126) Assert.AreEqual(mlen, (int)(receivedBuffer[1] & 0x7F)); else if (mlen < 65536) { Assert.AreEqual(126, (int)(receivedBuffer[1] & 0x7F)); var sizeData = receivedBuffer.ToArrayData(2, 2); Assert.AreEqual(mlen, (int)sizeData[0] * 256 + (int)sizeData[1]); skip += 2; } else { Assert.AreEqual(127, (int)(receivedBuffer[1] & 0x7F)); var sizeData = receivedBuffer.ToArrayData(2, 8); long len = 0; int n = 1; for (int k = 7; k >= 0; k--) { len += (int)sizeData[k] * n; n *= 256; } Assert.AreEqual(mlen, len); skip += 8; } Assert.AreEqual(sentMessages[j], Encoding.UTF8.GetString(receivedBuffer.ToArrayData(skip, mlen))); receivedBuffer.ClearSegements(); Console.WriteLine("Passed " + j); } } socket.Shutdown(SocketShutdown.Both); socket.Close(); }
public void TestIndexOf() { ArraySegmentList<char> source = new ArraySegmentList<char>(); source.AddSegment("I love you,".ToCharArray(), 3, 7); source.AddSegment("Hello world!".ToCharArray(), 0, 4); //string exptected = "ove youHell"; Assert.AreEqual(-1, source.IndexOf('I')); Assert.AreEqual(9, source.IndexOf('l')); Assert.AreEqual(0, source.IndexOf('o')); Assert.AreEqual(1, source.IndexOf('v')); }