public static void WriteTypeCodeIfTrue(this Protocol16Stream output, Protocol16Type type, bool writeTypeCode) { if (writeTypeCode) { output.WriteByte((byte)type); } }
private void TestDeserializeMethod(byte[] byteInput, object expected) { var input = new Protocol16Stream(byteInput); var actual = Protocol16Deserializer.Deserialize(input); Assert.AreEqual(expected, actual); }
public void SerializeSuperLongArray() { var actual = new Protocol16Stream(); float[] input = new float[short.MaxValue + 1]; Assert.Throws <NotSupportedException>( () => { Protocol16Serializer.Serialize(actual, input, true); }); }
public void SerializeSuperLongString() { var actual = new Protocol16Stream(); string input = string.Empty.PadLeft(short.MaxValue + 1, '0'); Assert.Throws <NotSupportedException>( () => { Protocol16Serializer.Serialize(actual, input, true); }); }
private void TestSerializeMethod(byte[] expectedBytes, object obj) { var expected = new MemoryStream(expectedBytes); var actual = new Protocol16Stream(); Protocol16Serializer.Serialize(actual, obj, true); ToString(actual); Assert.AreEqual(expected, actual); }
public void DeserializeEventData() { var buffer = new byte[] { 0x65, 0x64, 0x00, 0x02, 0x00, 0x73, 0x00, 0x05, 0x74, 0x65, 0x73, 0x74, 0x31, 0x01, 0x73, 0x00, 0x05, 0x74, 0x65, 0x73, 0x74, 0x32, }; var stream = new Protocol16Stream(buffer); EventData result = Protocol16Deserializer.Deserialize(stream) as EventData; Assert.NotNull(result); Assert.AreEqual(100, result.Code); Assert.AreEqual("test1", result.Parameters[0]); Assert.AreEqual("test2", result.Parameters[1]); }
public void DeserializeOperationRequest() { var buffer = new byte[] { 0x71, 0x64, 0x00, 0x02, 0x00, 0x73, 0x00, 0x05, 0x74, 0x65, 0x73, 0x74, 0x31, 0x01, 0x73, 0x00, 0x05, 0x74, 0x65, 0x73, 0x74, 0x32, }; var stream = new Protocol16Stream(buffer); byte typeCode = (byte)stream.ReadByte(); OperationRequest result = Protocol16Deserializer.Deserialize(stream, typeCode) as OperationRequest; Assert.NotNull(result); Assert.AreEqual(100, result.OperationCode); Assert.AreEqual("test1", result.Parameters[0]); Assert.AreEqual("test2", result.Parameters[1]); }
public void DeserializeOperationResponse() { var buffer = new byte[] { 0x70, 0x64, 0x00, 0x65, 0x2a, 0x00, 0x02, 0x00, 0x73, 0x00, 0x05, 0x74, 0x65, 0x73, 0x74, 0x31, 0x01, 0x73, 0x00, 0x05, 0x74, 0x65, 0x73, 0x74, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; var stream = new Protocol16Stream(buffer); byte typeCode = (byte)stream.ReadByte(); OperationResponse result = Protocol16Deserializer.Deserialize(stream, typeCode) as OperationResponse; Assert.NotNull(result); Assert.AreEqual(result.OperationCode, 100); Assert.AreEqual(result.ReturnCode, 101); Assert.AreEqual(result.Parameters[0], "test1"); Assert.AreEqual(result.Parameters[1], "test2"); }
private void HandleSendReliable(byte[] source, ref int offset, ref int commandLength) { // Skip 1 byte offset++; commandLength--; ReadByte(out byte messageType, source, ref offset); commandLength--; int operationLength = commandLength; var payload = new Protocol16Stream(operationLength); payload.Write(source, offset, operationLength); payload.Seek(0L, SeekOrigin.Begin); offset += operationLength; switch ((MessageType)messageType) { case MessageType.OperationRequest: { OperationRequest requestData = Protocol16Deserializer.DeserializeOperationRequest(payload); OnRequest(requestData.OperationCode, requestData.Parameters); break; } case MessageType.OperationResponse: { OperationResponse responseData = Protocol16Deserializer.DeserializeOperationResponse(payload); OnResponse(responseData.OperationCode, responseData.ReturnCode, responseData.DebugMessage, responseData.Parameters); break; } case MessageType.Event: { EventData eventData = Protocol16Deserializer.DeserializeEventData(payload); OnEvent(eventData.Code, eventData.Parameters); break; } } }
public void DeserializeOperationResponseWithDebugMessage() { var buffer = new byte[] { 0x70, 0x64, 0x00, 0x66, 0x73, 0x00, 0x11, 0x53, 0x6F, 0x6D, 0x65, 0x44, 0x65, 0x62, 0x75, 0x67, 0x20, 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00, 0x02, 0x00, 0x73, 0x00, 0x05, 0x74, 0x65, 0x73, 0x74, 0x31, 0x01, 0x69, 0x00, 0x00, 0x00, 0x02, }; var stream = new Protocol16Stream(buffer); byte typeCode = (byte)stream.ReadByte(); OperationResponse result = Protocol16Deserializer.Deserialize(stream, typeCode) as OperationResponse; Assert.NotNull(result); Assert.AreEqual(result.OperationCode, 100); Assert.AreEqual(result.ReturnCode, 102); Assert.AreEqual(result.DebugMessage, "SomeDebug message"); Assert.AreEqual(result.Parameters[0], "test1"); Assert.AreEqual(result.Parameters[1], 2); }
private void HandleSendReliable(byte[] source, ref int offset, ref int commandLength) { if (commandLength < 0) { Debug.WriteLine("Something wrong with packet | content=" + Convert.ToBase64String(source)); } byte[] payloadBytes = new byte[commandLength]; Array.Copy(source, offset, payloadBytes, 0, commandLength); // Skip 1 byte offset += commandLength; int index = 0; ReadByte(out byte flag, payloadBytes, ref index); bool flag3 = flag != 243 && flag != 253; if (flag3) { return; } ReadByte(out byte messageDeets, payloadBytes, ref index); byte messageType = (byte)(messageDeets & 127); bool special = (messageDeets & 128) > 0; Protocol16Stream payload; if (!special) { int operationLength = payloadBytes.Length - index; payload = new Protocol16Stream(operationLength); payload.Write(payloadBytes, index, operationLength); payload.Seek(0L, SeekOrigin.Begin); } else { byte[] actualContent = new byte[payloadBytes.Length - index]; Array.Copy(payloadBytes, index, actualContent, 0, actualContent.Length); byte[] result = OnSpecial(actualContent); if (result.Length == 0) { return; } int operationLength = result.Length; payload = new Protocol16Stream(operationLength); payload.Write(result, 0, operationLength); payload.Seek(0L, SeekOrigin.Begin); } switch ((MessageType)messageType) { case MessageType.OperationRequest: { OperationRequest requestData = Protocol16Deserializer.DeserializeOperationRequest(payload); OnRequest(requestData.OperationCode, requestData.Parameters); break; } case MessageType.OperationResponse: { OperationResponse responseData = Protocol16Deserializer.DeserializeOperationResponse(payload); OnResponse(responseData.OperationCode, responseData.ReturnCode, responseData.DebugMessage, responseData.Parameters); break; } case MessageType.Event: { EventData eventData = Protocol16Deserializer.DeserializeEventData(payload); OnEvent(eventData.Code, eventData.Parameters); break; } case MessageType.InternalOperationRequest: { OperationRequest intReqData = Protocol16Deserializer.DeserializeOperationRequest(payload); intReqData.Parameters.Add(88, TickCount); OnRequest(intReqData.OperationCode, intReqData.Parameters); break; } case MessageType.InternalOperationResponse: { OperationResponse intResponseData = Protocol16Deserializer.DeserializeOperationResponse(payload); OnResponse(intResponseData.OperationCode, intResponseData.ReturnCode, intResponseData.DebugMessage, intResponseData.Parameters); break; } } }