Ejemplo n.º 1
0
        private byte[] Receive()
        {
            bool ok;
            bool timedOut;

            byte[] remote;
            byte[] buffer;
            scheduler.ReceivePacket(1000, out ok, out timedOut, out remote, out buffer);
            return(buffer);
        }
Ejemplo n.º 2
0
 public void Receive(int timeLimit, out bool ok, out bool timedOut, out Dafny.ISequence <byte> remote, out byte[] buffer)
 {
     byte[] remoteBytes;
     scheduler.ReceivePacket(timeLimit, out ok, out timedOut, out remoteBytes, out buffer);
     if (ok && !timedOut && remoteBytes != null && remoteBytes.Length > MaxPublicKeySize)
     {
         timedOut = true;
     }
     if (ok && !timedOut)
     {
         remote = Dafny.Sequence <byte> .FromArray(remoteBytes);
     }
     else
     {
         remote = Dafny.Sequence <byte> .Empty;
     }
 }
Ejemplo n.º 3
0
 private void ReceiverThread()
 {
     while (true)
     {
         bool   ok;
         bool   timedOut;
         byte[] remote;
         byte[] messageBytes;
         scheduler.ReceivePacket(0, out ok, out timedOut, out remote, out messageBytes);
         if (!ok)
         {
             Console.WriteLine("Not OK, so terminating receiver thread");
             return;
         }
         if (timedOut)
         {
             Thread.Sleep(100);
             continue;
         }
         string message = Encoding.UTF8.GetString(messageBytes);
         Console.WriteLine("Received message {0} from {1}", message, IoScheduler.PublicKeyToString(remote));
     }
 }
Ejemplo n.º 4
0
        public byte[] SubmitRequest(byte[] request, bool verbose = false, int timeBeforeServerSwitchMs = 1000)
        {
            UInt64 seqNum = nextSeqNum++;

            byte[] requestMessage;
            using (var memStream = new MemoryStream())
            {
                IoEncoder.WriteUInt64(memStream, 0);                                 // 0 means "this is a CMessage_Request"
                IoEncoder.WriteUInt64(memStream, seqNum);                            // sequence number
                IoEncoder.WriteUInt64(memStream, (UInt64)request.Length);            // size of CAppRequest
                IoEncoder.WriteBytes(memStream, request, 0, (UInt64)request.Length); // CAppRequest
                requestMessage = memStream.ToArray();
            }

            scheduler.SendPacket(serverPublicKeys[primaryServerIndex], requestMessage);
            if (verbose)
            {
                Console.WriteLine("Sending a request with sequence number {0} to {1}",
                                  seqNum, serviceIdentity.Servers[primaryServerIndex]);
            }

            while (true)
            {
                bool   ok, timedOut;
                byte[] remote;
                byte[] replyBytes;
                scheduler.ReceivePacket(timeBeforeServerSwitchMs, out ok, out timedOut, out remote, out replyBytes);

                if (!ok)
                {
                    throw new Exception("Unrecoverable networking failure");
                }

                if (timedOut)
                {
                    primaryServerIndex = (primaryServerIndex + 1) % serviceIdentity.Servers.Count();
                    if (verbose)
                    {
                        Console.WriteLine("#timeout; rotating to server {0}", primaryServerIndex);
                    }
                    scheduler.SendPacket(serverPublicKeys[primaryServerIndex], requestMessage);
                    continue;
                }

                if (replyBytes.Length < 24)
                {
                    throw new Exception(String.Format("Got RSL reply with invalid length {0}", replyBytes.Length));
                }

                UInt64 messageType = IoEncoder.ExtractUInt64(replyBytes, 0);
                if (messageType != 6)
                {
                    throw new Exception("Got RSL message that wasn't a reply");
                }

                UInt64 replySeqNum = IoEncoder.ExtractUInt64(replyBytes, 8);
                if (replySeqNum != seqNum)
                {
                    // This is a retransmission of a reply for an old sequence
                    // number.  Ignore it.
                    continue;
                }

                UInt64 replyLength = IoEncoder.ExtractUInt64(replyBytes, 16);
                if (replyLength + 24 != (UInt64)replyBytes.Length)
                {
                    throw new Exception(String.Format("Got RSL reply with invalid encoded length ({0} instead of {1})",
                                                      replyLength, replyBytes.Length - 24));
                }

                return(replyBytes.Skip(24).ToArray());
            }
        }