summaryrefslogtreecommitdiff
path: root/io_muxer.c
blob: 17b32a9ce5925aab511481007bceb3a2ff78ab65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2022 Mikhail Burakov. This file is part of toolbox.
 *
 * toolbox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * toolbox is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with toolbox.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "io_muxer.h"

#include <poll.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

struct IoMuxerTask {
  void (*fun)(void*);
  void* user;
};

static bool AppendRecord(struct IoMuxer* io_muxer) {
  if (io_muxer->size < io_muxer->alloc) return true;
  size_t alloc = io_muxer->alloc + 1;
  struct pollfd* pfds = realloc(io_muxer->pfds, alloc * sizeof(struct pollfd));
  if (!pfds) return false;
  io_muxer->pfds = pfds;
  struct IoMuxerTask* tasks =
      realloc(io_muxer->tasks, alloc * sizeof(struct IoMuxerTask));
  if (!tasks) return false;
  io_muxer->tasks = tasks;
  io_muxer->alloc = alloc;
  return true;
}

static void CompressRecords(struct IoMuxer* io_muxer) {
  for (size_t forward = 0, reverse = io_muxer->size - 1; io_muxer->size;) {
    while (forward < reverse && !io_muxer->pfds[forward].revents) forward++;
    while (forward < reverse && io_muxer->pfds[reverse].revents) reverse--;
    if (forward == reverse) {
      if (!io_muxer->pfds[forward].revents) forward++;
      io_muxer->size = forward;
      return;
    }
    struct pollfd pfd = io_muxer->pfds[forward];
    io_muxer->pfds[forward] = io_muxer->pfds[reverse];
    io_muxer->pfds[reverse] = pfd;
    struct IoMuxerTask task = io_muxer->tasks[forward];
    io_muxer->tasks[forward] = io_muxer->tasks[reverse];
    io_muxer->tasks[reverse] = task;
  }
}

void IoMuxerCreate(struct IoMuxer* io_muxer) {
  memset(io_muxer, 0, sizeof(struct IoMuxer));
}

bool IoMuxerOnRead(struct IoMuxer* io_muxer, int fd, void (*fun)(void*),
                   void* user) {
  if (!AppendRecord(io_muxer)) return false;
  struct pollfd pfd = {.fd = fd, .events = POLLIN};
  struct IoMuxerTask task = {.fun = fun, .user = user};
  io_muxer->pfds[io_muxer->size] = pfd;
  io_muxer->tasks[io_muxer->size] = task;
  io_muxer->size++;
  return true;
}

bool IoMuxerOnWrite(struct IoMuxer* io_muxer, int fd, void (*fun)(void*),
                    void* user) {
  if (!AppendRecord(io_muxer)) return false;
  struct pollfd pfd = {.fd = fd, .events = POLLOUT};
  struct IoMuxerTask task = {.fun = fun, .user = user};
  io_muxer->pfds[io_muxer->size] = pfd;
  io_muxer->tasks[io_muxer->size] = task;
  io_muxer->size++;
  return true;
}

enum IoMuxerResult IoMuxerIterate(struct IoMuxer* io_muxer, int timeout) {
  for (;;) {
    CompressRecords(io_muxer);
    int result = poll(io_muxer->pfds, io_muxer->size, timeout);
    switch (result) {
      case -1:
        return kIoMuxerResultError;
      case 0:
        return kIoMuxerResultTimeout;
      default:
        break;
    }
    for (size_t i = 0; i < io_muxer->size; i++) {
      if (io_muxer->pfds[i].revents)
        io_muxer->tasks[i].fun(io_muxer->tasks[i].user);
    }
  }
}

void IoMuxerDestroy(struct IoMuxer* io_muxer) {
  free(io_muxer->pfds);
  free(io_muxer->tasks);
}