Example #1
0
        public async Task <bool> SyncResponse(SyncResponseMessage response)
        {
            await Task.Delay(100);

            var handle = GetUserHandle();
            await Clients.Group(response.SyncId).OnSyncResponse(new OnSyncResponseMessage
            {
                UserId   = handle,
                SyncId   = response.SyncId,
                Messages = response.Messages
            });

            return(true);
        }
Example #2
0
        public MessageManager()
        {
            sendev.Completed += SendCompleted;
            recvev.Completed += RecvCompleted;
            connectev.Completed += ConnectCompleted;

            rpchandler.Add((UInt16)MessageType.AUTHENTICATE_REQUEST, new Queue<Action<Dictionary<String, Object>>>());
            rpchandler.Add((UInt16)MessageType.AUTHENTICATE_RESPONSE, new Queue<Action<Dictionary<String, Object>>>());
            rpchandler.Add((UInt16)MessageType.REFRESH_INDEX_REQUEST, new Queue<Action<Dictionary<String, Object>>>());
            rpchandler.Add((UInt16)MessageType.REFRESH_INDEX_RESPONSE, new Queue<Action<Dictionary<String, Object>>>());
            rpchandler.Add((UInt16)MessageType.NOTIFY_AVAILABLE_REQUEST, new Queue<Action<Dictionary<String, Object>>>());
            rpchandler.Add((UInt16)MessageType.SYNC_REQUEST, new Queue<Action<Dictionary<String, Object>>>());
            rpchandler.Add((UInt16)MessageType.SYNC_RESPONSE, new Queue<Action<Dictionary<String, Object>>>());

            receiver.Add((UInt16)MessageType.AUTHENTICATE_REQUEST, new List<Action<Dictionary<String, Object>>>());
            receiver.Add((UInt16)MessageType.AUTHENTICATE_RESPONSE, new List<Action<Dictionary<String, Object>>>());
            receiver.Add((UInt16)MessageType.REFRESH_INDEX_REQUEST, new List<Action<Dictionary<String, Object>>>());
            receiver.Add((UInt16)MessageType.REFRESH_INDEX_RESPONSE, new List<Action<Dictionary<String, Object>>>());
            receiver.Add((UInt16)MessageType.NOTIFY_AVAILABLE_REQUEST, new List<Action<Dictionary<String, Object>>>());
            receiver.Add((UInt16)MessageType.SYNC_REQUEST, new List<Action<Dictionary<String, Object>>>());
            receiver.Add((UInt16)MessageType.SYNC_RESPONSE, new List<Action<Dictionary<String, Object>>>());
            receiver.Add((UInt16)MessageType.PING, new List<Action<Dictionary<String, Object>>>());

            receiver[(UInt16)MessageType.NOTIFY_AVAILABLE_REQUEST].Add(dictionary =>
                {
                    OnContentAvailable(new NotifyEventArgs()
                    {
                        ContentId = Convert.ToUInt64(dictionary["content_id"]),
                        ContentType = new List<String>(Array.ConvertAll((Object[])dictionary["content_type"], Convert.ToString)),
                        Description = (String)dictionary["description"]
                    });
                });

            receiver[(UInt16)MessageType.SYNC_REQUEST].Add(dictionary =>
            {
                var content_id = Convert.ToUInt64(dictionary["content_id"]);
                String content = null;
                UInt32 content_size = 0;
                UInt32 message = 0;

                lock (contentCache)
                {
                    if (contentCache.ContainsKey(content_id))
                    {
                        content = contentCache[content_id];
                        content_size = (UInt32)content.Length;
                        message = 1;
                    }
                }

                var msg = new SyncResponseMessage
                {
                    token = token,
                    content_id = content_id,
                    content_size = content_size,
                    dataset_id = (UInt16)DatasetType.CLIPBOARD,
                    message = message
                };

                Send(serializer.Serialize(msg)).ContinueWith(msgtask =>
                    {
                        if (msgtask.Result)
                            SendContent(content).ContinueWith(contask =>
                            {
                                if (contask.Result == false)
                                    Console.WriteLine("failed to send sync content.");
                            });
                        else Console.WriteLine("failed to send sync response.");
                    });
            });

            Disconnected += (sender, ev) =>
                {
                    try
                    {
                        socket.Disconnect(true);
                    }
                    catch (SocketException ex) {} // will throw if we're not connected, which is fine
                };
        }