Beispiel #1
0
        void BulkSendAsync(byte[] buffer, int length, Span <IPEndPoint> endPoints, Action onComplete)
        {
            for (int endpointIndex = 0; endpointIndex < endPoints.Length; endpointIndex++)
            {
                var args = _socketEventArgsPool.Get();
                args.RemoteEndPoint = endPoints[endpointIndex];
                args.SetBuffer(buffer, 0, length);

                if (!_socket.SendToAsync(args))
                {
                    Interlocked.Increment(ref _waitingSendCount);
                    //completed syncronously
                    _socketEventArgsPool.Add(args); //release the memory back to the pool
                }
            }
            lock (_asyncCompleteLock)
            {
                if (_waitingSendCount == 0)
                {
                    onComplete();
                    return;
                }
                _onBulkAsyncFinished = onComplete;
            }
        }
Beispiel #2
0
        public void BulkSendAsync(byte[] buffer, int length, Span <IPEndPoint> endPoints, Action <object?> onComplete, object state, IPEndPoint except, uint groupId, CachedEncryptionKey keyInfo)
        {
            var token = _bulkRequestTokenPool.Get();

            token.Reset();
            token.Finished = onComplete;
            token.State    = state;

            for (int endpointIndex = 0; endpointIndex < endPoints.Length; endpointIndex++)
            {
                var args = _socketEventArgsPool.Get();
                args.UserToken = token;
                var endPoint = endPoints[endpointIndex];
                if (endPoint.Equals(except))
                {
                    continue;
                }
                args.RemoteEndPoint = endPoint;
                SetBufferEncrypted(args, buffer, length, groupId, keyInfo);

                if (!_socket.SendToAsync(args))
                {
                    //completed syncronously
                    _socketEventArgsPool.Add(args); //release the memory back to the pool
                }
                else
                {
                    token.IncrementWaitingCount();
                }
            }
            lock (_asyncCompleteLock)
            {
                if (token.WaitingCount == 0)
                {
                    token.Finish();
                    return;
                }
            }
        }
Beispiel #3
0
        public void Add(T item)
        {
            lock (_writeLock)
            {
                if (_locked)
                {
                    var setChange = _setChangePool.Get();
                    setChange.ChangeType = ChangeType.Add;
                    setChange.Value      = item;
                    _queuedChanges.Add(setChange);
                    return;
                }
                //check if we already have it
                if (_indexLookup.ContainsKey(item))
                {
                    return;
                }

                AddUnsafe(item);

                _indexLookup.Add(item, _length - 1);
            }
        }
Beispiel #4
0
        public unsafe void OnNext(StreamMessage <CompoundGroupKey <TOuterKey, TInnerKey>, TInnerResult> batch)
        {
            outPool.Get(out StreamMessage <TOuterKey, TResult> tmp);
            tmp.AllocatePayload();
            outPool.GetKey(out tmp.key);
            tmp.hash = batch.hash.MakeWritable(outPool.intPool);
            var count = batch.Count;

            tmp.vsync     = batch.vsync;
            tmp.vother    = batch.vother;
            tmp.bitvector = batch.bitvector;

            fixed(long *srcbv = batch.bitvector.col)
            fixed(int *desthash = tmp.hash.col)
            {
                var srckey  = batch.key.col;
                var destkey = tmp.key.col;

                for (int i = 0; i < count; i++)
                {
                    if ((srcbv[i >> 6] & (1L << (i & 0x3f))) != 0)
                    {
                        if (batch.vother.col[i] == long.MinValue)
                        {
                            destkey[i]  = srckey[i].outerGroup;
                            desthash[i] = batch.hash.col[i];
                            tmp.bitvector.col[i >> 6] |= 1L << (i & 0x3f);
                        }
                        continue;
                    }
                    destkey[i]  = srckey[i].outerGroup;
                    tmp[i]      = resultSelector(srckey[i].innerGroup, batch[i]);
                    desthash[i] = this.outerHashCode(destkey[i]);
                }
            }

            tmp.Count = count;
            tmp.Seal();

            Observer.OnNext(tmp);

            batch.ReleasePayload();
            batch.key.Return();
            batch.Return();
        }
