From: <che...@us...> - 2010-09-08 23:01:08
|
Revision: 3785 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=3785&view=rev Author: chef_koch Date: 2010-09-08 23:01:01 +0000 (Wed, 08 Sep 2010) Log Message: ----------- reduced redundant code by moving more to IrssComms/IrssMessage.cs Modified Paths: -------------- trunk/plugins/IR Server Suite/IR Server Suite/Applications/Debug Client/MainForm.cs trunk/plugins/IR Server Suite/IR Server Suite/Applications/Keyboard Input Relay/Program.cs trunk/plugins/IR Server Suite/IR Server Suite/Applications/Translator/Program.cs trunk/plugins/IR Server Suite/IR Server Suite/Applications/Virtual Remote/Program.cs trunk/plugins/IR Server Suite/IR Server Suite/Applications/Web Remote/Program.cs trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssComms/IrssMessage.cs trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server/IR Server.cs Modified: trunk/plugins/IR Server Suite/IR Server Suite/Applications/Debug Client/MainForm.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/Applications/Debug Client/MainForm.cs 2010-09-08 13:15:11 UTC (rev 3784) +++ trunk/plugins/IR Server Suite/IR Server Suite/Applications/Debug Client/MainForm.cs 2010-09-08 23:01:01 UTC (rev 3785) @@ -514,16 +514,8 @@ return; } - byte[] deviceNameBytes = Encoding.ASCII.GetBytes(textBoxRemoteDevice.Text); - byte[] keyCodeBytes = Encoding.ASCII.GetBytes(textBoxRemoteCode.Text); - - byte[] bytes = new byte[8 + deviceNameBytes.Length + keyCodeBytes.Length]; - - BitConverter.GetBytes(deviceNameBytes.Length).CopyTo(bytes, 0); - deviceNameBytes.CopyTo(bytes, 4); - BitConverter.GetBytes(keyCodeBytes.Length).CopyTo(bytes, 4 + deviceNameBytes.Length); - keyCodeBytes.CopyTo(bytes, 8 + deviceNameBytes.Length); - + byte[] bytes = IrssMessage.EncodeRemoteEventData(textBoxRemoteDevice.Text, textBoxRemoteCode.Text); + IrssMessage message = new IrssMessage(MessageType.ForwardRemoteEvent, MessageFlags.Notify, bytes); _client.Send(message); } Modified: trunk/plugins/IR Server Suite/IR Server Suite/Applications/Keyboard Input Relay/Program.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/Applications/Keyboard Input Relay/Program.cs 2010-09-08 13:15:11 UTC (rev 3784) +++ trunk/plugins/IR Server Suite/IR Server Suite/Applications/Keyboard Input Relay/Program.cs 2010-09-08 23:01:01 UTC (rev 3785) @@ -394,16 +394,8 @@ if (_registered) { - byte[] deviceNameBytes = Encoding.ASCII.GetBytes("Keyboard"); - byte[] keyCodeBytes = Encoding.ASCII.GetBytes(String.Format("{0:X8}", keyCode)); + byte[] bytes = IrssMessage.EncodeRemoteEventData("Keyboard", String.Format("{0:X8}", keyCode)); - byte[] bytes = new byte[8 + deviceNameBytes.Length + keyCodeBytes.Length]; - - BitConverter.GetBytes(deviceNameBytes.Length).CopyTo(bytes, 0); - deviceNameBytes.CopyTo(bytes, 4); - BitConverter.GetBytes(keyCodeBytes.Length).CopyTo(bytes, 4 + deviceNameBytes.Length); - keyCodeBytes.CopyTo(bytes, 8 + deviceNameBytes.Length); - IrssMessage message = new IrssMessage(MessageType.ForwardRemoteEvent, MessageFlags.Notify, bytes); _client.Send(message); } Modified: trunk/plugins/IR Server Suite/IR Server Suite/Applications/Translator/Program.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/Applications/Translator/Program.cs 2010-09-08 13:15:11 UTC (rev 3784) +++ trunk/plugins/IR Server Suite/IR Server Suite/Applications/Translator/Program.cs 2010-09-08 23:01:01 UTC (rev 3785) @@ -794,31 +794,29 @@ switch (received.Type) { case MessageType.RemoteEvent: - string deviceName = received.MessageData[IrssMessage.DEVICE_NAME] as string; - string keyCode = received.MessageData[IrssMessage.KEY_CODE] as string; + { + string deviceName = received.MessageData[IrssMessage.DEVICE_NAME] as string; + string keyCode = received.MessageData[IrssMessage.KEY_CODE] as string; - RemoteHandlerCallback(deviceName, keyCode); + RemoteHandlerCallback(deviceName, keyCode); + } break; case MessageType.KeyboardEvent: { - byte[] dataBytes = received.GetDataAsBytes(); + int vKey = (int)received.MessageData[IrssMessage.V_KEY]; + bool keyUp = (bool)received.MessageData[IrssMessage.KEY_UP]; - int vKey = BitConverter.ToInt32(dataBytes, 0); - bool keyUp = BitConverter.ToBoolean(dataBytes, 4); - KeyboardHandlerCallback("TODO", vKey, keyUp); break; } case MessageType.MouseEvent: { - byte[] dataBytes = received.GetDataAsBytes(); + int deltaX = (int)received.MessageData[IrssMessage.DELTA_X]; + int deltaY = (int)received.MessageData[IrssMessage.DELTA_Y]; + int buttons = (int)received.MessageData[IrssMessage.BUTTONS]; - int deltaX = BitConverter.ToInt32(dataBytes, 0); - int deltaY = BitConverter.ToInt32(dataBytes, 4); - int buttons = BitConverter.ToInt32(dataBytes, 8); - MouseHandlerCallback("TODO", deltaX, deltaY, buttons); break; } Modified: trunk/plugins/IR Server Suite/IR Server Suite/Applications/Virtual Remote/Program.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/Applications/Virtual Remote/Program.cs 2010-09-08 13:15:11 UTC (rev 3784) +++ trunk/plugins/IR Server Suite/IR Server Suite/Applications/Virtual Remote/Program.cs 2010-09-08 23:01:01 UTC (rev 3785) @@ -415,16 +415,8 @@ if (!_registered) return; - byte[] deviceNameBytes = Encoding.ASCII.GetBytes(_device); - byte[] keyCodeBytes = Encoding.ASCII.GetBytes(keyCode); + byte[] bytes = IrssMessage.EncodeRemoteEventData(_device, keyCode); - byte[] bytes = new byte[8 + deviceNameBytes.Length + keyCodeBytes.Length]; - - BitConverter.GetBytes(deviceNameBytes.Length).CopyTo(bytes, 0); - deviceNameBytes.CopyTo(bytes, 4); - BitConverter.GetBytes(keyCodeBytes.Length).CopyTo(bytes, 4 + deviceNameBytes.Length); - keyCodeBytes.CopyTo(bytes, 8 + deviceNameBytes.Length); - IrssMessage message = new IrssMessage(MessageType.ForwardRemoteEvent, MessageFlags.Request, bytes); SendMessage(message); } Modified: trunk/plugins/IR Server Suite/IR Server Suite/Applications/Web Remote/Program.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/Applications/Web Remote/Program.cs 2010-09-08 13:15:11 UTC (rev 3784) +++ trunk/plugins/IR Server Suite/IR Server Suite/Applications/Web Remote/Program.cs 2010-09-08 23:01:01 UTC (rev 3785) @@ -278,16 +278,8 @@ if (!_registered) return; - byte[] deviceNameBytes = Encoding.ASCII.GetBytes(_device); - byte[] keyCodeBytes = Encoding.ASCII.GetBytes(keyCode); + byte[] bytes = IrssMessage.EncodeRemoteEventData(_device, keyCode); - byte[] bytes = new byte[8 + deviceNameBytes.Length + keyCodeBytes.Length]; - - BitConverter.GetBytes(deviceNameBytes.Length).CopyTo(bytes, 0); - deviceNameBytes.CopyTo(bytes, 4); - BitConverter.GetBytes(keyCodeBytes.Length).CopyTo(bytes, 4 + deviceNameBytes.Length); - keyCodeBytes.CopyTo(bytes, 8 + deviceNameBytes.Length); - IrssMessage message = new IrssMessage(MessageType.ForwardRemoteEvent, MessageFlags.Notify, bytes); SendMessage(message); } Modified: trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssComms/IrssMessage.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssComms/IrssMessage.cs 2010-09-08 13:15:11 UTC (rev 3784) +++ trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssComms/IrssMessage.cs 2010-09-08 23:01:01 UTC (rev 3785) @@ -197,16 +197,27 @@ #region Const public const string DATA = "DATA"; + + // remote public const string DEVICE_NAME = "DEVICE_NAME"; public const string KEY_CODE = "KEY_CODE"; + // keyboard + public const string V_KEY = "V_KEY"; + public const string KEY_UP = "KEY_UP"; + + // mouse + public const string DELTA_X = "DELTA_X"; + public const string DELTA_Y = "DELTA_Y"; + public const string BUTTONS = "BUTTONS"; + #endregion #region Protected fields protected MessageType _messageType; - protected IDictionary<string, object> _messageData = new Dictionary<string, object>(); protected MessageFlags _messageFlags; + protected IDictionary<string, object> _messageData; #endregion @@ -304,7 +315,7 @@ /// <summary> /// Set message data as bytes. /// </summary> - private void SetDataAsBytes(byte[] data) + public void SetDataAsBytes(byte[] data) { if (data == null) _messageData[DATA] = null; @@ -327,7 +338,7 @@ /// <summary> /// Set message data as string. /// </summary> - private void SetDataAsString(string data) + public void SetDataAsString(string data) { if (String.IsNullOrEmpty(data)) _messageData[DATA] = null; @@ -345,15 +356,38 @@ switch (_messageType) { case MessageType.RemoteEvent: - byte[] data = GetDataAsBytes(); + case MessageType.ForwardRemoteEvent: + { + byte[] data = GetDataAsBytes(); - int deviceNameSize = BitConverter.ToInt32(data, 0); - _messageData[DEVICE_NAME] = System.Text.Encoding.ASCII.GetString(data, 4, deviceNameSize); + int deviceNameSize = BitConverter.ToInt32(data, 0); + _messageData[DEVICE_NAME] = System.Text.Encoding.ASCII.GetString(data, 4, deviceNameSize); - int keyCodeSize = BitConverter.ToInt32(data, 4 + deviceNameSize); - _messageData[KEY_CODE] = System.Text.Encoding.ASCII.GetString(data, 8 + deviceNameSize, keyCodeSize); + int keyCodeSize = BitConverter.ToInt32(data, 4 + deviceNameSize); + _messageData[KEY_CODE] = System.Text.Encoding.ASCII.GetString(data, 8 + deviceNameSize, keyCodeSize); + } + break; + case MessageType.KeyboardEvent: + case MessageType.ForwardKeyboardEvent: + { + byte[] data = GetDataAsBytes(); + + _messageData[V_KEY] = BitConverter.ToInt32(data, 0); + _messageData[KEY_UP] = BitConverter.ToBoolean(data, 4); + } break; + + case MessageType.MouseEvent: + case MessageType.ForwardMouseEvent: + { + byte[] data = GetDataAsBytes(); + + _messageData[DELTA_X] = BitConverter.ToInt32(data, 0); + _messageData[DELTA_Y] = BitConverter.ToInt32(data, 4); + _messageData[BUTTONS] = BitConverter.ToInt32(data, 8); + } + break; } } @@ -384,7 +418,7 @@ /// </summary> /// <param name="from">Byte array to generate message from.</param> /// <returns>New Message.</returns> - public static IrssMessage FromBytes(byte[] from) + internal static IrssMessage FromBytes(byte[] from) { if (from == null) throw new ArgumentNullException("from"); @@ -404,6 +438,42 @@ return new IrssMessage(type, flags, data); } + public static byte[] EncodeRemoteEventData(string deviceName, string keyCode) + { + byte[] deviceNameBytes = Encoding.ASCII.GetBytes(deviceName); + byte[] keyCodeBytes = Encoding.ASCII.GetBytes(keyCode); + + byte[] bytes = new byte[8 + deviceNameBytes.Length + keyCodeBytes.Length]; + + BitConverter.GetBytes(deviceNameBytes.Length).CopyTo(bytes, 0); + deviceNameBytes.CopyTo(bytes, 4); + BitConverter.GetBytes(keyCodeBytes.Length).CopyTo(bytes, 4 + deviceNameBytes.Length); + keyCodeBytes.CopyTo(bytes, 8 + deviceNameBytes.Length); + + return bytes; + } + + public static byte[] EncodeKeyboardEventData(int vKey, bool keyUp) + { + byte[] bytes = new byte[8]; + + BitConverter.GetBytes(vKey).CopyTo(bytes, 0); + BitConverter.GetBytes(keyUp).CopyTo(bytes, 4); + + return bytes; + } + + public static byte[] EncodeMouseEventData(int deltaX, int deltaY, int buttons) + { + byte[] bytes = new byte[12]; + + BitConverter.GetBytes(deltaX).CopyTo(bytes, 0); + BitConverter.GetBytes(deltaY).CopyTo(bytes, 4); + BitConverter.GetBytes(buttons).CopyTo(bytes, 8); + + return bytes; + } + #endregion Implementation } } \ No newline at end of file Modified: trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server/IR Server.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server/IR Server.cs 2010-09-08 13:15:11 UTC (rev 3784) +++ trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server/IR Server.cs 2010-09-08 23:01:01 UTC (rev 3785) @@ -798,16 +798,8 @@ return; } - byte[] deviceNameBytes = Encoding.ASCII.GetBytes(messageDeviceName); - byte[] keyCodeBytes = Encoding.ASCII.GetBytes(messageKeyCode); + byte[] bytes = IrssMessage.EncodeRemoteEventData(messageDeviceName, messageKeyCode); - byte[] bytes = new byte[8 + deviceNameBytes.Length + keyCodeBytes.Length]; - - BitConverter.GetBytes(deviceNameBytes.Length).CopyTo(bytes, 0); - deviceNameBytes.CopyTo(bytes, 4); - BitConverter.GetBytes(keyCodeBytes.Length).CopyTo(bytes, 4 + deviceNameBytes.Length); - keyCodeBytes.CopyTo(bytes, 8 + deviceNameBytes.Length); - switch (Settings.Mode) { case IRServerMode.ServerMode: @@ -830,9 +822,7 @@ { IrssLog.Debug("{0} generated a keyboard event: {1}, keyUp: {2}", deviceName, vKey, keyUp); - byte[] bytes = new byte[8]; - BitConverter.GetBytes(vKey).CopyTo(bytes, 0); - BitConverter.GetBytes(keyUp).CopyTo(bytes, 4); + byte[] bytes = IrssMessage.EncodeKeyboardEventData(vKey, keyUp); switch (Settings.Mode) { @@ -863,10 +853,7 @@ IrssLog.Debug("{0} generated a mouse Event - deltaX: {1}, deltaY: {2}, buttons: {3}", deviceName, deltaX, deltaY, buttons); - byte[] bytes = new byte[12]; - BitConverter.GetBytes(deltaX).CopyTo(bytes, 0); - BitConverter.GetBytes(deltaY).CopyTo(bytes, 4); - BitConverter.GetBytes(buttons).CopyTo(bytes, 8); + byte[] bytes = IrssMessage.EncodeMouseEventData(deltaX, deltaY, buttons); switch (Settings.Mode) { @@ -1157,10 +1144,8 @@ if (Settings.AbstractRemoteMode) { // Decode message ... - int deviceNameSize = BitConverter.ToInt32(data, 0); - string deviceName = Encoding.ASCII.GetString(data, 4, deviceNameSize); - int keyCodeSize = BitConverter.ToInt32(data, 4 + deviceNameSize); - string keyCode = Encoding.ASCII.GetString(data, 8 + deviceNameSize, keyCodeSize); + string deviceName = combo.Message.MessageData[IrssMessage.DEVICE_NAME] as string; + string keyCode = combo.Message.MessageData[IrssMessage.KEY_CODE] as string; // Check that the device maps are loaded for the forwarded device bool foundDevice = false; @@ -1190,15 +1175,7 @@ IrssLog.Info("Abstract Remote Button mapped from forwarded remote event: {0}", abstractButton); // Encode new message ... - byte[] deviceNameBytes = Encoding.ASCII.GetBytes("Abstract"); - byte[] keyCodeBytes = Encoding.ASCII.GetBytes(abstractButton); - - data = new byte[8 + deviceNameBytes.Length + keyCodeBytes.Length]; - - BitConverter.GetBytes(deviceNameBytes.Length).CopyTo(data, 0); - deviceNameBytes.CopyTo(data, 4); - BitConverter.GetBytes(keyCodeBytes.Length).CopyTo(data, 4 + deviceNameBytes.Length); - keyCodeBytes.CopyTo(data, 8 + deviceNameBytes.Length); + data = IrssMessage.EncodeRemoteEventData("Abstract", abstractButton); } else { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |