Beispiel #1
0
        public void AddOrUpdateServerMessageAsync(ServerMessageData entity, Action <ResponseBase, Exception> callback)
        {
            DataRequest <ServerMessageData> request = new DataRequest <ServerMessageData>()
            {
                Data = entity
            };

            RpcRoot.SignPostAsync(RpcRoot.OfficialServerHost, RpcRoot.OfficialServerPort, _controllerName, nameof(IServerMessageController.AddOrUpdateServerMessage), data: request, callback);
        }
Beispiel #2
0
            public void AddOrUpdateServerMessageAsync(ServerMessageData entity, Action <ResponseBase, Exception> callback)
            {
                DataRequest <ServerMessageData> request = new DataRequest <ServerMessageData>()
                {
                    Data = entity
                };

                PostAsync(SControllerName, nameof(IServerMessageController.AddOrUpdateServerMessage), request.ToQuery(SingleUser.LoginName, SingleUser.PasswordSha1), request, callback);
            }
Beispiel #3
0
        public void AddOrUpdateServerMessageAsync(ServerMessageData entity, Action <ResponseBase, Exception> callback)
        {
            DataRequest <ServerMessageData> request = new DataRequest <ServerMessageData>()
            {
                Data = entity
            };

            RpcRoot.PostAsync(_host, _port, _controllerName, nameof(IServerMessageController.AddOrUpdateServerMessage), request, request, callback);
        }
Beispiel #4
0
        protected override void OnMessage(MessageEventArgs e)
        {
            // try to parse and execute the command sent
            try
            {
                UnityEngine.Debug.Log(e.Data);
                var proxyData = JsonConvert.DeserializeObject <ProxyMessageData>(e.Data);
                ServerMessageData messageData = proxyData.ToMessageData(currentTarget, senderIdentity, this);

                ReferenceManager.Instance.ExecuteForReference(messageData);
            }
            catch (CoflnetException ex)
            {
                var data = JsonConvert.SerializeObject(new CoflnetExceptionTransmit(ex));
                Send(data);
                Track.instance.Error(ex.Message, e.Data, ex.StackTrace);
            }
        }
