private bool SendMessage(IMessageBuffer message) { if (_connectionState == ConnectionState.Disconnected) { message.Dispose(); return(false); } _webSocketSession.Send(message.Buffer, 0, message.Count); message.Dispose(); return(true); }
/// <summary> /// Recycles this object back into the pool. /// </summary> public void Dispose() { buffer.Dispose(); ObjectCache.ReturnReader(this); isCurrentlyLoungingInAPool = true; }
static void Copy(string sourceFile, string sourceFormat, string destinationFile, string destinationFormat) { if (String.IsNullOrEmpty(sourceFile)) { throw new ArgumentException(_("sourceFile must not be empty.")); } IMessageBuffer sourceBuffer = null, destinationBuffer = null; try { var sourceBufferType = ParseMessageBufferType(sourceFile, sourceFormat); sourceBuffer = CreateMessageBuffer(sourceFile, sourceBufferType); if (!String.IsNullOrEmpty(destinationFile)) { var destinationBufferType = ParseMessageBufferType(destinationFile, destinationFormat); destinationBuffer = CreateMessageBuffer(destinationFile, destinationBufferType); if (destinationBuffer.Count > 0) { throw new InvalidOperationException( String.Format( _("Destination database {0} must be empty!"), destinationFile ) ); } } if (destinationBuffer == null) { // JSON pipe Console.WriteLine("["); var msgCount = sourceBuffer.Count; var i = 0; foreach (var msg in sourceBuffer) { var dto = new MessageDtoModelV1(msg); var json = JsonSerializer.SerializeToString(dto); if (i++ < msgCount - 1) { Console.WriteLine("{0},", json); } else { Console.WriteLine(json); } } if (destinationBuffer == null) { Console.WriteLine("]"); } } else { foreach (var msg in sourceBuffer) { destinationBuffer.Add(msg); } destinationBuffer.Flush(); } } finally { if (sourceBuffer != null) { sourceBuffer.Dispose(); } if (destinationBuffer != null) { destinationBuffer.Dispose(); } } }
static void Copy(string[] sourceFiles, string sourceFormat, string destinationFile, string destinationFormat) { if (sourceFiles == null || sourceFiles.Length == 0) { throw new ArgumentException(_("sourceFiles must not be empty.")); } var sourceBuffers = new List <IMessageBuffer>(); IMessageBuffer destinationBuffer = null; try { foreach (var sourceFile in sourceFiles) { var sourceBufferType = ParseMessageBufferType(sourceFile, sourceFormat); var sourceBuffer = CreateMessageBuffer(sourceFile, sourceBufferType); sourceBuffers.Add(sourceBuffer); } if (!String.IsNullOrEmpty(destinationFile)) { var destinationBufferType = ParseMessageBufferType(destinationFile, destinationFormat); destinationBuffer = CreateMessageBuffer(destinationFile, destinationBufferType); if (destinationBuffer.Count > 0) { throw new InvalidOperationException( String.Format( _("Destination database {0} must be empty!"), destinationFile ) ); } } // append all messages of all source buffers together in a lazy way IEnumerable <MessageModel> concatenatedMessages = new List <MessageModel>(0); sourceBuffers.ForEach(x => concatenatedMessages = concatenatedMessages.Concat(x) ); if (destinationBuffer == null) { // JSON pipe WriteMessagesToJson(concatenatedMessages, Console.Out); } else { foreach (var msg in concatenatedMessages) { destinationBuffer.Add(msg); } destinationBuffer.Flush(); } } finally { foreach (var sourceBuffer in sourceBuffers) { sourceBuffer.Dispose(); } if (destinationBuffer != null) { destinationBuffer.Dispose(); } } }