juegos

Juegos de terminal de Pancho: Catan (TUI, GUI, web, servidor, PicoCalc), ajedrez, calculadora y minijuegos
git clone https://git.lu3dhn.xyz/juegos.git
Log | Files | Refs

plat.h (2285B)


      1 /* plat.h - lo poco que cambia entre Linux y Windows (MinGW): sockets, mkdir,
      2  * permisos. En Linux es POSIX tal cual; en Windows, Winsock con WSAPoll.
      3  * Los sockets se guardan como int en los dos (en Windows entran de sobra). */
      4 #ifndef COMUN_PLAT_H
      5 #define COMUN_PLAT_H
      6 
      7 #ifdef _WIN32
      8 #ifndef _WIN32_WINNT
      9 #define _WIN32_WINNT 0x0600          /* WSAPoll */
     10 #endif
     11 #include <winsock2.h>
     12 #include <ws2tcpip.h>
     13 #include <direct.h>
     14 #include <io.h>
     15 #include <stdio.h>
     16 #undef RGB                           /* la de wingdi choca con la de gfx.h */
     17 
     18 typedef unsigned long nfds_t;
     19 #define poll(p, n, t)   WSAPoll((p), (ULONG)(n), (t))
     20 #define MSG_NOSIGNAL    0
     21 #define mkdir(p, mode)  _mkdir(p)
     22 #define fchmod(fd, m)   ((void)(fd))
     23 #define localtime_r(t, tm) (localtime_s((tm), (t)) ? NULL : (tm))
     24 #define sock_close(fd)  closesocket((SOCKET)(fd))
     25 #define SOCK_ETIMEDOUT  WSAETIMEDOUT
     26 
     27 /* WSAStartup una vez; lo llaman lc_dial y srv_open */
     28 static inline void sock_init(void)
     29 {
     30     static int done;
     31     if (!done) { WSADATA w; WSAStartup(MAKEWORD(2, 2), &w); done = 1; }
     32 }
     33 static inline void sock_nonblock(int fd, int on) { u_long v = (u_long)on; ioctlsocket((SOCKET)fd, FIONBIO, &v); }
     34 static inline int  sock_err(void) { return WSAGetLastError(); }
     35 static inline int  sock_pending(int e) { return e == WSAEWOULDBLOCK || e == WSAEINPROGRESS; }
     36 static inline const char *sock_strerror(int e)
     37 {
     38     static char buf[160];
     39     if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, (DWORD)e, 0, buf, sizeof buf, 0))
     40         snprintf(buf, sizeof buf, "error %d", e);
     41     for (char *p = buf; *p; p++) if (*p == '\r' || *p == '\n') *p = ' ';
     42     return buf;
     43 }
     44 #else
     45 #include <errno.h>
     46 #include <fcntl.h>
     47 #include <netdb.h>
     48 #include <poll.h>
     49 #include <string.h>
     50 #include <sys/socket.h>
     51 #include <unistd.h>
     52 
     53 #define sock_close(fd)  close(fd)
     54 #define SOCK_ETIMEDOUT  ETIMEDOUT
     55 static inline void sock_init(void) {}
     56 static inline void sock_nonblock(int fd, int on)
     57 {
     58     int fl = fcntl(fd, F_GETFL);
     59     fcntl(fd, F_SETFL, on ? fl | O_NONBLOCK : fl & ~O_NONBLOCK);
     60 }
     61 static inline int  sock_err(void) { return errno; }
     62 static inline int  sock_pending(int e) { return e == EINPROGRESS; }
     63 static inline const char *sock_strerror(int e) { return strerror(e); }
     64 #endif
     65 
     66 #endif