Beispiel #5
0
 public ServerMessageSet(string dbFileFullName, bool isServer)
 {
     if (!string.IsNullOrEmpty(dbFileFullName))
     {
         _connectionString = $"filename={dbFileFullName};journal=false";
     }
     _isServer = isServer;
     if (!_isServer)
     {
         VirtualRoot.BuildCmdPath <LoadNewServerMessageCommand>(action: message => {
             DateTime localTimestamp = VirtualRoot.LocalServerMessageSetTimestamp;
             // 如果已知服务器端最新消息的时间戳不比本地已加载的最新消息新就不用加载了
             if (message.KnowServerMessageTimestamp <= Timestamp.GetTimestamp(localTimestamp))
             {
                 return;
             }
             OfficialServer.ServerMessageService.GetServerMessagesAsync(localTimestamp, (response, e) => {
                 if (response.IsSuccess() && response.Data.Count > 0)
                 {
                     LinkedList <ServerMessageData> data = new LinkedList <ServerMessageData>();
                     lock (_locker) {
                         DateTime maxTime = localTimestamp;
                         foreach (var item in response.Data.OrderBy(a => a.Timestamp))
                         {
                             if (item.Timestamp > maxTime)
                             {
                                 maxTime = item.Timestamp;
                             }
                             data.AddLast(item);
                             _linkedList.AddFirst(item);
                         }
                         if (maxTime != localTimestamp)
                         {
                             VirtualRoot.LocalServerMessageSetTimestamp = maxTime;
                         }
                     }
                     using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                         var col = db.GetCollection <ServerMessageData>();
                         foreach (var item in data)
                         {
                             col.Insert(item);
                         }
                     }
                     VirtualRoot.RaiseEvent(new NewServerMessageLoadedEvent(data));
                 }
             });
         });
     }
     VirtualRoot.BuildCmdPath <AddOrUpdateServerMessageCommand>(action: message => {
         if (string.IsNullOrEmpty(_connectionString))
         {
             return;
         }
         InitOnece();
         if (_isServer)
         {
             #region Server
             ServerMessageData exist;
             List <ServerMessageData> toRemoves = new List <ServerMessageData>();
             ServerMessageData data             = null;
             lock (_locker) {
                 exist = _linkedList.FirstOrDefault(a => a.Id == message.Input.Id);
                 if (exist != null)
                 {
                     DateTime timestamp = exist.Timestamp;
                     exist.Update(message.Input);
                     // 如果更新前后时间戳没有变化则自动变更时间戳
                     if (timestamp == exist.Timestamp)
                     {
                         exist.Timestamp = DateTime.Now;
                     }
                 }
                 else
                 {
                     data = new ServerMessageData(message.Input);
                     _linkedList.AddFirst(data);
                     while (_linkedList.Count > NTKeyword.ServerMessageSetCapacity)
                     {
                         toRemoves.Add(_linkedList.Last.Value);
                         _linkedList.RemoveLast();
                     }
                 }
             }
             if (exist != null)
             {
                 using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                     var col = db.GetCollection <ServerMessageData>();
                     col.Update(exist);
                 }
             }
             else
             {
                 using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                     var col = db.GetCollection <ServerMessageData>();
                     if (toRemoves.Count != 0)
                     {
                         foreach (var item in toRemoves)
                         {
                             col.Delete(item.Id);
                         }
                     }
                     col.Insert(data);
                 }
             }
             #endregion
         }
         else
         {
             OfficialServer.ServerMessageService.AddOrUpdateServerMessageAsync(new ServerMessageData(message.Input), (response, ex) => {
                 if (response.IsSuccess())
                 {
                     VirtualRoot.Execute(new LoadNewServerMessageCommand());
                 }
             });
         }
     });
     VirtualRoot.BuildCmdPath <MarkDeleteServerMessageCommand>(action: message => {
         if (string.IsNullOrEmpty(_connectionString))
         {
             return;
         }
         InitOnece();
         if (_isServer)
         {
             #region Server
             ServerMessageData exist = null;
             lock (_locker) {
                 exist = _linkedList.FirstOrDefault(a => a.Id == message.EntityId);
                 if (exist != null)
                 {
                     exist.IsDeleted = true;
                     exist.Timestamp = DateTime.Now;
                 }
             }
             if (exist != null)
             {
                 using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                     var col = db.GetCollection <ServerMessageData>();
                     col.Update(exist);
                 }
             }
             #endregion
         }
         else
         {
             OfficialServer.ServerMessageService.MarkDeleteServerMessageAsync(message.EntityId, (response, ex) => {
                 VirtualRoot.Execute(new LoadNewServerMessageCommand());
             });
         }
     });
     VirtualRoot.BuildCmdPath <ClearServerMessages>(action: message => {
         if (string.IsNullOrEmpty(_connectionString))
         {
             return;
         }
         InitOnece();
         // 服务端不应有清空消息的功能
         if (_isServer)
         {
             return;
         }
         using (LiteDatabase db = new LiteDatabase(_connectionString)) {
             lock (_locker) {
                 _linkedList.Clear();
             }
             db.DropCollection(nameof(ServerMessageData));
         }
         VirtualRoot.RaiseEvent(new ServerMessagesClearedEvent());
     });
 }
