Exemplo n.º 1
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;
                }
            };
        }
Exemplo n.º 2
0
 /// <summary>
 /// Tạo một thể hiện mới của lớp RawNotificationServerParam
 /// </summary>
 /// <param name="senderParam">Tham số dùng để khởi tạo cho việc gửi thông báo</param>
 /// <param name="clientCommunicatorParam">Tham số dùng để khởi tạo cho client Communicate Server</param>
 /// <param name="serverCommunicateListenPort">Tham số dùng để khởi tạo cho việc gửi thông báo</param>
 public RawNotificationServerParam(RawNotificationSenderParams senderParam,
                                   RawNotificationClientCommunicatorParams clientCommunicatorParam)
 {
     RawNotificationSenderParam   = senderParam;
     this.ClientCommunicatorParam = clientCommunicatorParam;
 }