Esempio n. 1
0
            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);
            }
Esempio n. 2
0
            private int ReceiveFromPkgBuffer(byte[] buffer, int offset, int size)
            {
                //size > 0

                if (myPkg.Available > 0)
                {
                    var sizeToCopy = Math.Min(size, myPkg.Available);
                    myPkg.MoveTo(buffer, offset, sizeToCopy);
                    return(sizeToCopy);
                }
                else
                {
                    while (true)
                    {
                        myPkgHeaderBuffer.Clear();
                        if (!myPkgHeaderBuffer.Read(ref mySocketBuffer, ReceiveFromSocket))
                        {
                            return(0);
                        }

                        Int32 len  = UnsafeReader.ReadInt32FromBytes(myPkgHeaderBuffer.Data);
                        Int64 seqN = UnsafeReader.ReadInt64FromBytes(myPkgHeaderBuffer.Data, sizeof(Int32));

                        if (len == ACK_MSG_LEN)
                        {
                            SendBuffer.Acknowledge(seqN);
                        }
                        else
                        {
                            myPkg.Clear();
                            if (!myPkg.Read(ref mySocketBuffer, ReceiveFromSocket, len))
                            {
                                return(0);
                            }

                            if (seqN > myMaxReceivedSeqn || seqN == 1 /*TODO new client, possible duplicate problem if ack for seqN=1 from previous client's connection hasn't passed*/)
                            {
                                myMaxReceivedSeqn = seqN; //will be acknowledged when we read whole message
                                Assertion.Assert(myPkg.Available > 0, "myPkgBuffer.Available > 0");

                                var sizeToCopy = Math.Min(size, myPkg.Available);
                                myPkg.MoveTo(buffer, offset, sizeToCopy);
                                return(sizeToCopy);
                            }
                            else
                            {
                                myAcktor.SendAsync(seqN);
                            }
                        }
                    }
                }
            }
Esempio n. 3
0
            private int ReceiveFromPkgBuffer(byte[] buffer, int offset, int size)
            {
                //size > 0

                if (myPkg.Available > 0)
                {
                    var sizeToCopy = Math.Min(size, myPkg.Available);
                    myPkg.MoveTo(buffer, offset, sizeToCopy);
                    return(sizeToCopy);
                }
                else
                {
                    while (true)
                    {
                        myPkgHeaderBuffer.Clear();
                        if (!myPkgHeaderBuffer.Read(ref mySocketBuffer, ReceiveFromSocket))
                        {
                            return(0);
                        }

                        Int32 len = UnsafeReader.ReadInt32FromBytes(myPkgHeaderBuffer.Data);

                        if (len == PING_LEN)
                        {
                            Int32 receivedTimestamp            = UnsafeReader.ReadInt32FromBytes(myPkgHeaderBuffer.Data, sizeof(Int32));
                            Int32 receivedCounterpartTimestamp = UnsafeReader.ReadInt32FromBytes(myPkgHeaderBuffer.Data, sizeof(Int32) + sizeof(Int32));
                            Log.Trace()?.Log($"{Id}: Received PING package " +
                                             $"receivedTimestamp={receivedTimestamp}, " +
                                             $"receivedCounterpartTimestamp={receivedCounterpartTimestamp}, " +
                                             $"currentTimeStamp={myCurrentTimeStamp}, " +
                                             $"counterpartTimestamp={myCounterpartTimestamp}, " +
                                             $"counterpartNotionTimestamp={myCounterpartNotionTimestamp}");
                            myCounterpartTimestamp       = receivedTimestamp;
                            myCounterpartNotionTimestamp = receivedCounterpartTimestamp;

                            if (ConnectionEstablished(myCurrentTimeStamp, myCounterpartNotionTimestamp))
                            {
                                HeartbeatAlive.Value = true;
                            }

                            continue;
                        }

                        Int64 seqN = UnsafeReader.ReadInt64FromBytes(myPkgHeaderBuffer.Data, sizeof(Int32));
                        if (len == ACK_MSG_LEN)
                        {
                            SendBuffer.Acknowledge(seqN);
                        }
                        else
                        {
                            myPkg.Clear();
                            if (!myPkg.Read(ref mySocketBuffer, ReceiveFromSocket, len))
                            {
                                return(0);
                            }

                            if (seqN > myMaxReceivedSeqn || seqN == 1 /*TODO new client, possible duplicate problem if ack for seqN=1 from previous client's connection hasn't passed*/)
                            {
                                myMaxReceivedSeqn = seqN; //will be acknowledged when we read whole message
                                Assertion.Assert(myPkg.Available > 0, "myPkgBuffer.Available > 0");

                                var sizeToCopy = Math.Min(size, myPkg.Available);
                                myPkg.MoveTo(buffer, offset, sizeToCopy);
                                return(sizeToCopy);
                            }
                            else
                            {
                                myAcktor.SendAsync(seqN);
                            }
                        }
                    }
                }
            }