Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SparklesChat
- *
- * Copyright (C) 2014-2015 NovaSquirrel
- *
- * This program 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 2 of the
- * License, or (at your option) any later version.
- *
- * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "chat.h"
- void IPC_New(IPC_Holder *IPC, int Size) {
- #ifdef _WIN32
- _pipe(IPC->Pipe, Size, 0);
- #else
- pipe(IPC->Pipe);
- #endif
- SDL_AtomicSet(&IPC->Ready, 0);
- }
- void IPC_Free(IPC_Holder *IPC) {
- close(IPC->Pipe[0]);
- close(IPC->Pipe[1]);
- }
- void IPC_Write(IPC_Holder *IPC, const char *Text) {
- char *Clone = StringClone(Text);
- write(IPC->Pipe[PIPE_WRITE], &Clone, sizeof(char *));
- SDL_AtomicAdd(&IPC->Ready, 1);
- }
- void IPC_WriteF(IPC_Holder *IPC, const char *format, ...) {
- va_list args;
- char *buf;
- va_start(args, format);
- buf = strdup_vprintf(format, args);
- va_end(args);
- IPC_Write(IPC, buf);
- free(buf);
- }
- char *IPC_Read(int Timeout, int Count, ...) {
- int i;
- IPC_Holder *IPC[Count];
- va_list ap;
- va_start(ap, Count);
- for(i=0; i<Count; i++)
- IPC[i] = va_arg(ap, IPC_Holder*);
- va_end(ap);
- // alternative strategy because select() doesn't work on pipes on Windows
- int Delay = Timeout / 10;
- for(int Tries = 0; Tries < 10; Tries++) {
- for(i=0; i<Count; i++)
- if(SDL_AtomicGet(&IPC[i]->Ready)) {
- char *String;
- read(IPC[i]->Pipe[PIPE_READ], &String, sizeof(char *));
- SDL_AtomicAdd(&IPC[i]->Ready, -1);
- return String;
- }
- SDL_Delay(Delay);
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement