/// <summary> /// Registers the domain objects async. /// </summary> private void RegisterDomainObjectsAsync() { using (var wrk = new BackgroundWorker()) { wrk.DoWork += (s, e) => { try { RegisterDomainObjects(); } catch (Exception ex) { FxLog <CloudServiceHostFactory> .Error("Error registering domain objects (Async): \n{0}", ex); wrk.Dispose(); throw; } }; wrk.RunWorkerCompleted += (s, e) => { wrk.Dispose(); }; wrk.RunWorkerAsync(); } }
// Statics methods goes here. #endregion #region Publics /// <summary> /// Resolves the DNS host name or IP address to an IPHostEntry instance. /// </summary> public void Resolve() { try { IPHostEntry ipHostInfo; if (String.IsNullOrEmpty(_host)) { // Get the Hostname given the IP Address. ipHostInfo = Dns.GetHostEntry(_ip); _host = ipHostInfo.HostName; } else { // Get the IP Address given the Hostname. ipHostInfo = Dns.GetHostEntry(_host); _ip = ipHostInfo.AddressList[0]; } _endpoint = new IPEndPoint(_ip, _port); } catch (Exception ex) { FxLog<AsyncTcpClient> .Error("Exception raised trying to resolve the DNS host name or IP address to an IPHostEntry instance.", ex); } }
/// <summary> /// Connect to the remote host.<br/> /// SendTimeout is set to 3000ms by default. /// </summary> /// <remarks> /// As the connection is made asynchronously a call to <see cref="Send"/> may failed if this call is /// made right after the Connect().<br /> /// </remarks> public void Connect() { try { Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.SendTimeout = 3000; newsock.BeginConnect(_endpoint, new AsyncCallback(EndConnect), newsock); } catch (Exception ex) { FxLog<AsyncTcpClient> .Error("Exception raised trying to connect to the remote host.", ex); } }
/// <summary> /// Send data to the remote host (async method). /// </summary> /// <param name="data"></param> public void Send(byte[] data) { try { if (_client != null && _client.Connected) { _client.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendData), _client); } } catch (Exception ex) { if (Error != null) { Error(this, new AsyncTcpClientEventArgs(null, ex.GetBaseException())); } FxLog<AsyncTcpClient> .Error("Exception raised trying to send message to the remote host.", ex); } }
/// <summary> /// Close the connection. /// </summary> public void Close() { try { if (_client != null && _client.Connected) { // Disconnected Event are raised only if a "real" disconnection occurred (not when Stop() is called). _stopping = true; _client.Shutdown(SocketShutdown.Both); _client.Disconnect(false); _client.Close(); _client = null; _stopping = false; } } catch (Exception ex) { FxLog<AsyncTcpClient> .Error("Exception raised trying to close the connection.", ex); } }