Example #1
0
        public bool FlushNextMessage(TestYInstance sender, TestYInstance receiver)
        {
            Assert.AreNotEqual(sender, receiver);

            var messages = receiver._receiving[sender];

            if (messages.Count == 0)
            {
                receiver._receiving.Remove(sender);
                return(false);
            }

            var m = messages.Dequeue();

            // Debug.WriteLine($"MSG {sender.ClientId} -> {receiver.ClientId}, len {m.Length}:");
            // Debug.WriteLine(string.Join(",", m));

            using var writer = new MemoryStream();
            using (var reader = new MemoryStream(m))
            {
                SyncProtocol.ReadSyncMessage(reader, writer, receiver, receiver._tc);
            }

            if (writer.Length > 0)
            {
                // Send reply message.
                var replyMessage = writer.ToArray();
                // Debug.WriteLine($"REPLY {receiver.ClientId} -> {sender.ClientId}, len {replyMessage.Length}:");
                // Debug.WriteLine(string.Join(",", replyMessage));
                sender.Receive(replyMessage, receiver);
            }

            return(true);
        }
Example #2
0
        public TestYInstance(TestConnector connector, int clientId, YDocOptions options)
            : base(options)
        {
            ClientId = clientId;

            _tc = connector;
            _tc._allConns.Add(this);

            // Setup observe on local model.
            UpdateV2 += (s, e) =>
            {
                if (e.origin != _tc)
                {
                    using var stream = new MemoryStream();
                    SyncProtocol.WriteUpdate(stream, e.data);

                    BroadcastMessage(this, stream.ToArray());
                }
            };

            Connect();
        }
Example #3
0
        public void Connect()
        {
            if (!_tc._onlineConns.Contains(this))
            {
                _tc._onlineConns.Add(this);

                using var stream = new MemoryStream();
                SyncProtocol.WriteSyncStep1(stream, this);

                // Publish SyncStep1
                BroadcastMessage(this, stream.ToArray());

                foreach (var remoteYInstance in _tc._onlineConns)
                {
                    if (remoteYInstance != this)
                    {
                        stream.SetLength(0);
                        SyncProtocol.WriteSyncStep1(stream, remoteYInstance);

                        Receive(stream.ToArray(), remoteYInstance);
                    }
                }
            }
        }