Beispiel #6
0
 public ServerMessageSet(string dbFileFullName, bool isServer)
 {
     if (string.IsNullOrEmpty(dbFileFullName))
     {
         throw new ArgumentNullException(nameof(dbFileFullName));
     }
     _connectionString = $"filename={dbFileFullName}";
     if (!isServer)
     {
         VirtualRoot.AddCmdPath <LoadNewServerMessageCommand>(action: message => {
             if (!VirtualRoot.IsServerMessagesVisible)
             {
                 return;
             }
             DateTime localTimestamp = VirtualRoot.LocalServerMessageSetTimestamp;
             // 如果已知服务器端最新消息的时间戳不比本地已加载的最新消息新就不用加载了
             if (message.KnowServerMessageTimestamp <= Timestamp.GetTimestamp(localTimestamp))
             {
                 return;
             }
             RpcRoot.OfficialServer.ServerMessageService.GetServerMessagesAsync(localTimestamp, (response, e) => {
                 if (response.IsSuccess())
                 {
                     if (response.Data.Count > 0)
                     {
                         ReceiveServerMessage(response.Data);
                     }
                 }
                 else
                 {
                     VirtualRoot.Out.ShowError(response.ReadMessage(e), autoHideSeconds: 4);
                 }
             });
         }, location: this.GetType());
         VirtualRoot.AddCmdPath <ReceiveServerMessageCommand>(action: message => {
             ReceiveServerMessage(message.Data);
         }, location: this.GetType());
     }
     VirtualRoot.AddCmdPath <AddOrUpdateServerMessageCommand>(action: message => {
         InitOnece();
         if (isServer)
         {
             #region Server
             ServerMessageData exist;
             List <ServerMessageData> toRemoves = new List <ServerMessageData>();
             ServerMessageData data             = null;
             lock (_locker) {
                 exist = _linkedList.FirstOrDefault(a => a.Id == message.Input.Id);
                 if (exist != null)
                 {
                     exist.Update(message.Input);
                     exist.Timestamp = DateTime.Now;
                     _linkedList.Remove(exist);
                     _linkedList.AddFirst(exist);
                 }
                 else
                 {
                     data           = new ServerMessageData().Update(message.Input);
                     data.Timestamp = DateTime.Now;
                     _linkedList.AddFirst(data);
                     while (_linkedList.Count > NTKeyword.ServerMessageSetCapacity)
                     {
                         toRemoves.Add(_linkedList.Last.Value);
                         _linkedList.RemoveLast();
                     }
                 }
             }
             if (exist != null)
             {
                 try {
                     using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                         var col = db.GetCollection <ServerMessageData>();
                         col.Update(exist);
                     }
                 }
                 catch (Exception e) {
                     Logger.ErrorDebugLine(e);
                 }
             }
             else
             {
                 try {
                     using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                         var col = db.GetCollection <ServerMessageData>();
                         if (toRemoves.Count != 0)
                         {
                             foreach (var item in toRemoves)
                             {
                                 col.Delete(item.Id);
                             }
                         }
                         col.Insert(data);
                     }
                 }
                 catch (Exception e) {
                     Logger.ErrorDebugLine(e);
                 }
             }
             #endregion
         }
         else
         {
             RpcRoot.OfficialServer.ServerMessageService.AddOrUpdateServerMessageAsync(new ServerMessageData().Update(message.Input), (response, ex) => {
                 if (response.IsSuccess())
                 {
                     VirtualRoot.Execute(new LoadNewServerMessageCommand());
                 }
                 else
                 {
                     VirtualRoot.Out.ShowError(response.ReadMessage(ex), autoHideSeconds: 4);
                 }
             });
         }
     }, location: this.GetType());
     VirtualRoot.AddCmdPath <MarkDeleteServerMessageCommand>(action: message => {
         InitOnece();
         if (isServer)
         {
             #region Server
             ServerMessageData exist = null;
             lock (_locker) {
                 exist = _linkedList.FirstOrDefault(a => a.Id == message.EntityId);
                 if (exist != null)
                 {
                     exist.IsDeleted = true;
                     exist.Content   = string.Empty;
                     exist.Timestamp = DateTime.Now;
                     _linkedList.Remove(exist);
                     _linkedList.AddFirst(exist);
                 }
             }
             if (exist != null)
             {
                 try {
                     using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                         var col = db.GetCollection <ServerMessageData>();
                         col.Update(exist);
                     }
                 }
                 catch (Exception e) {
                     Logger.ErrorDebugLine(e);
                 }
             }
             #endregion
         }
         else
         {
             RpcRoot.OfficialServer.ServerMessageService.MarkDeleteServerMessageAsync(message.EntityId, (response, ex) => {
                 if (response.IsSuccess())
                 {
                     VirtualRoot.Execute(new LoadNewServerMessageCommand());
                 }
                 else
                 {
                     VirtualRoot.Out.ShowError(response.ReadMessage(ex), autoHideSeconds: 4);
                 }
             });
         }
     }, location: this.GetType());
     VirtualRoot.AddCmdPath <ClearServerMessages>(action: message => {
         InitOnece();
         // 服务端不应有清空消息的功能
         if (isServer)
         {
             return;
         }
         try {
             using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                 lock (_locker) {
                     _linkedList.Clear();
                 }
                 db.DropCollection(nameof(ServerMessageData));
             }
         }
         catch (Exception e) {
             Logger.ErrorDebugLine(e);
         }
         VirtualRoot.RaiseEvent(new ServerMessagesClearedEvent());
     }, location: this.GetType());
 }
