protected void InitializeNetwork(DcmSocket socket) { _socket = socket; _socket.SendTimeout = _socketTimeout * 1000; _socket.ReceiveTimeout = _socketTimeout * 1000; _socket.ThrottleSpeed = _throttle; OnInitializeNetwork(); _network = _socket.GetStream(); _stop = false; _isRunning = true; _thread = new Thread(Process); _thread.IsBackground = true; _thread.Start(); }
private void Connect() { bool success = false; try { Log.Info("{0} -> Connecting to server at {1}:{2}", LogID, _host, _port); _socket = DcmSocket.Create(_socketType); _socket.ConnectTimeout = _connectTimeout * 1000; _socket.SendTimeout = _socketTimeout * 1000; _socket.ReceiveTimeout = _socketTimeout * 1000; _socket.ThrottleSpeed = _throttle; _socket.Connect(_host, _port); if (_socketType == DcmSocketType.TLS) Log.Info("{0} -> Authenticating SSL/TLS for server: {1}", LogID, _socket.RemoteEndPoint); OnInitializeNetwork(); _network = _socket.GetStream(); success = true; } catch (SocketException e) { if (e.SocketErrorCode == SocketError.TimedOut) Log.Error("{0} -> Connection timeout after {1} seconds", LogID, _connectTimeout); else Log.Error("{0} -> Network error: {1}", LogID, e.Message); OnNetworkError(e); OnConnectionClosed(); } catch (Exception e) { #if DEBUG Log.Error("{0} -> Processing failure: {1}", LogID, e.ToString()); #else Log.Error("{0} -> Processing failure: {1}", LogID, e.Message); #endif OnNetworkError(e); OnConnectionClosed(); } finally { if (!success) { if (_network != null) { try { _network.Close(); } catch { } _network = null; } if (_socket != null) { try { _socket.Close(); } catch { } _socket = null; } _isRunning = false; } } if (success) Process(); }