private byte[] HandleDelFriend(EventDataMsg eventMsg, MsgContext ctx) { User userReq = _api.Services.User.GetUserByClientId(ctx.senderId); if (userReq == null) { throw new FriendsAppProviderException("You must be signedIn to add some friends."); } User userTo = User.Get(eventMsg.LongValue); if (userTo == null) { throw new FriendsAppProviderException("User with this ID could not be found."); } List <ulong> friendList = _friendsData.GetFriendsList(userReq.Id); if (friendList.Contains(userTo.Id)) { _friendsData.RemoveFriend(userReq.Id, userTo.Id); } else { List <ulong> fromRequests = _friendsData.GetFriendRequests(userReq.Id); if (fromRequests.Contains(userTo.Id)) { _friendsData.RemoveFriendRequest(userTo.Id, userReq.Id); } else { _friendsData.RemoveFriendRequest(userReq.Id, userTo.Id); } } return(null); }
public EventResponse Handle(ulong clientId, EventDataMsg eventMsg, EventResponse response) { if (!_eventMasks.TryGetValue(clientId, out EventMask eventMask)) { lock (_eventMasks) { eventMask = new EventMask(); _eventMasks[clientId] = eventMask; } } lock (eventMask) { if (eventMsg.EventId == 0) { lock (_eventMasks) { _eventMasks[clientId] = new EventMask(); } } if (eventMsg.EventId > eventMask.HighestId) { eventMask.Mask = eventMask.Mask << (int)(eventMsg.EventId - eventMask.HighestId); eventMask.Mask |= 0x01; eventMask.HighestId = eventMsg.EventId; } else if (eventMask.HighestId - eventMsg.EventId < 32) { eventMask.Mask |= (uint)0x01 << (int)(eventMask.HighestId - eventMsg.EventId); } response.EventMask = eventMask.Mask; response.HighestEventId = eventMask.HighestId; return(response); } }
private byte[] HandleAddFriend(EventDataMsg eventMsg, MsgContext ctx) { if (ctx.senderType != SenderType.USER) { throw new FriendsAppProviderException("Only client machine can call this event."); } User userReq = _api.Services.User.GetUserByClientId(ctx.senderId); if (userReq == null) { throw new FriendsAppProviderException("You must be signedIn to add some friends."); } User userTo = User.Get(eventMsg.LongValue); if (userTo == null) { throw new FriendsAppProviderException("User with this ID could not be found."); } List <ulong> requests = _friendsData.GetFriendRequests(userReq.Id); if (requests.Contains(userTo.Id)) { _friendsData.AcceptFriendRequest(userTo.Id, userReq.Id); } else { _friendsData.SendFriendRequest(userReq.Id, userTo.Id); } return(null); }
private byte[] HandleGetFriend(EventDataMsg eventMsg, MsgContext ctx) { User userReq = _api.Services.User.GetUserByClientId(ctx.senderId); if (userReq == null) { throw new FriendsAppProviderException("You must be signedIn to add some friends."); } User userTo = User.Get(eventMsg.LongValue); if (userTo == null) { throw new FriendsAppProviderException("User with this ID could not be found."); } List <ulong> friends = _friendsData.GetFriendsList(userReq.Id); if (!friends.Contains(userTo.Id)) { throw new FriendsAppProviderException("This user is not your friend."); } FriendsAppUser friendDetail = ToFriendsAppUser(userTo.Id); FriendsAppMsg msg = new FriendsAppMsg(); msg.FriendDetail = friendDetail.ToNetworkModel(); return(msg.ToByteArray()); }
public ServiceCallback <FriendsAppUser> GetFriendDetails(ulong userId) { return(new ServiceCallback <FriendsAppUser>(() => { EventDataMsg msg = new EventDataMsg(); msg.AppId = APP_ID; msg.EventType = (uint)FriendsAppEvents.GET_FRIEND; msg.LongValue = userId; byte[] data; try { data = _api.Event.SendEvent(msg, EventRecipient.PROVIDER).Wait(); } catch (EventServiceException e) { throw new FriendsAppException(e.Message); } FriendsAppMsg friendsAppMsg = FriendsAppMsg.Parser.ParseFrom(data); if (friendsAppMsg == null || friendsAppMsg.FriendDetail == null) { throw new FriendsAppException("Unknown response."); } return new FriendsAppUser(friendsAppMsg.FriendDetail); })); }
public ServiceCallback <List <FriendsAppUser> > ListFriends() { return(new ServiceCallback <List <FriendsAppUser> >(() => { EventDataMsg msg = new EventDataMsg(); msg.AppId = APP_ID; msg.EventType = (uint)FriendsAppEvents.LIST_FRIENDS; byte[] data; try { data = _api.Event.SendEvent(msg, EventRecipient.PROVIDER).Wait(); } catch (EventServiceException e) { throw new FriendsAppException(e.Message); } FriendsAppMsg friendsAppMsg = FriendsAppMsg.Parser.ParseFrom(data); if (friendsAppMsg == null || friendsAppMsg.FriendsList == null) { throw new FriendsAppException("Unknown response."); } return friendsAppMsg.FriendsList.FriendsList .Where(x => x != null) .Select(x => new FriendsAppUser(x)) .ToList(); })); }
public void SetId(EventDataMsg eventMsg) { lock (_eventLock) { eventMsg.EventId = _eventsSent++; _eventBuffer.Enqueue(eventMsg); } }
private EventResponse HandleGameObjectEvent(ulong msgId, ulong clientId, EventDataMsg eventMsg) { GameObject gameObject = eventMsg.ObjectValue; if (gameObject == null) { return(IEventService.CreateErrorResponse(msgId, 0, 0, "GameObject value cannot be null.")); } return(null); }
public EventResponse HandleEvent(MainMessage msg) { EventDataMsg eventMsg = msg.EventMsg.EventDataMsg; EventType eventType = (EventType)eventMsg.EventType; switch (eventType) { case EventType.SKELETON_STATE: return(HandleSkeletonEvent(msg.MsgId, msg.ClientId, eventMsg)); case EventType.OBJECT_STATE: return(HandleGameObjectEvent(msg.MsgId, msg.ClientId, eventMsg)); default: return(IEventService.CreateErrorResponse(msg.MsgId, 0, 0, "Wrong event type.")); } }
public MainMessage HandleMessage(MainMessage msg) { Stopwatch sw = new Stopwatch(); sw.Start(); EventDataMsg eventMsg = msg.EventMsg.EventDataMsg; EventResponse response = new EventResponse(); if (eventMsg == null) { return(ISystemService.CreateErrorMessage(msg.MsgId, 0, 0, "Forwarder can process only EventDataMsg type.")); } if (eventMsg.AppTypeCase != EventDataMsg.AppTypeOneofCase.None) { try { byte[] responseData = _api.Services.App.HandleEvent(msg); if (responseData != null) { response.Data = ByteString.CopyFrom(responseData); } } catch (EventErrorException e) { _log.Error(e); response = IEventService.CreateErrorResponse(msg.MsgId, 0, 0, e.Message); } } else { EventResponse handleResponse = HandleEvent(msg); if (handleResponse != null) { response = handleResponse; } } response.ProcessTime = (uint)sw.ElapsedMilliseconds; response = _maskHandler.Handle(msg.ClientId, eventMsg, response); MainMessage responseMsg = new MainMessage(); responseMsg.EventMsg = new EventMsg(); responseMsg.EventMsg.EventResponse = response; return(responseMsg); }
private ServiceCallback <bool> SendRmFriend(ulong userId) { return(new ServiceCallback <bool>(() => { EventDataMsg msg = new EventDataMsg(); msg.AppId = APP_ID; msg.EventType = (uint)FriendsAppEvents.DEL_FRIEND; msg.LongValue = userId; try { _api.Event.SendEvent(msg, EventRecipient.PROVIDER).Wait(); } catch (EventServiceException e) { throw new FriendsAppException(e.Message); } return true; })); }
public MainMessage HandleMessage(MainMessage msg) { EventDataMsg dataMsg = msg.EventMsg.EventDataMsg; if (dataMsg == null) { return(ISystemService.CreateErrorMessage(msg.MsgId, 0, 0, "Invalid msg type.")); } EventResponse response; if (msg.EventMsg.EventDataMsg.AppTypeCase != EventDataMsg.AppTypeOneofCase.None) { try { response = new EventResponse(); byte[] responseData = _api.Services.App.HandleEvent(msg); if (responseData != null) { response.Data = ByteString.CopyFrom(responseData); } } catch (EventErrorException e) { _log.Error(e); response = IEventService.CreateErrorResponse(msg.MsgId, 0, 0, e.Message); } } else { const string err_msg = "Main server does not handle events of this type."; _log.Error(err_msg); response = IEventService.CreateErrorResponse(msg.MsgId, 0, 0, err_msg); } response = _maskHandler.Handle(msg.ClientId, dataMsg, response); MainMessage responseMsg = new MainMessage(); responseMsg.EventMsg = new EventMsg(); responseMsg.EventMsg.EventResponse = response; return(responseMsg); }
public byte[] HandleEvent(EventDataMsg eventData, MsgContext ctx) { if (ctx.senderType != SenderType.USER) { throw new ChatAppException("Server can not use this app."); } ulong?userId = _api.User.ClientId2UserId(ctx.senderId); if (!userId.HasValue) { throw new ChatAppException("You must be signedIn to add some friends."); } switch ((ChatAppEventType)eventData.EventType) { case ChatAppEventType.GET_UPDATE: return(GetMsgUpdate(userId.Value)); default: throw new ChatAppException("Unknown event type."); } }
private byte[] HandleListFriendRequests(EventDataMsg eventMsg, MsgContext ctx) { User userReq = _api.Services.User.GetUserByClientId(ctx.senderId); if (userReq == null) { throw new FriendsAppProviderException("You must be signedIn to add some friends."); } List <ulong> requests = _friendsData.GetFriendRequests(userReq.Id); List <FriendsAppUser> requestUsers = requests .Select(x => User.Get(x)) .Where(x => x != null) .Select(x => new FriendsAppUser(x.ToMessage(), null, null)) .ToList(); FriendsAppMsg msg = new FriendsAppMsg(); msg.FriendRequests = new FriendsAppRequestsMsg(); msg.FriendRequests.ToUser = userReq.Id; msg.FriendRequests.FriendRequestsList.AddRange(requestUsers.Select(x => x.ToNetworkModel())); return(msg.ToByteArray()); }
private byte[] HandleListFriends(EventDataMsg eventMsg, MsgContext ctx) { User userReq = _api.Services.User.GetUserByClientId(ctx.senderId); if (userReq == null) { throw new FriendsAppProviderException("You must be signedIn to add some friends."); } List <ulong> friendIds = _friendsData.GetFriendsList(userReq.Id); if (friendIds == null) { friendIds = new List <ulong>(); } List <FriendsAppUser> friends = friendIds.Select(x => ToFriendsAppUser(x)).ToList(); FriendsAppMsg msg = new FriendsAppMsg(); msg.FriendsList = new FriendsAppListMsg(); msg.FriendsList.FriendsList.AddRange(friends.Select(x => x.ToNetworkModel())); return(msg.ToByteArray()); }
private EventResponse HandleSkeletonEvent(ulong msgId, ulong clientId, EventDataMsg eventMsg) { Skeleton skeleton = eventMsg.SkeletonValue; if (skeleton == null) { return(IEventService.CreateErrorResponse(msgId, 0, 0, "Skeleton value cannot be null.")); } if (!_api.Services.User.FastCheckUserId(clientId, skeleton.UserId)) { return(IEventService.CreateErrorResponse(msgId, 0, 0, "Authentication error.")); } uint?roomId = _api.Services.Room.RoomByUserId(skeleton.UserId); if (!roomId.HasValue) { return(IEventService.CreateErrorResponse(msgId, 0, 0, "User is not connected to any room.")); } SkeletonState skelState = new SkeletonState(skeleton); _api.Services.TickRate.SetSkeletonState(roomId.Value, skeleton.UserId, skelState); return(null); }
public byte[] HandleEvent(MainMessage msg) { EventDataMsg eventData = msg.EventMsg.EventDataMsg; if (eventData == null) { throw new EventErrorException("Invalid EventData msg."); } ulong appId = eventData.AppId; if (!_appInstances.ContainsKey(appId)) { throw new EventErrorException("No handler could be found for this event."); } try { return(_appInstances[appId].HandleEvent(eventData, new MsgContext(msg))); } catch (Exception e) { throw new EventErrorException($"{e.GetType().Name}: {e.Message}"); } }
public byte[] HandleEvent(EventDataMsg eventData, MsgContext ctx) { switch ((FriendsAppEvents)eventData.EventType) { case FriendsAppEvents.ADD_FRIEND: return(HandleAddFriend(eventData, ctx)); case FriendsAppEvents.DEL_FRIEND: return(HandleDelFriend(eventData, ctx)); case FriendsAppEvents.GET_FRIEND: return(HandleGetFriend(eventData, ctx)); case FriendsAppEvents.LIST_FRIEND_REQUESTS: return(HandleListFriendRequests(eventData, ctx)); case FriendsAppEvents.LIST_FRIENDS: return(HandleListFriends(eventData, ctx)); default: throw new FriendsAppProviderException("Unknown event type."); } }
public byte[] HandleEvent(MainMessage msg) { EventDataMsg eventData = msg.EventMsg.EventDataMsg; if (eventData == null) { throw new EventErrorException("Invalid EventData msg."); } ulong?userId = _api.Services.User.GetUserIdByClientId(msg.ClientId, true); if (!userId.HasValue) { throw new EventErrorException("Unauthenticated user."); } uint?roomId = _api.Services.Room.RoomByUserId(userId.Value); if (!roomId.HasValue) { throw new EventErrorException("User is not connected to any room."); } ulong appId = eventData.AppId; if (!_appInstances.ContainsKey(roomId.Value) || !_appInstances[roomId.Value].ContainsKey(appId)) { throw new EventErrorException("No handler could be found for this event."); } try { return(_appInstances[roomId.Value][appId].HandleEvent(eventData, new MsgContext(msg))); } catch (Exception e) { _log.Error(e); throw new EventErrorException(e.Message); } }
private IEnumerator ChatCoroutine() { ChatAppMsg msg = new ChatAppMsg(); msg.Request = new ChatRequest(); msg.Request.ListRequest = true; var req = _api.App.SendAppMsg(_info, msg.ToByteArray(), AppMsgRecipient.PROVIDER); yield return(req.WaitCoroutine()); if (req.HasException) { throw req.Exception; } if (req.Result != null) { ChatAppMsg response = ChatAppMsg.Parser.ParseFrom(req.Result); if (response == null || response.List == null) { throw new ChatAppException("Unknown response."); } _chats = response.List.List .Select(x => new ChatObj(x)) .ToDictionary(x => x.User1 == _userId ? x.User2 : x.User1, x => x); yield return(null); } else { _chats = new Dictionary <ulong, ChatObj>(); } _recentBlock.SetChatList(_chats.Select(x => x.Value).ToList()); yield return(null); while (true) { EventDataMsg updateReq = new EventDataMsg(); updateReq.AppId = APP_ID; updateReq.EventType = (uint)ChatAppEventType.GET_UPDATE; var updateReqService = _api.Event.SendEvent(updateReq, EventRecipient.PROVIDER); yield return(updateReqService.WaitCoroutine()); if (updateReqService.HasException) { yield return(new WaitForSeconds(UPDATE_INTERVAL_SEC)); continue; } ChatUpdateMsg updateMsg = ChatUpdateMsg.Parser.ParseFrom(updateReqService.Result); if (updateMsg == null) { yield return(new WaitForSeconds(UPDATE_INTERVAL_SEC)); continue; } List <ChatObjMsg> msgs = updateMsg.Messages .Select(x => new ChatObjMsg(x)) .ToList(); Update(msgs); yield return(new WaitForSeconds(UPDATE_INTERVAL_SEC)); } }
public byte[] HandleEvent(EventDataMsg eventData, MsgContext context) { throw new NotImplementedException(); }