Beispiel #7
0
 public ServerMessageSet(string dbFileFullName)
 {
     if (string.IsNullOrEmpty(dbFileFullName))
     {
         throw new ArgumentNullException(nameof(dbFileFullName));
     }
     _connectionString = $"filename={dbFileFullName}";
     VirtualRoot.BuildCmdPath <AddOrUpdateServerMessageCommand>(location: this.GetType(), LogEnum.DevConsole, path: message => {
         InitOnece();
         #region Server
         ServerMessageData exist;
         List <ServerMessageData> toRemoves = new List <ServerMessageData>();
         ServerMessageData data             = null;
         lock (_linkedList) {
             exist = _linkedList.FirstOrDefault(a => a.Id == message.Input.Id);
             if (exist != null)
             {
                 exist.Update(message.Input);
                 exist.Timestamp = DateTime.Now;
                 _linkedList.Remove(exist);
                 _linkedList.AddFirst(exist);
             }
             else
             {
                 data           = new ServerMessageData().Update(message.Input);
                 data.Timestamp = DateTime.Now;
                 _linkedList.AddFirst(data);
                 while (_linkedList.Count > NTKeyword.ServerMessageSetCapacity)
                 {
                     toRemoves.Add(_linkedList.Last.Value);
                     _linkedList.RemoveLast();
                 }
             }
         }
         if (exist != null)
         {
             try {
                 using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                     var col = db.GetCollection <ServerMessageData>();
                     col.Update(exist);
                 }
             }
             catch (Exception e) {
                 Logger.ErrorDebugLine(e);
             }
         }
         else
         {
             try {
                 using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                     var col = db.GetCollection <ServerMessageData>();
                     if (toRemoves.Count != 0)
                     {
                         foreach (var item in toRemoves)
                         {
                             col.Delete(item.Id);
                         }
                     }
                     col.Insert(data);
                 }
             }
             catch (Exception e) {
                 Logger.ErrorDebugLine(e);
             }
         }
         SetServerMessageTimestamp();
         #endregion
     });
     VirtualRoot.BuildCmdPath <MarkDeleteServerMessageCommand>(location: this.GetType(), LogEnum.DevConsole, path: message => {
         InitOnece();
         #region Server
         ServerMessageData exist = null;
         lock (_linkedList) {
             exist = _linkedList.FirstOrDefault(a => a.Id == message.EntityId);
             if (exist != null)
             {
                 exist.IsDeleted = true;
                 exist.Content   = string.Empty;
                 exist.Timestamp = DateTime.Now;
                 _linkedList.Remove(exist);
                 _linkedList.AddFirst(exist);
             }
         }
         if (exist != null)
         {
             try {
                 using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                     var col = db.GetCollection <ServerMessageData>();
                     col.Update(exist);
                 }
             }
             catch (Exception e) {
                 Logger.ErrorDebugLine(e);
             }
         }
         #endregion
         SetServerMessageTimestamp();
     });
 }