示例#1
0
        // broadcast: [methodId, [argument]]

        public Task WriteAllAsync <T>(int methodId, T value, bool fireAndForget)
        {
            var message = BuildMessage(methodId, value);

            var source = members;

            if (fireAndForget)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    WriteInAsyncLockVoid(source[i], message);
                }
                logger.InvokeHubBroadcast(GroupName, message.Length, source.Length);
                return(TaskEx.CompletedTask);
            }
            else
            {
                var promise = new ReservedWhenAllPromise(source.Length);
                for (int i = 0; i < source.Length; i++)
                {
                    promise.Add(WriteInAsyncLock(source[i], message));
                }
                logger.InvokeHubBroadcast(GroupName, message.Length, source.Length);
                return(promise.AsValueTask().AsTask());
            }
        }
        ValueTask ToPromise(ValueTask[] whenAll, int index)
        {
            var promise = new ReservedWhenAllPromise(index);

            for (int i = 0; i < index; i++)
            {
                promise.Add(whenAll[i]);
            }
            return(promise.AsValueTask());
        }
示例#3
0
        public Task WriteExceptAsync <T>(int methodId, T value, Guid[] connectionIds, bool fireAndForget)
        {
            var message = BuildMessage(methodId, value);

            var source = members;

            if (fireAndForget)
            {
                var writeCount = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    foreach (var item in connectionIds)
                    {
                        if (source[i].ContextId == item)
                        {
                            goto NEXT;
                        }
                    }

                    WriteInAsyncLockVoid(source[i], message);
                    writeCount++;
NEXT:
                    continue;
                }
                logger.InvokeHubBroadcast(GroupName, message.Length, writeCount);
                return(TaskEx.CompletedTask);
            }
            else
            {
                var promise    = new ReservedWhenAllPromise(source.Length);
                var writeCount = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    foreach (var item in connectionIds)
                    {
                        if (source[i].ContextId == item)
                        {
                            promise.Add(default(ValueTask));
                            goto NEXT;
                        }
                    }

                    promise.Add(WriteInAsyncLock(source[i], message));
                    writeCount++;
NEXT:
                    continue;
                }

                logger.InvokeHubBroadcast(GroupName, message.Length, writeCount);
                return(promise.AsValueTask().AsTask());
            }
        }
示例#4
0
        public Task WriteToAsync <T>(int methodId, T value, Guid connectionId, bool fireAndForget)
        {
            var message = BuildMessage(methodId, value);

            var source = members;

            if (fireAndForget)
            {
                var writeCount = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    if (source[i].ContextId == connectionId)
                    {
                        WriteInAsyncLockVoid(source[i], message);
                        writeCount++;
                        break;
                    }
                }
                logger.InvokeHubBroadcast(GroupName, message.Length, writeCount);
                return(TaskEx.CompletedTask);
            }
            else
            {
                var promise    = new ReservedWhenAllPromise(source.Length);
                var writeCount = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    if (source[i].ContextId == connectionId)
                    {
                        promise.Add(WriteInAsyncLock(source[i], message));
                        writeCount++;
                        break;
                    }
                }
                logger.InvokeHubBroadcast(GroupName, message.Length, writeCount);
                return(promise.AsValueTask().AsTask());
            }
        }
示例#5
0
        public Task WriteRawAsync(ArraySegment <byte> msg, Guid[] exceptConnectionIds, bool fireAndForget)
        {
            // oh, copy is bad but current gRPC interface only accepts byte[]...
            var message = new byte[msg.Count];

            Array.Copy(msg.Array, msg.Offset, message, 0, message.Length);

            var source = members;

            if (fireAndForget)
            {
                var writeCount = 0;
                if (exceptConnectionIds == null)
                {
                    for (int i = 0; i < source.Length; i++)
                    {
                        WriteInAsyncLockVoid(source[i], message);
                        writeCount++;
                    }
                }
                else
                {
                    for (int i = 0; i < source.Length; i++)
                    {
                        foreach (var item in exceptConnectionIds)
                        {
                            if (source[i].ContextId == item)
                            {
                                goto NEXT;
                            }
                        }
                        WriteInAsyncLockVoid(source[i], message);
                        writeCount++;
NEXT:
                        continue;
                    }
                }
                logger.InvokeHubBroadcast(GroupName, message.Length, writeCount);
                return(TaskEx.CompletedTask);
            }
            else
            {
                var promise    = new ReservedWhenAllPromise(source.Length);
                var writeCount = 0;
                if (exceptConnectionIds == null)
                {
                    for (int i = 0; i < source.Length; i++)
                    {
                        promise.Add(WriteInAsyncLock(source[i], message));
                        writeCount++;
                    }
                }
                else
                {
                    for (int i = 0; i < source.Length; i++)
                    {
                        foreach (var item in exceptConnectionIds)
                        {
                            if (source[i].ContextId == item)
                            {
                                promise.Add(default(ValueTask));
                                goto NEXT;
                            }
                        }

                        promise.Add(WriteInAsyncLock(source[i], message));
                        writeCount++;
NEXT:
                        continue;
                    }
                }

                logger.InvokeHubBroadcast(GroupName, message.Length, writeCount);
                return(promise.AsValueTask().AsTask());
            }
        }