Beispiel #5
0
        public void SendCallEnded(ushort groupId, Span <IPEndPoint> endPoints, CachedEncryptionKey keyInfo)
        {
            var buffer = _sendBufferPool.Get();

            // Packet Type
            buffer[0] = (byte)RopuPacketType.CallEnded;
            // Group ID (uint16)
            buffer.WriteUshort(groupId, 1);

            BulkSendEncrypted(buffer.AsSpan(0, 3), groupId, endPoints, keyInfo);

            _sendBufferPool.Add(buffer);
        }
Beispiel #6
0
        public unsafe void OnNext(StreamMessage <TInnerKey, TInnerResult> batch)
        {
            outPool.Get(out StreamMessage <Empty, TResult> tmp);
            tmp.AllocatePayload();
            outPool.GetKey(out tmp.key);
            tmp.hash = batch.hash.MakeWritable(outPool.intPool);
            var count = batch.Count;

            tmp.vsync     = batch.vsync;
            tmp.vother    = batch.vother;
            tmp.bitvector = batch.bitvector;

            fixed(long *srcbv = batch.bitvector.col)
            fixed(int *desthash = tmp.hash.col)
            {
                var srckey  = batch.key.col;
                var destkey = tmp.key.col;

                Array.Clear(tmp.hash.col, 0, count);

                var unit = Empty.Default;

                for (int i = 0; i < count; i++)
                {
                    if ((srcbv[i >> 6] & (1L << (i & 0x3f))) != 0)
                    {
                        continue;
                    }
                    destkey[i] = unit;
                    tmp[i]     = resultSelector(srckey[i], batch[i]);
                }
            }

            tmp.Count = count;
            tmp.Seal();

            Observer.OnNext(tmp);

            batch.ReleasePayload();
            batch.key.Return();
            batch.Return();
        }
Beispiel #7
0
        public void Serialize(Stream serializationStream, object graph)
        {
            IMessage message = (graph as IMessage);

            serializationStream.EncodeVariableInt(message.Id);

            PropertyManager.ReadLock();
            try
            {
                Dictionary <UInt32, PropertyContainer> .Enumerator iterator = PropertyManager.GetEnumerator();
                if (iterator.MoveNext())
                {
                    MemoryStream components = MemoryPool <MemoryStream> .Get();

                    try
                    {
                        do
                        {
                            object value; if (iterator.Current.Value.TryGet(message.Template, out value) && value != null && TypeFormatter.CanSerialize(value.GetType()))
                            {
                                components.Encode(iterator.Current.Key);
                                TypeFormatter.Serialize(components, value);

                                serializationStream.Encode(components.Length);
                                components.Seek(0, SeekOrigin.Begin);
                                components.CopyTo(serializationStream);
                                components.SetLength(0);
                            }
                        }while (iterator.MoveNext());
                    }
                    finally
                    {
                        MemoryPool <MemoryStream> .Return(components);
                    }
                }
            }
            finally
            {
                PropertyManager.ReadRelease();
            }
            serializationStream.Encode((long)0);
        }
Beispiel #8
0
        async ValueTask <bool> AwaitRequest <H>(
            ushort requestId, H handler, byte[] buffer, IPEndPoint endPoint,
            ManualResetEvent manualResetEvent, int length) where H : class
        {
            var userId  = UserId;
            var keyInfo = _keysClient.GetMyKeyInfo();

            if (userId == null)
            {
                Console.Error.WriteLine($"Could not send packet because userId is null");
                return(false);
            }
            if (keyInfo == null)
            {
                Console.Error.WriteLine($"Could not send packet because keyInfo is null");
                return(false);
            }

            _requests[requestId] = handler;

            var packet = _sendBufferPool.Get();

            try
            {
                int packetLength = _packetEncryption.CreateEncryptedPacket(buffer.AsSpan(0, length), packet, false, userId.Value, keyInfo);

                _socket.SendTo(packet, 0, packetLength, SocketFlags.None, endPoint);
                bool acknowledged = await AwaitResetEvent(manualResetEvent);

                _sendBufferPool.Add(packet);

                _requests[requestId] = null;
                return(acknowledged);
            }
            catch (Exception exception)
            {
                Console.Error.WriteLine(exception);
                throw exception;
            }
        }