public void PushMessages(IEnumerable <CommandMessage> messages) { if (_disposed) { throw new ObjectDisposedException(this.GetType().FullName); } if (_protocolVersion == ProtocolVersion.Version1) { Stream stream = new BufferStream(_bufferManager); try { var message = new CommandsMessage(); message.CommandMessages.AddRange(messages); stream = new JoinStream(stream, message.Export(_bufferManager)); _connection.Send(stream, _sendTimeSpan); _lastSendTime = DateTime.UtcNow; } catch (ConnectionException) { this.OnClose(new EventArgs()); } finally { stream.Close(); } } else { throw new ConnectionManagerException(); } }
private void Pull(object state) { Thread.CurrentThread.Name = "Pull"; try { Stopwatch sw = new Stopwatch(); for (; ;) { if (_disposed) { throw new ObjectDisposedException(this.GetType().FullName); } sw.Restart(); if (_protocolVersion == ProtocolVersion.Version1) { using (Stream stream = _connection.Receive(_receiveTimeSpan)) { if (stream.Length == 0) { continue; } byte type = (byte)stream.ReadByte(); using (Stream stream2 = new RangeStream(stream, 1, stream.Length - 1, true)) { var message = CommandsMessage.Import(stream2, _bufferManager); this.OnPullMessagesEvent(new MessagesEventArgs() { Messages = message.CommandMessages }); } } } else { throw new ConnectionManagerException(); } sw.Stop(); if (sw.ElapsedMilliseconds < 1000) { Thread.Sleep(1000 - (int)sw.ElapsedMilliseconds); } } } catch (Exception) { if (!_disposed) { this.OnClose(new EventArgs()); } } }