Example #1
0
        public async Task <JoinGroupResponse> JoinAsync(IPeer peer, object arg)
        {
            // check if group is closed
            if (isClosed)
            {
                return(new JoinGroupResponse(GroupId, OperationCode, JoinGroupResponse.ResultType.Cancelled, string.Format("Group is closed.")));
            }

            // check if peer has joined
            if (peers.ContainsKey(peer.Id))
            {
                return(new JoinGroupResponse(GroupId, OperationCode, JoinGroupResponse.ResultType.HasJoined, string.Format("Has joined in group")));
            }

            // check if peer is in queue
            if (joinQueuing.Exists(req => req.Peer.Id == peer.Id))
            {
                return(new JoinGroupResponse(GroupId, OperationCode, JoinGroupResponse.ResultType.InQueue, string.Format("Join request is in queue.")));
            }

            // check if peer is been handling
            if (joinHandling.Exists(req => req.Peer.Id == peer.Id))
            {
                return(new JoinGroupResponse(GroupId, OperationCode, JoinGroupResponse.ResultType.Handling, string.Format("Join request is handling.")));
            }

            // add request into queue and waiting result
            JoinGroupRequest request = new JoinGroupRequest(GroupId, OperationCode, peer, arg);

            joinQueuing.Add(request);
            JoinGroupResponse result = await request.Task;

            joinHandling.Remove(request);       // remove request from handling (because finished)
            if (result.type == JoinGroupResponse.ResultType.Accepted)
            {
                lock (peers) peers.Add(peer.Id, peer);
                peer.TrackGroup(this);
                try
                {
                    if (OnPeerJoinedEvent != null)
                    {
                        OnPeerJoinedEvent.Invoke(peer);
                    }
                }
                catch (Exception e)
                {
                    debugger.LogError(e);
                }
            }
            return(result);
        }