private bool ReadMsg() { long maxSeqnAtStart = myMaxReceivedSeqn; myMsgLengthBuffer.Lo = myMsgLengthBuffer.Hi = 0; if (!myMsgLengthBuffer.Read(ref myPkgBuffer, ReceiveFromPkgBuffer)) { return(false); } Int32 len = UnsafeReader.ReadInt32FromBytes(myMsgLengthBuffer.Data); var msgBuffer = new BufferWindow(len); if (!msgBuffer.Read(ref myPkgBuffer, ReceiveFromPkgBuffer)) { Log.Warn("{0}: Can't read message with len={1} from the wire because connection was shut down", Id, len); return(false); } if (myMaxReceivedSeqn > maxSeqnAtStart) { myAcktor.SendAsync(myMaxReceivedSeqn); } Receive(msgBuffer.Data); ReadBytesCount += len + sizeof(Int32 /*len*/); return(true); }
public bool Read(ref BufferWindow helper, Receiver receiver, int size) { var hi = Hi + size; Assertion.Assert(hi <= Data.Length, "hi <= Data.Length"); while (Hi < hi) { Assertion.Assert(helper.Hi >= helper.Lo, "helper.Hi >= helper.Lo"); if (helper.Available > 0) { var copylen = Math.Min(hi - Hi, helper.Available); helper.MoveTo(Data, Hi, copylen); Hi += copylen; } else { if (helper.Hi == helper.Data.Length) { helper.Hi = helper.Lo = 0; } var receiverAvailable = receiver(helper.Data, helper.Hi, helper.Data.Length - helper.Hi); if (receiverAvailable == 0) { return(false); } helper.Hi += receiverAvailable; } } Assertion.Assert(Hi == hi, "lo == hi"); return(true); }
private void ReceiverProc(Socket socket) { myPkg = new BufferWindow(16384); myPkgBuffer = new BufferWindow(16384); mySocketBuffer = new BufferWindow(16384); myMsgLengthBuffer = new BufferWindow(4); myPkgHeaderBuffer = new BufferWindow(12); while (myLifetime.IsAlive) { if (!socket.Connected) { Log.Verbose("Stop receive messages because socket disconnected"); break; } try { if (!ReadMsg()) { Log.Verbose("{0} Connection was gracefully shutdown", Id); break; } } catch (Exception e) { if (e is SocketException socketEx) { var errcode = socketEx.SocketErrorCode; if (errcode == SocketError.TimedOut || errcode == SocketError.WouldBlock) { continue; //expected } } if (e is SocketException || e is ObjectDisposedException) { Log.Verbose("Exception in SocketWire.Receive: {0} {1} {2}", e.GetType().Name, Id, e.Message); //That's why we don't use Timeout any more. Exception happens only on windows but blocks socket completely. if (e.Message.ToLower().Contains("Overlapped I/O Operation is in progress".ToLower())) { Log.Error( "ERROR! Socket {0} {1} is in invalid state. Probably no more messages will be received. Exception message: '{2}'. " + "Sometimes it happens on Windows 8. Your os: {3}." + "In case this problem arises ask Ivan Paschenko.", e.GetType().Name, Id, e.Message, Environment.OSVersion.VersionString); } } else { Log.Error(e); } break; } } LogTraffic(); }
public bool Read(ref BufferWindow helper, Receiver receiver) => Read(ref helper, receiver, Data.Length - Hi);