|
| 1 | +#include <string.h> |
| 2 | +#include <signal.h> |
| 3 | +#include <time.h> |
| 4 | +#include <errno.h> |
| 5 | +#include <unistd.h> |
| 6 | +#ifndef _WIN32 |
| 7 | + #include <sys/select.h> |
| 8 | + #define MM_API __attribute__((visibility ("default"))) |
| 9 | +#else |
| 10 | + #define MM_API __attribute__((dllexport)) |
| 11 | +#endif |
| 12 | + |
| 13 | +#define BACKEND_NAME "core" |
| 14 | +#include "midimonster.h" |
| 15 | +#include "core.h" |
| 16 | +#include "backend.h" |
| 17 | +#include "routing.h" |
| 18 | +#include "plugin.h" |
| 19 | +#include "config.h" |
| 20 | + |
| 21 | +static size_t fds = 0; |
| 22 | +static int max_fd = -1; |
| 23 | +static managed_fd* fd = NULL; |
| 24 | +static managed_fd* signaled_fds = NULL; |
| 25 | +static volatile sig_atomic_t fd_set_dirty = 1; |
| 26 | +static uint64_t global_timestamp = 0; |
| 27 | + |
| 28 | +static fd_set all_fds; |
| 29 | + |
| 30 | +MM_API uint64_t mm_timestamp(){ |
| 31 | + return global_timestamp; |
| 32 | +} |
| 33 | + |
| 34 | +static void core_timestamp(){ |
| 35 | + #ifdef _WIN32 |
| 36 | + global_timestamp = GetTickCount(); |
| 37 | + #else |
| 38 | + struct timespec current; |
| 39 | + if(clock_gettime(CLOCK_MONOTONIC_COARSE, ¤t)){ |
| 40 | + fprintf(stderr, "Failed to update global timestamp, time-based processing for some backends may be impaired: %s\n", strerror(errno)); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + global_timestamp = current.tv_sec * 1000 + current.tv_nsec / 1000000; |
| 45 | + #endif |
| 46 | +} |
| 47 | + |
| 48 | +static fd_set core_collect(int* max_fd){ |
| 49 | + size_t u = 0; |
| 50 | + fd_set rv_fds; |
| 51 | + |
| 52 | + if(max_fd){ |
| 53 | + *max_fd = -1; |
| 54 | + } |
| 55 | + |
| 56 | + DBGPF("Building selector set from %" PRIsize_t " FDs registered to core", fds); |
| 57 | + FD_ZERO(&rv_fds); |
| 58 | + for(u = 0; u < fds; u++){ |
| 59 | + if(fd[u].fd >= 0){ |
| 60 | + FD_SET(fd[u].fd, &rv_fds); |
| 61 | + if(max_fd){ |
| 62 | + *max_fd = max(*max_fd, fd[u].fd); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return rv_fds; |
| 68 | +} |
| 69 | + |
| 70 | +MM_API int mm_manage_fd(int new_fd, char* back, int manage, void* impl){ |
| 71 | + backend* b = backend_match(back); |
| 72 | + size_t u; |
| 73 | + |
| 74 | + if(!b){ |
| 75 | + fprintf(stderr, "Unknown backend %s registered for managed fd\n", back); |
| 76 | + return 1; |
| 77 | + } |
| 78 | + |
| 79 | + //find exact match |
| 80 | + for(u = 0; u < fds; u++){ |
| 81 | + if(fd[u].fd == new_fd && fd[u].backend == b){ |
| 82 | + fd[u].impl = impl; |
| 83 | + if(!manage){ |
| 84 | + fd[u].fd = -1; |
| 85 | + fd[u].backend = NULL; |
| 86 | + fd[u].impl = NULL; |
| 87 | + fd_set_dirty = 1; |
| 88 | + } |
| 89 | + return 0; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if(!manage){ |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + //find free slot |
| 98 | + for(u = 0; u < fds; u++){ |
| 99 | + if(fd[u].fd < 0){ |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + //if necessary expand |
| 104 | + if(u == fds){ |
| 105 | + fd = realloc(fd, (fds + 1) * sizeof(managed_fd)); |
| 106 | + if(!fd){ |
| 107 | + fprintf(stderr, "Failed to allocate memory\n"); |
| 108 | + return 1; |
| 109 | + } |
| 110 | + |
| 111 | + signaled_fds = realloc(signaled_fds, (fds + 1) * sizeof(managed_fd)); |
| 112 | + if(!signaled_fds){ |
| 113 | + fprintf(stderr, "Failed to allocate memory\n"); |
| 114 | + return 1; |
| 115 | + } |
| 116 | + fds++; |
| 117 | + } |
| 118 | + |
| 119 | + //store new fd |
| 120 | + fd[u].fd = new_fd; |
| 121 | + fd[u].backend = b; |
| 122 | + fd[u].impl = impl; |
| 123 | + fd_set_dirty = 1; |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +int core_initialize(){ |
| 128 | + FD_ZERO(&all_fds); |
| 129 | + |
| 130 | + //load initial timestamp |
| 131 | + core_timestamp(); |
| 132 | + |
| 133 | + #ifdef _WIN32 |
| 134 | + WSADATA wsa; |
| 135 | + WORD version = MAKEWORD(2, 2); |
| 136 | + if(WSAStartup(version, &wsa)){ |
| 137 | + return 1; |
| 138 | + } |
| 139 | + _fmode = _O_BINARY; |
| 140 | + #endif |
| 141 | + |
| 142 | + //attach plugins |
| 143 | + if(plugins_load(PLUGINS)){ |
| 144 | + LOG("Failed to initialize a backend"); |
| 145 | + return 1; |
| 146 | + } |
| 147 | + |
| 148 | + return 0; |
| 149 | +} |
| 150 | + |
| 151 | +int core_start(){ |
| 152 | + if(backends_start()){ |
| 153 | + return 1; |
| 154 | + } |
| 155 | + |
| 156 | + routing_stats(); |
| 157 | + |
| 158 | + if(!fds){ |
| 159 | + LOG("No descriptors registered for multiplexing"); |
| 160 | + } |
| 161 | + |
| 162 | + return 0; |
| 163 | +} |
| 164 | + |
| 165 | +int core_iteration(){ |
| 166 | + fd_set read_fds; |
| 167 | + struct timeval tv; |
| 168 | + int error; |
| 169 | + size_t n, u; |
| 170 | + #ifdef _WIN32 |
| 171 | + char* error_message = NULL; |
| 172 | + #else |
| 173 | + struct timespec ts; |
| 174 | + #endif |
| 175 | + |
| 176 | + //rebuild fd set if necessary |
| 177 | + if(fd_set_dirty || !signaled_fds){ |
| 178 | + all_fds = core_collect(&max_fd); |
| 179 | + fd_set_dirty = 0; |
| 180 | + } |
| 181 | + |
| 182 | + //wait for & translate events |
| 183 | + read_fds = all_fds; |
| 184 | + tv = backend_timeout(); |
| 185 | + |
| 186 | + //check whether there are any fds active, windows does not like select() without descriptors |
| 187 | + if(max_fd >= 0){ |
| 188 | + error = select(max_fd + 1, &read_fds, NULL, NULL, &tv); |
| 189 | + if(error < 0){ |
| 190 | + #ifndef _WIN32 |
| 191 | + fprintf(stderr, "select failed: %s\n", strerror(errno)); |
| 192 | + #else |
| 193 | + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 194 | + NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &error_message, 0, NULL); |
| 195 | + fprintf(stderr, "select failed: %s\n", error_message); |
| 196 | + LocalFree(error_message); |
| 197 | + error_message = NULL; |
| 198 | + #endif |
| 199 | + return 1; |
| 200 | + } |
| 201 | + } |
| 202 | + else{ |
| 203 | + DBGPF("No descriptors, sleeping for %zu msec", tv.tv_sec * 1000 + tv.tv_usec / 1000); |
| 204 | + #ifdef _WIN32 |
| 205 | + Sleep(tv.tv_sec * 1000 + tv.tv_usec / 1000); |
| 206 | + #else |
| 207 | + ts.tv_sec = tv.tv_sec; |
| 208 | + ts.tv_nsec = tv.tv_usec * 1000; |
| 209 | + nanosleep(&ts, NULL); |
| 210 | + #endif |
| 211 | + } |
| 212 | + |
| 213 | + //update this iteration's timestamp |
| 214 | + core_timestamp(); |
| 215 | + |
| 216 | + //find all signaled fds |
| 217 | + n = 0; |
| 218 | + for(u = 0; u < fds; u++){ |
| 219 | + if(fd[u].fd >= 0 && FD_ISSET(fd[u].fd, &read_fds)){ |
| 220 | + signaled_fds[n] = fd[u]; |
| 221 | + n++; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + //run backend processing to collect events |
| 226 | + DBGPF("%" PRIsize_t " backend FDs signaled", nfds); |
| 227 | + if(backends_handle(n, signaled_fds)){ |
| 228 | + return 1; |
| 229 | + } |
| 230 | + |
| 231 | + //route generated events |
| 232 | + return routing_iteration(); |
| 233 | +} |
| 234 | + |
| 235 | +static void fds_free(){ |
| 236 | + size_t u; |
| 237 | + for(u = 0; u < fds; u++){ |
| 238 | + if(fd[u].fd >= 0){ |
| 239 | + close(fd[u].fd); |
| 240 | + fd[u].fd = -1; |
| 241 | + } |
| 242 | + } |
| 243 | + free(fd); |
| 244 | + fds = 0; |
| 245 | + fd = NULL; |
| 246 | +} |
| 247 | + |
| 248 | +void core_shutdown(){ |
| 249 | + backends_stop(); |
| 250 | + routing_cleanup(); |
| 251 | + free(signaled_fds); |
| 252 | + signaled_fds = NULL; |
| 253 | + fds_free(); |
| 254 | + plugins_close(); |
| 255 | + config_free(); |
| 256 | + fd_set_dirty = 1; |
| 257 | +} |
0 commit comments