| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Net.Sockets; |
| | 4 | | using System.Net.WebSockets; |
| | 5 | | using System.Runtime.Serialization.Formatters.Binary; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using CoreOSC; |
| | 9 | | using CoreOSC.IO; |
| | 10 | | using NLog; |
| | 11 | | using ReFlex.Core.Common.Adapter; |
| | 12 | | using ReFlex.Core.Common.Interfaces; |
| | 13 | | using ReFlex.Core.Tuio.Interfaces; |
| | 14 | | using ReFlex.Core.Tuio.Util; |
| | 15 | |
|
| | 16 | | namespace ReFlex.Core.Tuio.Components |
| | 17 | | { |
| | 18 | | public class TuioSender : ITuioSender |
| | 19 | | { |
| 1 | 20 | | private static readonly Logger Log = LogManager.GetCurrentClassLogger(); |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Client for sending TUIO Messages to Server using UDP |
| | 24 | | /// </summary> |
| | 25 | | protected UdpClient UdpClient; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Client Interface for sending TUIO Messages to Server using TCP. (use <see cref="TcpClientAdapter"/> for bett |
| | 29 | | /// </summary> |
| | 30 | | protected ITcpClient TcpClient; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Client Interface for sending TUIO Messages to Server using WebSocket protocol. (use <see cref="ClientWebSock |
| | 34 | | /// </summary> |
| | 35 | | protected IClientWebSocket WsClient; |
| | 36 | |
|
| | 37 | | private string _serverAddress; |
| | 38 | | private int _serverPort; |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Specifies whether a valid <see cref="TuioConfiguration"/> has been provided and sending is enabled. |
| | 42 | | /// </summary> |
| 62 | 43 | | public bool IsInitialized { get; protected set; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Initializes communication by instantiating clients and setting server address and port. |
| | 47 | | /// If config has a valid <see cref="TransportProtocol"/> and server address and port are formally valid, <see c |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="config"><see cref="TuioConfiguration"/> specifying <see cref="TransportProtocol"/>, server addr |
| | 50 | | /// <exception cref="ArgumentException">if provided <see cref="TransportProtocol"/> has an invalid value (neithe |
| | 51 | | public virtual void Initialize(TuioConfiguration config) |
| 21 | 52 | | { |
| 21 | 53 | | if (config == null || string.IsNullOrWhiteSpace(config.ServerAddress) || config.ServerPort <= 0) |
| 5 | 54 | | { |
| 5 | 55 | | Log.Warn( |
| 5 | 56 | | $"Provided invalid value for initializing {nameof(TuioSender)}: Config: {config?.GetTuioConfiguratio |
| 5 | 57 | | return; |
| | 58 | | } |
| | 59 | |
|
| 16 | 60 | | switch (config.Transport) |
| | 61 | | { |
| | 62 | | case TransportProtocol.Udp: |
| 5 | 63 | | UdpClient = new UdpClient(config.ServerAddress, config.ServerPort); |
| 5 | 64 | | break; |
| | 65 | | case TransportProtocol.Tcp: |
| 4 | 66 | | TcpClient = new TcpClientAdapter(); |
| 4 | 67 | | break; |
| | 68 | | case TransportProtocol.WebSocket: |
| 5 | 69 | | WsClient = new ClientWebSocketAdapter(); |
| 5 | 70 | | break; |
| | 71 | | default: |
| 2 | 72 | | var msg = |
| 2 | 73 | | $"Provided {nameof(TransportProtocol)} ({config.Transport}) is not valid for configuring {nameof |
| 2 | 74 | | var exc = new ArgumentException(msg); |
| 2 | 75 | | Log.Error(exc, msg); |
| 2 | 76 | | throw exc; |
| | 77 | | } |
| | 78 | |
|
| 14 | 79 | | _serverAddress = config.ServerAddress; |
| 14 | 80 | | _serverPort = config.ServerPort; |
| 14 | 81 | | IsInitialized = true; |
| 19 | 82 | | } |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Send the provided data to the TUIO Server using UDP. |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="bundle"><see cref="OscBundle"/> containing valid TUIO messages (Usually constructed by <see cre |
| | 88 | | public virtual async Task SendUdp(OscBundle bundle) |
| 2 | 89 | | { |
| | 90 | | try |
| 2 | 91 | | { |
| 11 | 92 | | foreach (var msg in bundle.Messages) |
| 3 | 93 | | { |
| 3 | 94 | | await SendOscMessageUdp(msg); |
| 3 | 95 | | } |
| 1 | 96 | | } |
| 1 | 97 | | catch (Exception exc) |
| 1 | 98 | | { |
| 1 | 99 | | Log.Error(exc, $"Error sending osc message via UDP"); |
| 1 | 100 | | } |
| 2 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Send the provided data to the TUIO Server using TCP. |
| | 105 | | /// If not connected, the method connects to the server. If connection is successful, the data is sent |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="bundle"><see cref="OscBundle"/> containing valid TUIO messages (Usually constructed by <see cre |
| | 108 | | public virtual async Task SendTcp(OscBundle bundle) |
| 4 | 109 | | { |
| 4 | 110 | | if (TcpClient?.Connected == false) |
| 2 | 111 | | { |
| | 112 | | try |
| 2 | 113 | | { |
| 2 | 114 | | await TcpClient.ConnectAsync(_serverAddress, _serverPort); |
| 1 | 115 | | } |
| 1 | 116 | | catch (Exception exc) |
| 1 | 117 | | { |
| 1 | 118 | | Log.Error(exc); |
| 1 | 119 | | } |
| 2 | 120 | | } |
| | 121 | |
|
| 4 | 122 | | if (TcpClient?.Connected == true) |
| 2 | 123 | | { |
| 18 | 124 | | foreach (var msg in bundle.Messages) |
| 6 | 125 | | { |
| 6 | 126 | | await SendOscMessageTcp(msg); |
| 6 | 127 | | } |
| 2 | 128 | | } |
| 4 | 129 | | } |
| | 130 | |
|
| | 131 | | /// <summary> |
| | 132 | | /// Send the provided data to the TUIO Server using WebSocket protocol. |
| | 133 | | /// If Websocket status is not <see cref="WebSocketState.Open"/>, the method (re)connects to the server. |
| | 134 | | /// If connection is successful, the data is sent. |
| | 135 | | /// </summary> |
| | 136 | | /// <param name="bundle"><see cref="OscBundle"/> containing valid TUIO messages (Usually constructed by <see cre |
| | 137 | | public virtual async Task SendWebSocket(OscBundle bundle) |
| 4 | 138 | | { |
| 4 | 139 | | if (WsClient?.State != WebSocketState.Open) |
| 2 | 140 | | { |
| 2 | 141 | | var uri = new Uri($"ws://{_serverAddress}:{_serverPort}"); |
| | 142 | | try |
| 2 | 143 | | { |
| 2 | 144 | | if (WsClient != null) |
| 2 | 145 | | await WsClient.ConnectAsync(uri, CancellationToken.None); |
| 1 | 146 | | } |
| 1 | 147 | | catch (Exception exc) |
| 1 | 148 | | { |
| 1 | 149 | | Log.Error(exc); |
| 1 | 150 | | } |
| 2 | 151 | | } |
| | 152 | |
|
| 4 | 153 | | if (WsClient?.State == WebSocketState.Open) |
| 2 | 154 | | { |
| 28 | 155 | | foreach (var msg in bundle.Messages) |
| 11 | 156 | | { |
| 11 | 157 | | await SendOscMessageWebSocket(msg); |
| 11 | 158 | | } |
| 2 | 159 | | } |
| 4 | 160 | | } |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// Wrapper method for sending messages (protected to be overridden in test classes) |
| | 164 | | /// </summary> |
| | 165 | | /// <param name="msg"><see cref="OscMessage"/> to be transmitted</param> |
| | 166 | | protected virtual async Task SendOscMessageUdp(OscMessage msg) |
| 0 | 167 | | { |
| 0 | 168 | | await UdpClient.SendMessageAsync(msg); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary> |
| | 172 | | /// Wrapper method for sending messages (protected to be overridden in test classes). |
| | 173 | | /// Provided message is binary formatted using <see cref="BinaryFormatter"/> and written to TCP Stream. |
| | 174 | | /// </summary> |
| | 175 | | /// <param name="msg"><see cref="OscMessage"/> to be transmitted</param> |
| | 176 | | protected virtual async Task SendOscMessageTcp(OscMessage msg) |
| 0 | 177 | | { |
| 0 | 178 | | var format = new BinaryFormatter(); |
| | 179 | | try |
| 0 | 180 | | { |
| | 181 | |
|
| 0 | 182 | | format.Serialize(TcpClient.GetStream(), msg.Address.Value); |
| | 183 | |
|
| 0 | 184 | | foreach (var arg in msg.Arguments) |
| 0 | 185 | | { |
| 0 | 186 | | format.Serialize(TcpClient.GetStream(), arg); |
| 0 | 187 | | } |
| | 188 | |
|
| 0 | 189 | | await TcpClient.GetStream().FlushAsync(); |
| 0 | 190 | | } |
| 0 | 191 | | catch (Exception exc) |
| 0 | 192 | | { |
| 0 | 193 | | Log.Error(exc); |
| 0 | 194 | | } |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | /// <summary> |
| | 198 | | /// Wrapper method for sending messages (protected to be overridden in test classes). |
| | 199 | | /// Provided message is binary formatted using <see cref="BinaryFormatter"/> and |
| | 200 | | /// written to <see cref="MemoryStream"/> which in turn is sent asynchronously via Websockets. |
| | 201 | | /// </summary> |
| | 202 | | /// <param name="msg"><see cref="OscMessage"/> to be transmitted</param> |
| | 203 | | protected virtual async Task SendOscMessageWebSocket(OscMessage msg) |
| 0 | 204 | | { |
| 0 | 205 | | var mStream = new MemoryStream(); |
| 0 | 206 | | var format = new BinaryFormatter(); |
| 0 | 207 | | format.Serialize(mStream, msg.Address.Value); |
| 0 | 208 | | foreach (var arg in msg.Arguments) |
| 0 | 209 | | { |
| 0 | 210 | | format.Serialize(mStream, arg); |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | try |
| 0 | 214 | | { |
| 0 | 215 | | await WsClient.SendAsync(new ArraySegment<byte>(mStream.ToArray()), |
| 0 | 216 | | WebSocketMessageType.Binary, true, |
| 0 | 217 | | CancellationToken.None); |
| 0 | 218 | | } |
| 0 | 219 | | catch (Exception exc) |
| 0 | 220 | | { |
| 0 | 221 | | Log.Error(exc); |
| 0 | 222 | | } |
| 0 | 223 | | } |
| | 224 | |
|
| | 225 | | /// <summary> |
| | 226 | | /// Closes and disposes all clients (if initialized). |
| | 227 | | /// Resets <see cref="IsInitialized"/> to false. |
| | 228 | | /// </summary> |
| | 229 | | public virtual async Task StopAllConnections() |
| 3 | 230 | | { |
| 3 | 231 | | UdpClient?.Close(); |
| 3 | 232 | | TcpClient?.Close(); |
| 3 | 233 | | if (WsClient != null) |
| 1 | 234 | | await WsClient.CloseAsync(WebSocketCloseStatus.NormalClosure, "Close connection", |
| 1 | 235 | | CancellationToken.None); |
| | 236 | |
|
| 3 | 237 | | UdpClient?.Dispose(); |
| 3 | 238 | | TcpClient?.Dispose(); |
| 3 | 239 | | WsClient?.Dispose(); |
| | 240 | |
|
| 3 | 241 | | IsInitialized = false; |
| 3 | 242 | | } |
| | 243 | | } |
| | 244 | | } |