Ejemplo n.º 1
0
        public void EncodeDecode_Dictionary_Concurrent()
        {
            List <Dictionary <string, object> > dictionaries = new List <Dictionary <string, object> >();

            for (int i = 0; i < 1000; i++)
            {
                dictionaries.Add(new Dictionary <string, object>
                {
                    { "key", i.ToString() },
                    { "data", new byte[80000] }
                });
            }

            var result = Parallel.ForEach(dictionaries, new ParallelOptions {
                MaxDegreeOfParallelism = 10
            }, (s) =>
            {
                var encoded = BsonCodec.Encode(s);
                Assert.NotNull(encoded);
                var decoded = BsonCodec.Decode <Dictionary <string, object> >(encoded);
                Assert.NotNull(decoded);
                Assert.IsTrue(decoded.ContainsKey("key"));
                Assert.IsTrue(decoded.ContainsKey("data"));
                Assert.AreEqual(80000, (decoded["data"] as byte[]).Length);
            });

            Assert.IsTrue(result.IsCompleted);
        }
Ejemplo n.º 2
0
        public void EncodeDecode_SessionStart_Single_WithDictionaryData()
        {
            Dictionary <string, object> data = new Dictionary <string, object>
            {
                { "keyString", "string" },
                { "keyInteger", 36 },
                { "keyArray", new byte[10] },
            };

            var encoded = Frame.CreateSessionStartFrame(0, BsonCodec.Encode(data));
            var decoded = Frame.DecodeFrameBuffer(encoded, 0, encoded.Length, out var newOffset, out var newAvailable);

            Assert.AreEqual(1, decoded.Count);
            Assert.AreEqual(0, newOffset);
            Assert.AreEqual(0, newAvailable);
            Assert.AreEqual(FrameType.SessionStart, decoded[0].Type);
            Assert.AreEqual(0, decoded[0].SessionID);

            // validate data
            Assert.NotNull(decoded[0].Payload);
            var dataDecoded = BsonCodec.Decode <Dictionary <string, object> >(decoded[0].Payload);

            Assert.NotNull(dataDecoded);

            // keys and values present
            Assert.IsTrue(dataDecoded.ContainsKey("keyString"));
            Assert.AreEqual("string", (string)dataDecoded["keyString"]);

            Assert.IsTrue(dataDecoded.ContainsKey("keyInteger"));
            Assert.AreEqual(36, (int)(long)dataDecoded["keyInteger"]);      // need to first unbox

            Assert.IsTrue(dataDecoded.ContainsKey("keyArray"));
            Assert.AreEqual(10, ((byte[])dataDecoded["keyArray"]).Length);
        }
Ejemplo n.º 3
0
        public static byte[] CreateConnectionAcceptFrame()
        {
            var descriptor = new ConnectionDescriptor
            {
                ProtocolVersion = ProtocolVersion,
                MaxFrameSize    = MaxFrameSize
            };

            return(_createFrame(FrameType.ConnectionAccept, BsonCodec.Encode(descriptor)));
        }
Ejemplo n.º 4
0
        public static byte[] CreateConnectionStartFrame(Dictionary <string, string> data = null)
        {
            var descriptor = new ConnectionDescriptor
            {
                ProtocolVersion = ProtocolVersion,
                MaxFrameSize    = MaxFrameSize,
                Data            = data
            };

            return(_createFrame(FrameType.ConnectionStart, BsonCodec.Encode(descriptor)));
        }
Ejemplo n.º 5
0
        public static byte[] CreateConnectionRejectFrame(string error = null)
        {
            var descriptor = new ConnectionDescriptor
            {
                ProtocolVersion = ProtocolVersion,
                MaxFrameSize    = MaxFrameSize,
                Error           = error
            };

            return(_createFrame(FrameType.ConnectionReject, BsonCodec.Encode(descriptor)));
        }
