public void TestPause() { int x = 0; var buffer = new ByteBufferAsyncProcessor("TestAsyncProcessor", 1, delegate(byte[] data, int offset, int len, ref long seqN) { x += data[offset]; }); var reason1 = "reason1"; var reason2 = "reason2"; Assert.True(buffer.Pause(reason1)); Assert.False(buffer.Pause(reason1)); Assert.True(buffer.Pause(reason2)); Assert.False(buffer.Pause(reason2)); buffer.Start(); buffer.Put(new byte[] { 1, 2, 3 }); Thread.Sleep(50); Assert.AreEqual(0, x); Assert.True(buffer.Resume(reason1)); Assert.False(buffer.Resume(reason1)); Thread.Sleep(50); Assert.AreEqual(0, x); Assert.True(buffer.Resume(reason2)); Assert.False(buffer.Resume(reason2)); Assert.False(buffer.Resume(reason2)); SpinWait.SpinUntil(() => buffer.AllDataProcessed); Assert.AreEqual(6, x); }
protected Base(string id, Lifetime lifetime, [NotNull] IScheduler scheduler) : base(scheduler) { Id = id; Log = Diagnostics.Log.GetLog(GetType()); myLifetime = lifetime; myAcktor = new Actor <long>(id + "-ACK", lifetime, SendAck); SendBuffer = new ByteBufferAsyncProcessor(id + "-Sender", Send0); SendBuffer.Pause(DisconnectedPauseReason); SendBuffer.Start(); Connected.Advise(lifetime, value => HeartbeatAlive.Value = value); //when connected SocketProvider.Advise(lifetime, socket => { // //todo hack for multiconnection, bring it to API // if (SupportsReconnect) SendBuffer.Clear(); var timer = StartHeartbeat(); SendBuffer.ReprocessUnacknowledged(); SendBuffer.Resume(DisconnectedPauseReason); scheduler.Queue(() => { Connected.Value = true; }); try { //use current thread for receiving procedure ReceiverProc(socket); } finally { scheduler.Queue(() => { Connected.Value = false; }); SendBuffer.Pause(DisconnectedPauseReason); timer.Dispose(); CloseSocket(socket); } }); }
private readonly byte[] myAckPkgHeader = new byte[PkgHeaderLen]; //different threads private void Send0(byte[] data, int offset, int len, ref long seqN) { try { if (seqN == 0) { seqN = ++mySentSeqn; } using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(len); cookie.Writer.Write(seqN); cookie.CopyTo(mySendPkgHeader); } lock (mySocketSendLock) { Socket.Send(mySendPkgHeader, 0, PkgHeaderLen, SocketFlags.None); Socket.Send(data, offset, len, SocketFlags.None); } WrittenBytesCount += len; } catch (Exception e) { if (e is SocketException || e is ObjectDisposedException) { SendBuffer.Pause(DisconnectedPauseReason); LogTraffic(); } else { Log.Error(e); } } }