Browse Source

Refactor parsing message

pull/4418/head
Rustam Sayfutdinov 6 years ago
parent
commit
26ae25d28d
  1. 88
      src/Avalonia.DesignerSupport/Remote/HtmlTransport/HtmlTransport.cs

88
src/Avalonia.DesignerSupport/Remote/HtmlTransport/HtmlTransport.cs

@ -115,11 +115,11 @@ namespace Avalonia.DesignerSupport.Remote.HtmlTransport
while (true) while (true)
{ {
var msg = await socket.ReceiveMessage().ConfigureAwait(false); var msg = await socket.ReceiveMessage().ConfigureAwait(false);
if(msg == null) if(msg != null && msg.IsText)
return;
if (msg.IsText)
{ {
ProcessingReceiveMessage(msg.AsString()); var message = ParseMessage(msg.AsString());
if (message != null)
_onMessage?.Invoke(this, message);
} }
} }
} }
@ -178,7 +178,6 @@ namespace Avalonia.DesignerSupport.Remote.HtmlTransport
_pendingSocket?.Dispose(); _pendingSocket?.Dispose();
_simpleServer.Dispose(); _simpleServer.Dispose();
} }
public Task Send(object data) public Task Send(object data)
{ {
@ -262,70 +261,57 @@ namespace Avalonia.DesignerSupport.Remote.HtmlTransport
} }
#endregion #endregion
private void ProcessingReceiveMessage(string message) private static object ParseMessage(string message)
{ {
var parts = message.Split(':'); var parts = message.Split(':');
switch (parts[0]) switch (parts[0])
{ {
case "frame-received": case "frame-received":
{ {
_onMessage?.Invoke( return new FrameReceivedMessage { SequenceId = long.Parse(parts[1]) };
this,
new FrameReceivedMessage { SequenceId = long.Parse(parts[1]) });
break;
} }
case "pointer-released": case "pointer-released":
{ {
_onMessage?.Invoke( return new InputProtocol.PointerReleasedEventMessage
this, {
new InputProtocol.PointerReleasedEventMessage Modifiers = ParseInputModifiers(parts[1]),
{ X = ParseDouble(parts[2]),
Modifiers = ParseInputModifiers(parts[1]), Y = ParseDouble(parts[3]),
X = ParseDouble(parts[2]), Button = ParseMouseButton(parts[4]),
Y = ParseDouble(parts[3]), };
Button = ParseMouseButton(parts[4]),
});
break;
} }
case "pointer-pressed": case "pointer-pressed":
{ {
_onMessage?.Invoke( return new InputProtocol.PointerPressedEventMessage
this, {
new InputProtocol.PointerPressedEventMessage Modifiers = ParseInputModifiers(parts[1]),
{ X = ParseDouble(parts[2]),
Modifiers = ParseInputModifiers(parts[1]), Y = ParseDouble(parts[3]),
X = ParseDouble(parts[2]), Button = ParseMouseButton(parts[4]),
Y = ParseDouble(parts[3]), };
Button = ParseMouseButton(parts[4]),
});
break;
} }
case "pointer-moved": case "pointer-moved":
{ {
_onMessage?.Invoke( return new InputProtocol.PointerMovedEventMessage
this, {
new InputProtocol.PointerMovedEventMessage Modifiers = ParseInputModifiers(parts[1]),
{ X = ParseDouble(parts[2]),
Modifiers = ParseInputModifiers(parts[1]), Y = ParseDouble(parts[3]),
X = ParseDouble(parts[2]), };
Y = ParseDouble(parts[3]),
});
break;
} }
case "scroll": case "scroll":
{ {
_onMessage?.Invoke( return new InputProtocol.ScrollEventMessage
this, {
new InputProtocol.ScrollEventMessage Modifiers = ParseInputModifiers(parts[1]),
{ X = ParseDouble(parts[2]),
Modifiers = ParseInputModifiers(parts[1]), Y = ParseDouble(parts[3]),
X = ParseDouble(parts[2]), DeltaX = ParseDouble(parts[4]),
Y = ParseDouble(parts[3]), DeltaY = ParseDouble(parts[5]),
DeltaX = ParseDouble(parts[4]), };
DeltaY = ParseDouble(parts[5]),
});
break;
} }
default:
return null;
} }
} }
@ -345,6 +331,6 @@ namespace Avalonia.DesignerSupport.Remote.HtmlTransport
typeof(InputProtocol.MouseButton), buttonText, true); typeof(InputProtocol.MouseButton), buttonText, true);
private static double ParseDouble(string text) => private static double ParseDouble(string text) =>
double.Parse(text, CultureInfo.InvariantCulture); double.Parse(text, NumberStyles.Float, CultureInfo.InvariantCulture);
} }
} }

Loading…
Cancel
Save