Ejemplo n.º 6
0
        public void EncodeDecode_SessionDescriptor()
        {
            SessionDescriptor descriptor = new SessionDescriptor {
                Name = "testName"
            };
            var descEncoded = BsonCodec.Encode(descriptor);

            Assert.NotNull(descEncoded);
            var decoded = BsonCodec.Decode <SessionDescriptor>(descEncoded);

            Assert.NotNull(decoded);
            Assert.AreEqual(descriptor.Name, decoded.Name);
        }
Ejemplo n.º 7
0
        public void SessionOpen()
        {
            var data = new Dictionary <string, object>
            {
                { "data", "Frame.CreateSessionOpenFrame" }
            };

            var encoded = Frame.CreateSessionOpenFrame(99, BsonCodec.Encode(data));
            var decoded = Frame.DecodeFrameBuffer(encoded, 0, encoded.Length, out var newOffset, out var newAvailable);

            Assert.AreEqual(1, decoded.Count);
            Assert.AreEqual(0, newOffset);
            Assert.AreEqual(0, newAvailable);
            Assert.AreEqual(FrameType.SessionOpen, decoded[0].Type);
            Assert.AreEqual(99, decoded[0].SessionID);
            Assert.NotNull(decoded[0].Payload);
            var decodedData = BsonCodec.Decode <Dictionary <string, object> >(decoded[0].Payload);

            Assert.NotNull(decodedData);
            Assert.IsTrue(decodedData.ContainsKey("data"));
            Assert.AreEqual("Frame.CreateSessionOpenFrame", (string)decodedData["data"]);
        }
Ejemplo n.º 8
0
        public void EncodeDecode_SessionDescriptor_Concurrent()
        {
            List <SessionDescriptor> descriptors = new List <SessionDescriptor>();

            for (int i = 0; i < 1000; i++)
            {
                descriptors.Add(new SessionDescriptor {
                    Name = i.ToString()
                });
            }

            var result = Parallel.ForEach(descriptors, new ParallelOptions {
                MaxDegreeOfParallelism = 10
            }, (s) =>
            {
                var encoded = BsonCodec.Encode(s);
                Assert.NotNull(encoded);
                var decoded = BsonCodec.Decode <SessionDescriptor>(encoded);
                Assert.NotNull(decoded);
                Assert.AreEqual(s.Name, decoded.Name);
            });

            Assert.IsTrue(result.IsCompleted);
        }
