Exemplo n.º 1
0
        void OnPollServer(Poll handle, PollStatus status)
        {
            Socket socket       = this.serverContext.Socket.Accept();
            IntPtr socketHandle = TestHelper.GetHandle(socket);

            const PollMask Mask = PollMask.Readable | PollMask.Writable | PollMask.Disconnect;
            Poll           poll = this.loop
                                  .CreatePoll(socketHandle)
                                  .Start(Mask, this.OnPollConnection);

            Timer timer   = this.loop.CreateTimer();
            var   context = new ConnectionContext(socket, poll, true)
            {
                TimerHandle = timer,
                EventMask   = Mask,
                OpenHandles = 2
            };

            timer.UserToken = context;
            poll.UserToken  = context;

            this.serverContext.ConnectionCount++;
            if (this.serverContext.ConnectionCount < NumberOfClients)
            {
                return;
            }

            DestroyServerContext(this.serverContext);
        }
Exemplo n.º 2
0
 void OnPoll(Poll poll, PollStatus status)
 {
     this.eventArgs.Dispose();
     poll.Start(PollMask.Readable, this.OnPoll);
     this.socket?.Dispose();
     poll.CloseHandle(this.OnClose);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Return all polls with the given status
 /// </summary>
 /// <param name="status"></param>
 /// <returns></returns>
 public async Task <IEnumerable <Poll> > GetPollsByStatusAsync(PollStatus status)
 {
     return(await _context.Polls
            .Where(p => p.Status == status)
            .Include(q => q.Questions)
            .ThenInclude(a => a.Answers)
            .ToListAsync());
 }
Exemplo n.º 4
0
        public ActionResult UpdatePollStatus(long pollId, PollStatus pollStatus)
        {
            MyPollDelegate del = (Polls poll) => {
                poll.Status = (long)pollStatus;
                _db.SaveChanges();
                TempData["FlashMessage"] = "Poll status updated!";
            };

            return(MyPoll(pollId, del));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> GetPolls([FromQuery] PollStatus status = PollStatus.Undefined)
        {
            IEnumerable <Poll> polls;

            if (status != PollStatus.Undefined)
            {
                _logger.LogInformation(LoggingEvents.GetPolls, "Listing all polls with status ({status})", status.ToString());
                polls = await _pollRepository.GetPollsByStatusAsync(status);
            }
            else
            {
                _logger.LogInformation(LoggingEvents.GetPolls, "Listing all polls");
                polls = await _pollRepository.GetAllPollsAsync();
            }

            return(Ok(polls));
        }
Exemplo n.º 6
0
 void OnPoll(Poll poll, PollStatus status)
 {
     poll.CloseHandle(this.OnClose);
     this.pollCount++;
 }
Exemplo n.º 7
0
        void OnPollConnection(Poll handle, PollStatus status)
        {
            var context = (ConnectionContext)handle.UserToken;

            PollMask pollMask  = status.Mask;
            PollMask newEvents = context.EventMask;
            var      random    = new Random(10);

            if ((pollMask & PollMask.Readable) == PollMask.Readable)
            {
                int action = random.Next() % 7;

                if (action == 0 ||
                    action == 1)
                {
                    // Read a couple of bytes.
                    var buffer = new byte[74];
                    int count  = TryReceive(context.Socket, buffer);
                    if (count > 0)
                    {
                        context.Receive += count;
                    }
                    else
                    {
                        // Got FIN.
                        context.ReceiveFinished = true;
                        newEvents &= ~PollMask.Readable;
                    }
                }
                else if (action == 2 ||
                         action == 3)
                {
                    // Read until EAGAIN.
                    var buffer = new byte[931];
                    int count  = TryReceive(context.Socket, buffer);
                    while (count > 0)
                    {
                        context.Receive += count;
                        count            = TryReceive(context.Socket, buffer);
                    }

                    if (count == 0)
                    {
                        // Got FIN.
                        context.ReceiveFinished = true;
                        newEvents &= ~PollMask.Readable;
                    }
                }
                else if (action == 4)
                {
                    // Ignore.
                }
                else if (action == 5)
                {
                    // Stop reading for a while. Restart in timer callback.
                    newEvents &= ~PollMask.Readable;

                    if (!context.TimerHandle.IsActive)
                    {
                        context.DelayedEventMask = PollMask.Readable;
                        context.TimerHandle.Start(this.OnTimerDelay, 10, 0);
                    }
                    else
                    {
                        context.DelayedEventMask |= PollMask.Readable;
                    }
                }
                else if (action == 6)
                {
                    // Fudge with the event mask.
                    context.PollHandle.Start(PollMask.Writable, this.OnPollConnection);
                    context.PollHandle.Start(PollMask.Readable, this.OnPollConnection);
                    context.EventMask = PollMask.Readable;
                }
            }

            if ((pollMask & PollMask.Writable) == PollMask.Writable &&
                !this.deplux && context.IsServerConnection)
            {
                // We have to send more bytes.
                int action = random.Next() % 7;

                if (action == 0 ||
                    action == 1)
                {
                    // Send a couple of bytes.
                    var buffer = new byte[103];

                    int send  = Math.Min(TransferBytes - context.Sent, buffer.Length);
                    int count = TrySend(context.Socket, buffer, send);
                    if (count < 0)
                    {
                        this.spuriousWritableWakeups++;
                    }
                    else
                    {
                        context.Sent += count;
                        this.validWritableWakeups++;
                    }
                }
                else if (action == 2 ||
                         action == 3)
                {
                    // Send until EAGAIN.
                    var buffer = new byte[1234];
                    int send   = Math.Min(TransferBytes - context.Sent, buffer.Length);
                    int count  = TrySend(context.Socket, buffer, send);
                    if (count < 0)
                    {
                        this.spuriousWritableWakeups++;
                    }
                    else
                    {
                        context.Sent += count;
                        this.validWritableWakeups++;
                    }

                    while (context.Sent < TransferBytes)
                    {
                        send  = Math.Min(TransferBytes - context.Sent, buffer.Length);
                        count = TrySend(context.Socket, buffer, send);
                        if (count < 0)
                        {
                            break;
                        }
                        else
                        {
                            context.Sent += count;
                        }
                    }
                }
                else if (action == 4)
                {
                    // Ignore.
                }
                else if (action == 5)
                {
                    // Stop sending for a while. Restart in timer callback.
                    newEvents &= ~PollMask.Writable;
                    if (!context.TimerHandle.IsActive)
                    {
                        context.DelayedEventMask = PollMask.Writable;
                        context.TimerHandle.Start(this.OnTimerDelay, 100, 0);
                    }
                    else
                    {
                        context.DelayedEventMask |= PollMask.Writable;
                    }
                }
                else if (action == 6)
                {
                    // Fudge with the event mask.
                    context.PollHandle.Start(PollMask.Readable, this.OnPollConnection);
                    context.PollHandle.Start(PollMask.Writable, this.OnPollConnection);
                    context.EventMask = PollMask.Writable;
                }
            }
            else
            {
                // Nothing more to write. Send FIN.
                context.Socket.Shutdown(SocketShutdown.Send);
                context.SentFinished = true;
                newEvents           &= ~PollMask.Writable;
            }

            if ((pollMask & PollMask.Disconnect) == PollMask.Disconnect)
            {
                context.Disconnected = true;
                newEvents           &= ~PollMask.Disconnect;
            }

            if (context.SentFinished ||
                context.ReceiveFinished ||
                context.Disconnected)
            {
                if (context.SentFinished ||
                    context.ReceiveFinished)
                {
                    this.DestroyConnectionContext(context);
                }
                else
                {
                    /* Poll mask changed. Call uv_poll_start again. */
                    context.EventMask = newEvents;
                    context.PollHandle.Start(newEvents, this.OnPollConnection);
                }
            }
        }