Ejemplo n.º 1
0
        /// <summary>
        /// Phương thức này sẽ chạy khi Trigger được kích hoạt, không cần gọi tới nó
        /// </summary>
        /// <param name="taskInstance">hệ thống sẽ tự động tạo instance này khi chạy</param>
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //nếu sử dụng một phương thức async nào trong hàm này, hàm run sẽ kết thúc trước khi phương thức đó thực hiện xong.
            // sử dụng defferal để thông báo với hệ thống rằng chưa được phép kết thúc phương thức run
            // nếu bạn không sử dụng phương thức async nào, bạn có thể bỏ defferal đi.
            BackgroundTaskDeferral defferal = taskInstance.GetDeferral();

            System.Diagnostics.Debug.WriteLine("Đã nhận được toast");

            // TODO: làm những thứ bạn muốn trong background task ở đây
            // Lưu ý: tất cả các phương thức async nào nằm ngoài khu này đều sẽ không thực hiện được
            string content = (taskInstance.TriggerDetails as RawNotification).Content;

            try
            {
                // lấy id
                long notificationid = long.Parse(content);
                System.Diagnostics.Debug.WriteLine("Notification ID : " + notificationid.ToString());

                #region Bắt đầu liên lạc trực tiếp với server để lấy nội dung thông báo

                CommunicatePacket packet = new CommunicatePacket(CommunicateType.GetNotificationContent, new GetNotificationContentCommunicateData(notificationid));
                try
                {
                    UWPTCPClient.UWPTCPClient client = new UWPTCPClient.UWPTCPClient("115.74.126.7", "22112", ((data) =>
                    {
                        MyBitConverter <SendObject> contentconverter = new MyBitConverter <SendObject>();
                        SendObject notifyContent = contentconverter.BytesToObject(data);
                        UserCode.Run(notifyContent);

                        // sau khi xử lí xong, ta thông báo với hệ thống là hàm run đã thực hiện xong và hệ thống có thể đóng hàm run lại
                        //việc này đồng nghĩa với background task sẽ kết thúc
                        defferal.Complete();
                    }));
                    await client.ConnectAsync();

                    await client.SendAsync(new Models.MyBitConverter <CommunicatePacket>().ObjectToBytes(new CommunicatePacket(CommunicateType.GetNotificationContent, new GetNotificationContentCommunicateData(notificationid))));
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
                #endregion
            }
            catch (Exception ex)
            {
                // đường truyền lỗi nên không nhận được id
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
Ejemplo n.º 2
0
        MyBitConverter <long> _NotifyIDConverter = new MyBitConverter <long>(); // converter dùng để convert byte thành long, số long nhận được chính là notificationID được yêu cầu bởi client
        #endregion

        public ClientCommunicator(Params.RawNotificationClientCommunicatorParams param)
        {
            _Param = param;
            server = new TCPServer.TCPServer(_Param.ListenPort);
            // đăng ký sự kiện nhận được một gói tin từ client
            server.OnPacketReceived += (s, e) =>
            {
                FromClientPacket packet = null;
                try
                {
                    packet = PacketConverter.BytesToObject(e.Data);
                }
                catch
                {
                    // Bên ngoài gửi gói tin không đúng định dạng, có thể do lỗi đường truyền cũng có thể do ý đồ phá hoại
                    e.RespondToClient(ResultConverter.ObjectToBytes(new FromServerPacket(ServerResult.DataCorrupt, null)));
                    e.CloseConnection();
                    return;
                }
                switch (packet.Type)
                {
                case FromClientPacketType.Register:
                {
                    RegisterPacketData data = packet.Data as RegisterPacketData;
                    e.RespondToClient(ResultConverter.ObjectToBytes(new FromServerPacket(Register(data), null)));
                    e.CloseConnection();
                }
                break;

                case FromClientPacketType.GetNotificationContent:
                {
                    GetNotificationContentPacketData data = packet.Data as GetNotificationContentPacketData;
                    ServerResult result;
                    // lấy ra content
                    byte[] content = NotificationContent(data.NotificationID, out result);
                    e.RespondToClient(ResultConverter.ObjectToBytes(new FromServerPacket(result, content)));
                    e.CloseConnection();
                }
                break;
                }
            };
        }
Ejemplo n.º 3
0
        internal ServerCommunicator(int listenPort, int notifyPort, Func <ServerInfo> OnServerInfoRequestReceived, Action OnSendAllNotificationRequestReceived)
        {
            _ListenPort = listenPort;
            _NotifyPort = notifyPort;

            server                   = new TCPServer.TCPServer(_ListenPort);
            _NotifyServer            = new TCPServer.TCPServer(_NotifyPort, true);
            server.OnPacketReceived += (s, e) =>
            {
                System.Diagnostics.Debug.WriteLine("Đã nhận được yêu cầu");
                FromClientPacket ReceivedPacket = null;
                try
                {
                    ReceivedPacket = _FromClientConverter.BytesToObject(e.Data);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Gói tin bị lỗi khi vận chuyển");
                    e.RespondToClient(_FromServerConverter.ObjectToBytes(new FromServerPacket(FromServerPacketType.DataCorrupted, ex)));
                    return;
                }
                // Giữa 2 server với nhau phải luôn được kết nối, vì vậy khi giao tiếp xong thì vẫn để kết nối đó cho lần tiếp theo
                switch (ReceivedPacket.PacketType)
                {
                case FromClientPacketType.AddNotification:
                {
                    e.RespondToClient(_FromServerConverter.ObjectToBytes(new FromServerPacket(FromServerPacketType.AddNotificationResponse, AddNotification(ReceivedPacket.Data as AddNotificationPacketData)))); break;
                }

                case FromClientPacketType.RequestServerInfo:
                {
                    e.RespondToClient(_FromServerConverter.ObjectToBytes(new FromServerPacket(FromServerPacketType.ServerInfo, OnServerInfoRequestReceived()))); break;
                }

                case FromClientPacketType.SendAllNotification:
                {         // nhận được yêu cầu gửi thì khỏi cần hồi âm
                    OnSendAllNotificationRequestReceived(); break;
                }
                }
            };
        }