Esempio n. 1
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. 2
0
        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);
        }
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);
                            }
                        }
                    }
                }
            }