public static async Task RequestData(ClientOpcode opcode) { if (!OpcodesBinding.RequestResponseOpcodes.ContainsKey(opcode)) { throw new ArgumentException("RequestResponseOpcodes doesn`t contain the response type"); } Packet packet = new Packet(opcode, new byte[0]); Type responseType = OpcodesBinding.RequestResponseOpcodes[opcode]; if (responseType != typeof(EmptyResponse)) { if (!_dataUpdateNotifiers.ContainsKey(responseType)) { _dataUpdateNotifiers[responseType] = new EventWaitHandle(false, EventResetMode.ManualReset); } _dataUpdateNotifiers[responseType].Reset(); } try { await _controllerClient.SendAsync(_client, packet); } catch (SocketException ex) { _connected = false; _client.Disconnect(); throw ex; } if (responseType != typeof(EmptyResponse)) { bool isSignaled = await AsyncFactory.FromWaitHandle( _dataUpdateNotifiers[responseType], TimeSpan.FromMilliseconds(Config.DataTimeout)); if (!isSignaled) { throw new TimeoutException("Server didn`t respont in time"); } } }
private void DisconnectFromServer() { Client.Disconnect(); IsConnected = false; button1.Text = "Connect"; }