Ejemplo n.º 9
0
        public void EncodeDecode_SessionStart_Multiple_WithData()
        {
            var descriptor0 = new SessionDescriptor {
                Name = "0"
            };
            var encoded0 = Frame.CreateSessionStartFrame(10, BsonCodec.Encode(descriptor0));

            var descriptor1 = new SessionDescriptor {
                Name = "1"
            };
            var encoded1 = Frame.CreateSessionStartFrame(11, BsonCodec.Encode(descriptor1));

            var descriptor2 = new SessionDescriptor {
                Name = "2"
            };
            var encoded2 = Frame.CreateSessionStartFrame(12, BsonCodec.Encode(descriptor2));

            var descriptor3 = new SessionDescriptor {
                Name = "3"
            };
            var encoded3 = Frame.CreateSessionStartFrame(13, BsonCodec.Encode(descriptor3));

            var descriptor4 = new SessionDescriptor {
                Name = "4"
            };
            var encoded4 = Frame.CreateSessionStartFrame(14, BsonCodec.Encode(descriptor4));

            var combined = new byte[encoded0.Length + encoded1.Length + encoded2.Length + encoded3.Length + encoded4.Length];

            Array.Copy(encoded0, 0, combined, 0, encoded0.Length);
            Array.Copy(encoded1, 0, combined, encoded0.Length, encoded1.Length);
            Array.Copy(encoded2, 0, combined, encoded0.Length + encoded1.Length, encoded2.Length);
            Array.Copy(encoded3, 0, combined, encoded0.Length + encoded1.Length + encoded2.Length, encoded3.Length);
            Array.Copy(encoded4, 0, combined, encoded0.Length + encoded1.Length + encoded2.Length + encoded3.Length, encoded4.Length);

            var decoded = Frame.DecodeFrameBuffer(combined, 0, combined.Length, out var newOffset, out var newAvailable);

            Assert.AreEqual(5, decoded.Count);
            Assert.AreEqual(0, newOffset);
            Assert.AreEqual(0, newAvailable);

            Assert.AreEqual(FrameType.SessionStart, decoded[0].Type);
            Assert.AreEqual(10, decoded[0].SessionID);
            Assert.NotNull(decoded[0].Payload);
            var decodedDescriptor0 = BsonCodec.Decode <SessionDescriptor>(decoded[0].Payload);

            Assert.AreEqual("0", decodedDescriptor0.Name);

            Assert.AreEqual(FrameType.SessionStart, decoded[1].Type);
            Assert.AreEqual(11, decoded[1].SessionID);
            Assert.NotNull(decoded[1].Payload);
            var decodedDescriptor1 = BsonCodec.Decode <SessionDescriptor>(decoded[1].Payload);

            Assert.AreEqual("1", decodedDescriptor1.Name);

            Assert.AreEqual(FrameType.SessionStart, decoded[2].Type);
            Assert.AreEqual(12, decoded[2].SessionID);
            Assert.NotNull(decoded[2].Payload);
            var decodedDescriptor2 = BsonCodec.Decode <SessionDescriptor>(decoded[2].Payload);

            Assert.AreEqual("2", decodedDescriptor2.Name);

            Assert.AreEqual(FrameType.SessionStart, decoded[3].Type);
            Assert.AreEqual(13, decoded[3].SessionID);
            Assert.NotNull(decoded[3].Payload);
            var decodedDescriptor3 = BsonCodec.Decode <SessionDescriptor>(decoded[3].Payload);

            Assert.AreEqual("3", decodedDescriptor3.Name);

            Assert.AreEqual(FrameType.SessionStart, decoded[4].Type);
            Assert.AreEqual(14, decoded[4].SessionID);
            Assert.NotNull(decoded[4].Payload);
            var decodedDescriptor4 = BsonCodec.Decode <SessionDescriptor>(decoded[4].Payload);

            Assert.AreEqual("4", decodedDescriptor4.Name);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Creates a new session between both endpoints
        /// </summary>
        /// <param name="name">The application-defined name for the session</param>
        /// <param name="data">Optional payload data to be sent to the remote endpoint</param>
        /// <returns>The created session</returns>
        public Session CreateSession(string name, byte[] data = null)
        {
            if (State != ConnectionState.Open)
            {
                throw new ConnectionException();
            }

            uint id = uint.MaxValue;

            try
            {
                // create the session descriptor which is sent to the remote endpoint
                var descriptor = new SessionDescriptor
                {
                    Name = name,
                    Data = data
                };

                // construct new session frame
                Session session;
                lock (_pendingSessions)
                {
                    id      = _sessionIdProvider.Next();
                    session = new Session(this, descriptor, id);
                    _pendingSessions.Add(session.ID, session);
                }

                // send the session start frame
                _sendFrame(FrameType.SessionStart, Frame.CreateSessionStartFrame(session.ID, BsonCodec.Encode(descriptor)));

                // block until the connection is settled or a timeout occurs
                var index = WaitHandle.WaitAny(new[] { session.Settled, session.SessionError }, 5000);

                switch (index)
                {
                // session was settled (application needs to check if it was accepted/rejected
                case 0: return(session);

                // the session was rejected by the remote endpoint
                case 1: throw new SessionRejectedException($"The session was rejected by the remote endpoint");

                // the session creation timedout
                case WaitHandle.WaitTimeout: throw new SessionTimeoutException($"Session settlement timed out after {5000} ms");

                // should never hit here, but have to return something
                default: throw new Exception();
                }
            }
            catch (Exception)
            {
                lock (_pendingSessions)
                {
                    if (_pendingSessions.ContainsKey(id))
                    {
                        _pendingSessions.Remove(id);
                    }

                    _sessionIdProvider.Remove(id);
                }

                throw;
            }
        }