Build para Windows: make exe (MinGW + pacotes do MSYS2)
make exe gera build/win-x86_64/TinyBlocks/TinyBlocks.exe com as DLLs e o zip; make run-exe abre no Wine. As dependencias (SDL2, SDL2_ttf, libpng, freetype, openssl, zlib, regex) vem dos pacotes MinGW do MSYS2, com as dependencias resolvidas pelo indice do repositorio (packaging/windows/msys2_deps.py). O POSIX que falta fica em packaging/windows/compat (dlfcn, pwd, uname, mkdir, sysconf, setenv, readlink de /proc/self/exe, strndup, getline, open_memstream). No Windows uma DLL nao ve os simbolos do .exe, entao o editor, os modulos dele (TB_ED_STATIC_MODULES) e as libraries de extensao (TB_STATIC_EXTRAS) entram no executavel; TB_STATIC_BUILD deixa o TB_API vazio. Socket e servidor MCP ganham uma camada sobre o Winsock; o kernel do GEMM so usa target_clones no ELF. O pacote leva a fonte DejaVu (procurada ao lado do executavel pelo SDL2 e pela opentype) e os certificados raiz (certs/ca-bundle.crt, carregados pela ssl). Testado no Wine 9: interface, HTTPS com verificacao, tensor, informacoes do sistema e grafico em PNG. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
829bc67142
commit
7538d48a13
21 files changed
+820
-61
No files matched your search
@@ -5,44 +5,91 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#define _DEFAULT_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <poll.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
/* -- Portabilidade: sockets POSIX ou Winsock ------------------------------ */
|
||||
#ifdef _WIN32
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# include <winsock2.h>
|
||||
# include <ws2tcpip.h>
|
||||
# define MSG_NOSIGNAL 0
|
||||
static int sock_errno(void) { return WSAGetLastError(); }
|
||||
static bool sock_retry(int e) { return e == WSAEINTR; }
|
||||
static bool sock_timeout(int e) { return e == WSAETIMEDOUT || e == WSAEWOULDBLOCK; }
|
||||
static bool sock_pending(int e) { return e == WSAEWOULDBLOCK || e == WSAEINPROGRESS; }
|
||||
static void sock_close(int fd) { closesocket((SOCKET)fd); }
|
||||
static void sock_nonblock(int fd, bool on) { u_long m = on; ioctlsocket((SOCKET)fd, FIONBIO, &m); }
|
||||
static int sock_poll(struct pollfd* p, int n, int ms) { return WSAPoll(p, (ULONG)n, ms); }
|
||||
static void sock_timeouts(int fd, int ms) {
|
||||
DWORD t = (DWORD)ms;
|
||||
setsockopt((SOCKET)fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&t, sizeof(t));
|
||||
setsockopt((SOCKET)fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&t, sizeof(t));
|
||||
}
|
||||
static const char* sock_strerror(int e) {
|
||||
static char m[160];
|
||||
if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD)e, 0, m, sizeof(m), NULL))
|
||||
snprintf(m, sizeof(m), "erro de rede %d", e);
|
||||
for (char* c = m + strlen(m); c > m && (c[-1] == '\n' || c[-1] == '\r' || c[-1] == '.'); ) *--c = 0;
|
||||
return m;
|
||||
}
|
||||
#else
|
||||
# include <fcntl.h>
|
||||
# include <netdb.h>
|
||||
# include <poll.h>
|
||||
# include <unistd.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <netinet/in.h>
|
||||
# include <netinet/tcp.h>
|
||||
# include <sys/socket.h>
|
||||
# include <sys/time.h>
|
||||
static int sock_errno(void) { return errno; }
|
||||
static bool sock_retry(int e) { return e == EINTR; }
|
||||
static bool sock_timeout(int e) { return e == EAGAIN || e == EWOULDBLOCK; }
|
||||
static bool sock_pending(int e) { return e == EINPROGRESS; }
|
||||
static void sock_close(int fd) { close(fd); }
|
||||
static void sock_nonblock(int fd, bool on) {
|
||||
int fl = fcntl(fd, F_GETFL, 0);
|
||||
fcntl(fd, F_SETFL, on ? fl | O_NONBLOCK : fl & ~O_NONBLOCK);
|
||||
}
|
||||
static int sock_poll(struct pollfd* p, int n, int ms) { return poll(p, (nfds_t)n, ms); }
|
||||
static void sock_timeouts(int fd, int ms) {
|
||||
struct timeval tv = { ms / 1000, (ms % 1000) * 1000 };
|
||||
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
}
|
||||
static const char* sock_strerror(int e) { return strerror(e); }
|
||||
#endif
|
||||
|
||||
#include "tb_socket.h"
|
||||
|
||||
/* == TCP como TbStream ================================================ */
|
||||
static long tcp_read(TbStream* s, void* buf, size_t cap) {
|
||||
for (;;) {
|
||||
ssize_t n = recv(s->fd, buf, cap, 0);
|
||||
if (n >= 0) return (long)n;
|
||||
if (errno == EINTR) continue;
|
||||
snprintf(s->err, sizeof(s->err), "%s", errno == EAGAIN || errno == EWOULDBLOCK ? "tempo esgotado esperando dados" : strerror(errno));
|
||||
long n = (long)recv(s->fd, buf, (int)cap, 0);
|
||||
if (n >= 0) return n;
|
||||
int e = sock_errno();
|
||||
if (sock_retry(e)) continue;
|
||||
snprintf(s->err, sizeof(s->err), "%s", sock_timeout(e) ? "tempo esgotado esperando dados" : sock_strerror(e));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static long tcp_write(TbStream* s, const void* buf, size_t n) {
|
||||
for (;;) {
|
||||
ssize_t w = send(s->fd, buf, n, MSG_NOSIGNAL);
|
||||
if (w >= 0) return (long)w;
|
||||
if (errno == EINTR) continue;
|
||||
snprintf(s->err, sizeof(s->err), "%s", strerror(errno));
|
||||
long w = (long)send(s->fd, buf, (int)n, MSG_NOSIGNAL);
|
||||
if (w >= 0) return w;
|
||||
int e = sock_errno();
|
||||
if (sock_retry(e)) continue;
|
||||
snprintf(s->err, sizeof(s->err), "%s", sock_strerror(e));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static void tcp_close(TbStream* s) {
|
||||
if (s->fd >= 0) close(s->fd);
|
||||
if (s->fd >= 0) sock_close(s->fd);
|
||||
free(s);
|
||||
}
|
||||
|
||||
@@ -61,31 +108,29 @@ static TbStream* tcp_connect(const char* host, int port, int timeout_ms, char* e
|
||||
int fd = -1;
|
||||
char last[128] = "nenhum endereço respondeu";
|
||||
for (struct addrinfo* a = res; a; a = a->ai_next) {
|
||||
fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
|
||||
fd = (int)socket(a->ai_family, a->ai_socktype, a->ai_protocol);
|
||||
if (fd < 0) continue;
|
||||
int fl = fcntl(fd, F_GETFL, 0);
|
||||
fcntl(fd, F_SETFL, fl | O_NONBLOCK); /* connect com prazo */
|
||||
int c = connect(fd, a->ai_addr, a->ai_addrlen);
|
||||
if (c < 0 && errno == EINPROGRESS) {
|
||||
sock_nonblock(fd, true); /* connect com prazo */
|
||||
int c = connect(fd, a->ai_addr, (int)a->ai_addrlen);
|
||||
int ce = c < 0 ? sock_errno() : 0;
|
||||
if (c < 0 && sock_pending(ce)) {
|
||||
struct pollfd pf = { fd, POLLOUT, 0 };
|
||||
int pr = poll(&pf, 1, timeout_ms);
|
||||
int pr = sock_poll(&pf, 1, timeout_ms);
|
||||
int soerr = 0; socklen_t sl = sizeof(soerr);
|
||||
if (pr == 1) getsockopt(fd, SOL_SOCKET, SO_ERROR, &soerr, &sl);
|
||||
if (pr == 1) getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&soerr, &sl);
|
||||
c = (pr == 1 && soerr == 0) ? 0 : -1;
|
||||
if (c) snprintf(last, sizeof(last), "%s", pr == 0 ? "tempo esgotado conectando" : strerror(soerr ? soerr : errno));
|
||||
} else if (c < 0) snprintf(last, sizeof(last), "%s", strerror(errno));
|
||||
if (c == 0) { fcntl(fd, F_SETFL, fl); break; }
|
||||
close(fd); fd = -1;
|
||||
if (c) snprintf(last, sizeof(last), "%s", pr == 0 ? "tempo esgotado conectando" : sock_strerror(soerr ? soerr : sock_errno()));
|
||||
} else if (c < 0) snprintf(last, sizeof(last), "%s", sock_strerror(ce));
|
||||
if (c == 0) { sock_nonblock(fd, false); break; }
|
||||
sock_close(fd); fd = -1;
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
if (fd < 0) { snprintf(err, cap, "não conectou a %s:%d: %s", host, port, last); return NULL; }
|
||||
struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 };
|
||||
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
sock_timeouts(fd, timeout_ms);
|
||||
int one = 1;
|
||||
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
||||
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const char*)&one, sizeof(one));
|
||||
TbStream* s = calloc(1, sizeof(TbStream));
|
||||
if (!s) { close(fd); snprintf(err, cap, "sem memória"); return NULL; }
|
||||
if (!s) { sock_close(fd); snprintf(err, cap, "sem memória"); return NULL; }
|
||||
s->cls = &s_tcp;
|
||||
s->fd = fd;
|
||||
return s;
|
||||
@@ -246,6 +291,10 @@ static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
static bool socket_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = { "Socket", {0.20f, 0.60f, 0.62f, 1.f}, {0.13f, 0.45f, 0.47f, 1.f} };
|
||||
tb_library_set_category(lib, &cat);
|
||||
#ifdef _WIN32
|
||||
WSADATA wsa; /* o Winsock precisa ser iniciado */
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) return false;
|
||||
#endif
|
||||
lib->lib_userdata = (void*)&s_api;
|
||||
s_t_conn = tb_type_block_create(rt, "Socket._Conexao", NULL, 0, NULL, 0, NULL);
|
||||
if (!s_t_conn) return false;
|
||||
@@ -278,6 +327,9 @@ static bool socket_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static void socket_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt;
|
||||
lib->lib_userdata = NULL;
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(socket, "tinyblocks-socket", "1.0.0", socket_init, socket_deinit)
|
||||
|
||||
Reference in new issue
Block a user