Пример #1
0
 public ServerResult Register(RegisterPacketData data)
 {
     try
     {
         Device newdvc = new Device {
             DeviceID = data.DeviceID, OSID = (int)data.OSID, URI = data.URI
         };
         Notification_DBDataContext db = new Notification_DBDataContext();
         // tìm recceiver
         Receiver rcv = db.Receivers.Single(r => r.OldID == data.ReceiverOldID);
         // kiểm tra device đã tồn tại
         try
         {
             Device dvc = db.Devices.Single(dv => dv.DeviceID == data.DeviceID);
             //thiết bị đã tồn tại thì cập nhật URI cho thiết bị rồi xóa token key
             dvc.URI = data.URI;
         }
         catch (InvalidOperationException) // không tồn tại thiết bị thì add thiết bị mới vào
         {
             rcv.Devices.Add(newdvc);
         }
         db.SubmitChanges();
         return(ServerResult.Success);
     }
     catch (InvalidOperationException)
     {// không tìm thấy receiver
         return(ServerResult.NotFound);
     }
     catch (Exception ex)
     {
         OnUnhandledErrorOccurred(ex);
         return(ServerResult.Error);
     }
 }
Пример #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;
                }
            };
        }