private static FileStream TryCreateFileStream(string file) { try { var share = FileShare.ReadWrite | FileShare.Delete; return(new FileStream(file, FileMode.Open, FileAccess.Read, share, 4096, FileOptions.SequentialScan)); } catch (Exception ex) { _Log.WarnFormat("Unable to open file: {0}", ex, file); } return(null); }
/// <summary> /// Callback method called by the NetworkStream's thread when a message /// arrives. /// </summary> /// <param name="state">The state object holding information about /// the connection.</param> private void ReceiveComplete(IAsyncResult state) { try { int bytesReceived = 0; using (new ReadOnlyLock(_socketLock)) { if (_TcpClient != null) { bytesReceived = _TcpClient.GetStream().EndRead(state); } } //if there are bytes to process, do so. Otherwise, the //connection has been lost, so clean it up if (bytesReceived > 0) { try { //send the incoming message to the message handler _MessageHandler(this); } catch (Exception ex) { _Log.Error(string.Format("Instance {0} - {1} => Receive message handler failed.", this.GetHashCode(), _ThreadId), ex); } } // XXX: If readBytesCount == 0 --> Connection has been closed. // XXX: Access _TcpClient.Client.Available to ensure socket is still working.. //start listening again Receive(); } catch (Exception ex) { _Log.Warn("Receive failed.", ex); //the connection has been dropped so call the CloseHandler try { _SocketCloseHandler(this); } finally { _Log.WarnFormat("Instance {0} - {1} => Connection terminated, disposing..", this.GetHashCode(), _ThreadId); Dispose(); } } }
public void DeployLibrary(Stream stream, string fileName, DateTime timestamp) { var compressed = fileName.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase); fileName = compressed ? Path.GetFileNameWithoutExtension(fileName) : fileName; fileName = Path.Combine(_OutputPath, fileName); if (File.Exists(fileName)) { if (File.GetLastWriteTime(fileName) > timestamp) { return; } if (IsFileLocked(fileName)) { _Log.WarnFormat("Unable to update {0}: file in use!", fileName); return; } } _Log.InfoFormat("Deploying embedded {0} to {1}..", Path.GetFileName(fileName), _OutputPath); byte[] hash = null; using (var input = compressed ? new GZipStream(stream, CompressionMode.Decompress, false) : stream) using (var output = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { hash = CopyStream(input, output); } _Log.InfoFormat("Deployed {0} with md5sum: {1}.", fileName, string.Concat(hash.Select(b => b.ToString("X2")).ToArray())); if (Environment.OSVersion.Platform == PlatformID.Unix) { // Set as executable (only applies to mono).. var attrs = File.GetAttributes(fileName); File.SetAttributes(fileName, (FileAttributes)((uint)attrs | 0x80000000)); } }
private static void DeployLibrary(string resource) { var resourcePath = GetResourcePath(); var fileName = resource.Substring(resourcePath.Length + 1); var compressed = fileName.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase); fileName = compressed ? Path.GetFileNameWithoutExtension(fileName) : fileName; fileName = Path.Combine(_OutputPath, fileName); if (File.Exists(fileName)) { if (File.GetLastWriteTime(fileName) > File.GetLastWriteTime(Assembly.Location)) { return; } if (IsFileLocked(fileName)) { _Log.WarnFormat("Unable to update {0}: file in use!", fileName); return; } } _Log.InfoFormat("Deploying embedded {0} to {1}..", Path.GetFileName(fileName), _OutputPath); var res = Assembly.GetManifestResourceStream(resource); using (var input = compressed ? new GZipStream(res, CompressionMode.Decompress, false) : res) using (var output = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { CopyStream(input, output); } if (Environment.OSVersion.Platform == PlatformID.Unix) { // Set as executable (only applies to mono).. var attrs = File.GetAttributes(fileName); File.SetAttributes(fileName, (FileAttributes)((uint)attrs | 0x80000000)); } }
public void WarnFormat(IFormatProvider provider, String format, params Object[] args) { _log.WarnFormat(provider, format, args); }
private PDU WaitPDU() { PDUHeader header = null; PDU pdu = null; byte[] bodyBytes = null; byte[] headerBytes = null; //-- try { headerBytes = ReadHeaderBytes(); } catch (TcpIpException tcpIp_ex_1) { _Log.ErrorFormat("200010:TCP/IP Exception encountered while reading pdu header bytes: {0}", tcpIp_ex_1, tcpIp_ex_1.Message); if (vTraceSwitch.TraceInfo) { Trace.WriteLine(string.Format( "200010:TCP/IP Exception encountered while reading pdu header bytes:{0};", tcpIp_ex_1.Message)); } HandleException(tcpIp_ex_1); throw; } //-- header = PDUHeader.Parse(new ByteBuffer(headerBytes), vSmppEncodingService); _Log.TraceFormat("PDU header: {0}", Logging.LoggingExtensions.DumpString(header, vSmppEncodingService)); try { pdu = PDU.CreatePDU(header, vSmppEncodingService); } catch (InvalidPDUCommandException inv_ex) { ByteBuffer iBuffer = new ByteBuffer((int)header.CommandLength); iBuffer.Append(header.GetBytes(vSmppEncodingService)); if (header.CommandLength > 16) { try { iBuffer.Append(ReadBodyBytes((int)header.CommandLength - 16)); } catch (TcpIpException tcpIp_ex_3) { HandleException(tcpIp_ex_3); } } _Log.WarnFormat("200011:Invalid PDU command type:{0};", iBuffer.DumpString()); if (vTraceSwitch.TraceWarning) { Trace.WriteLine(string.Format( "200011:Invalid PDU command type:{0};", iBuffer.DumpString())); } RaisePduErrorEvent(inv_ex, iBuffer.ToBytes(), header, null); throw; } //-- try { bodyBytes = ReadBodyBytes((int)header.CommandLength - 16); } catch (TcpIpException tpcIp_ex_2) { _Log.ErrorFormat("200012:TCP/IP Exception encountered while reading pdu body bytes: {0}", tpcIp_ex_2, tpcIp_ex_2.Message); if (vTraceSwitch.TraceInfo) { Trace.WriteLine(string.Format( "200012:TCP/IP Exception encountered while reading pdu body bytes:{0};", tpcIp_ex_2.Message)); } HandleException(tpcIp_ex_2); throw; } //-- try { pdu.SetBodyData(new ByteBuffer(bodyBytes)); } catch (PDUException pdu_ex) { ByteBuffer pBuffer = new ByteBuffer((int)header.CommandLength); pBuffer.Append(headerBytes); pBuffer.Append(bodyBytes); RaisePduErrorEvent(pdu_ex, pBuffer.ToBytes(), header, pdu); _Log.WarnFormat("200013:Malformed PDU body received:{0}", pdu_ex, pBuffer.DumpString()); if (vTraceSwitch.TraceWarning) { Trace.WriteLine(string.Format( "200013:Malformed PDU body received:{0} {1};", pBuffer.DumpString(), pdu_ex.Message)); } throw; } return(pdu); }