public CopyTo ( byte dest, int offset_into_dest ) : int | ||
dest | byte | |
offset_into_dest | int | |
Résultat | int |
public void Test() { System.Random r = new System.Random(); byte[] data; for (int i = 0; i < 100; i++) { data = new byte[r.Next(1024)]; r.NextBytes(data); int offset = r.Next(data.Length); MemBlock mb1 = new MemBlock(data, 0, data.Length); MemBlock mb1a = MemBlock.Copy(data, 0, data.Length); Assert.IsTrue(mb1.Equals(mb1a), "MemBlock.Copy"); Assert.IsTrue(mb1.Equals(data), "MemBlock == byte[]"); MemBlock mb2 = new MemBlock(data, offset, data.Length - offset); MemBlock mb2a = mb1.Slice(offset); MemBlock mb3 = new MemBlock(data, 0, offset); MemBlock mb3a = mb1.Slice(0, offset); Assert.IsTrue(mb3.Equals(mb3a), "mb3.Equals(mb3a)"); Assert.IsTrue(mb3a.Equals(mb3), "mb3a.Equals(mb3)"); Assert.IsTrue(mb3.CompareTo(mb2) + mb2.CompareTo(mb3) == 0, "CompareTo"); Assert.IsTrue(mb2.Equals(mb2a), "mb2.Equals(mb2a)"); Assert.IsTrue(mb2a.Equals(mb2), "mb2a.Equals(mb2)"); MemBlock cat = MemBlock.Concat(mb3, mb2); MemBlock cata = MemBlock.Concat(mb3a, mb2a); Assert.IsTrue(cat.Equals(cata), "Concat Equals"); Assert.IsTrue(cata.Equals(cat), "Concat a Equals"); Assert.IsTrue(mb1.Equals(cat), "Concat Equals Original"); if (offset != 0) { //These should not be equal Assert.IsFalse(mb2.Equals(mb1), "mb2 != mb1"); } int mb2a_l = mb2a.Length; byte[] tmp_data = new byte[mb2a_l]; mb2a.CopyTo(tmp_data, 0); MemBlock mb2b = new MemBlock(tmp_data, 0, tmp_data.Length); Assert.IsTrue(mb2a.Equals(mb2b), "mb2a.Equals(mb2b)"); Assert.IsTrue(mb2b.Equals(mb2a), "mb2b.Equals(mb2a)"); //Check the Hash: Assert.IsTrue(mb2b.GetHashCode() == mb2a.GetHashCode(), "GetHashCode"); //Here are some manual equality testing using the indexer bool all_equals = true; int j = 0; while (all_equals && (j < mb1.Length)) { all_equals = (mb1[j] == cat[j]); j++; } Assert.IsTrue(all_equals, "Manual equality test mb1"); all_equals = true; j = 0; while (all_equals && (j < mb2.Length)) { all_equals = (mb2[j] == mb2a[j]); j++; } Assert.IsTrue(all_equals, "Manual equality test mb2"); all_equals = true; j = 0; while (all_equals && (j < mb2.Length)) { all_equals = (mb2[j] == mb2b[j]); j++; } Assert.IsTrue(all_equals, "Manual equality test mb2b"); } }
override protected bool HandleIncoming(MemBlock data, out MemBlock app_data) { app_data = null; int count = 0; lock(_buffer_sync) { if(data != null) { data.CopyTo(_buffer, 0); _read.Write(_buffer, data.Length); } count = _ssl.Read(_buffer, _buffer.Length); if(count > 0) { app_data = MemBlock.Copy(_buffer, 0, count); } } if(app_data != null) { // If the read was successful, Dtls has received an incoming data // message and decrypted it return true; } else { SslError error = _ssl.GetError(count); if(error == SslError.SSL_ERROR_WANT_READ) { if(SslState == SslState.OK) { UpdateState(States.Active); // In the SslCtx verify, there's no way to get the underlying Sender _ch.Verify(RemoteCertificate, Sender); } HandleWouldBlock(); } else if(error == SslError.SSL_ERROR_SSL) { var ose = new OpenSslException(); Close("Received unrecoverable error: " + ose.ToString()); throw ose; } else if(error == SslError.SSL_ERROR_ZERO_RETURN) { Close("Received clean close notification"); } else { ProtocolLog.WriteIf(ProtocolLog.SecurityExceptions, "Receive other: " + error); } } return false; }
/// <summary>Create a new Attribute.</summary> public Attribute(AttributeType type, MemBlock value) { Type = type; Value = value; byte[] data = new byte[4 + value.Length]; NumberSerializer.WriteUShort((ushort) type, data, 0); NumberSerializer.WriteUShort((ushort) value.Length, data, 2); value.CopyTo(data, 4); Data = MemBlock.Reference(data); }