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
@@ -10,6 +10,10 @@
|
||||
* O estado da interface vive em Ed/Tab (no editor), entao trocar o codigo
|
||||
* nao perde nada. As chamadas passam por tabelas de ponteiros (tb_ed_w,
|
||||
* tb_ed_img), por isso a troca vale imediatamente para o editor inteiro.
|
||||
*
|
||||
* Com TB_ED_STATIC_MODULES (build Windows) os modulos vem linkados no
|
||||
* proprio executavel: as tabelas apontam direto para as funcoes e nao ha
|
||||
* hot reload.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
@@ -19,6 +23,11 @@
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <tb_editor_internal.h>
|
||||
#ifdef TB_ED_STATIC_MODULES
|
||||
# include <tb_editor_widgets.h>
|
||||
# include <tb_editor_image.h>
|
||||
# include <tb_editor_files.h>
|
||||
#endif
|
||||
|
||||
/* Este arquivo nomeia os campos das tabelas (w.fill_rect, ...), entao os
|
||||
apelidos curtos do editor atrapalham: remove-os aqui. */
|
||||
@@ -159,6 +168,40 @@ static bool resolve_files(void) {
|
||||
|
||||
/* -- carga --- */
|
||||
|
||||
#ifdef TB_ED_STATIC_MODULES
|
||||
bool tb_ed_modules_load(Ed* ed) {
|
||||
tb_ed_w = (TbEdWidgetsApi){
|
||||
.col_mul = tb_ew_col_mul, .fill_rect = tb_ew_fill_rect, .fill_rrect = tb_ew_fill_rrect,
|
||||
.fill_circle = tb_ew_fill_circle, .stroke_rect = tb_ew_stroke_rect,
|
||||
.stroke_circle = tb_ew_stroke_circle, .line = tb_ew_line, .text_c = tb_ew_text_c,
|
||||
.text_width = tb_ew_text_width, .in_rect = tb_ew_in_rect };
|
||||
tb_ed_img = (TbEdImageApi){
|
||||
.view = tb_editor_image_view, .click = tb_editor_image_click, .drag = tb_editor_image_drag,
|
||||
.release = tb_editor_image_release, .load_file = tb_editor_image_load_file,
|
||||
.save = tb_editor_image_save, .ensure = tb_editor_image_ensure,
|
||||
.sel_reset = tb_editor_image_sel_reset, .sel_commit = tb_editor_image_sel_commit,
|
||||
.zoom = tb_editor_image_zoom };
|
||||
tb_ed_files = (TbEdFilesApi){
|
||||
.open = tb_editor_files_open, .close = tb_editor_files_close, .view = tb_editor_files_view,
|
||||
.click = tb_editor_files_click, .key = tb_editor_files_key, .text = tb_editor_files_text,
|
||||
.scroll = tb_editor_files_scroll };
|
||||
ed->mods.n = 0;
|
||||
for (int i = 0; i < s_mod_n; i++) {
|
||||
ed->mods.list[ed->mods.n].name = s_mod_name[i];
|
||||
ed->mods.list[ed->mods.n].file = s_mod_file[i];
|
||||
ed->mods.list[ed->mods.n].ok = 1;
|
||||
ed->mods.n++;
|
||||
}
|
||||
ed->mods.loaded = true;
|
||||
snprintf(ed->mods.info, sizeof(ed->mods.info), "Bundle do editor embutido no executável");
|
||||
return true;
|
||||
}
|
||||
|
||||
void tb_ed_modules_clear(void) {}
|
||||
|
||||
bool tb_ed_modules_check(Ed* ed) { (void)ed; return false; }
|
||||
|
||||
#else
|
||||
bool tb_ed_modules_load(Ed* ed) {
|
||||
if (!tb_api_runtime()) tb_api_init(ed->rt);
|
||||
tb_ed_modules_clear();
|
||||
@@ -228,3 +271,4 @@ bool tb_ed_modules_check(Ed* ed) {
|
||||
ed->hot_status_ok = ok;
|
||||
return true;
|
||||
}
|
||||
#endif /* TB_ED_STATIC_MODULES */
|
||||
@@ -6,15 +6,38 @@
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
/* -- Portabilidade: sockets POSIX ou Winsock --- */
|
||||
#ifdef _WIN32
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# include <winsock2.h>
|
||||
# include <ws2tcpip.h>
|
||||
# define MSG_NOSIGNAL 0
|
||||
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 bool sock_retry(void) { return WSAGetLastError() == WSAEINTR; }
|
||||
static bool sock_later(void) { int e = WSAGetLastError(); return e == WSAEWOULDBLOCK || e == WSAEINTR; }
|
||||
static bool sock_start(void) { WSADATA w; return WSAStartup(MAKEWORD(2, 2), &w) == 0; }
|
||||
#else
|
||||
# include <fcntl.h>
|
||||
# include <unistd.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <netinet/in.h>
|
||||
# include <sys/socket.h>
|
||||
static void sock_close(int fd) { close(fd); }
|
||||
static void sock_nonblock(int fd, bool on) {
|
||||
int fl = fcntl(fd, F_GETFL, 0);
|
||||
if (fl >= 0) fcntl(fd, F_SETFL, on ? fl | O_NONBLOCK : fl & ~O_NONBLOCK);
|
||||
}
|
||||
static bool sock_retry(void) { return errno == EINTR; }
|
||||
static bool sock_later(void) { return errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR; }
|
||||
static bool sock_start(void) { return true; }
|
||||
#endif
|
||||
|
||||
#include "tb_mcp_server_internal.h"
|
||||
|
||||
@@ -27,24 +50,22 @@ static unsigned now_ms(void) {
|
||||
return (unsigned)(ts.tv_sec * 1000u + (unsigned)(ts.tv_nsec / 1000000));
|
||||
}
|
||||
|
||||
static void set_nonblock(int fd) {
|
||||
int fl = fcntl(fd, F_GETFL, 0);
|
||||
if (fl >= 0) fcntl(fd, F_SETFL, fl | O_NONBLOCK);
|
||||
}
|
||||
static void set_nonblock(int fd) { sock_nonblock(fd, true); }
|
||||
|
||||
bool tb_mcp_server_listen(TbMcpServer* s, int port) {
|
||||
if (!s) return false;
|
||||
tb_mcp_http_close(s);
|
||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (!sock_start()) return false;
|
||||
int fd = (int)socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0) return false;
|
||||
int one = 1;
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&one, sizeof(one));
|
||||
struct sockaddr_in a;
|
||||
memset(&a, 0, sizeof(a));
|
||||
a.sin_family = AF_INET;
|
||||
a.sin_port = htons((unsigned short)port);
|
||||
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* so a propria maquina */
|
||||
if (bind(fd, (struct sockaddr*)&a, sizeof(a)) != 0 || listen(fd, 16) != 0) { close(fd); return false; }
|
||||
if (bind(fd, (struct sockaddr*)&a, sizeof(a)) != 0 || listen(fd, 16) != 0) { sock_close(fd); return false; }
|
||||
socklen_t al = sizeof(a);
|
||||
getsockname(fd, (struct sockaddr*)&a, &al);
|
||||
set_nonblock(fd);
|
||||
@@ -56,14 +77,14 @@ bool tb_mcp_server_listen(TbMcpServer* s, int port) {
|
||||
int tb_mcp_server_port(const TbMcpServer* s) { return s && s->listen_fd >= 0 ? s->port : 0; }
|
||||
|
||||
static void conn_drop(TbMcpServer* s, int i) {
|
||||
close(s->conns[i].fd);
|
||||
sock_close(s->conns[i].fd);
|
||||
free(s->conns[i].buf);
|
||||
s->conns[i] = s->conns[--s->conn_n];
|
||||
}
|
||||
|
||||
void tb_mcp_http_close(TbMcpServer* s) {
|
||||
while (s->conn_n > 0) conn_drop(s, 0);
|
||||
if (s->listen_fd >= 0) close(s->listen_fd);
|
||||
if (s->listen_fd >= 0) sock_close(s->listen_fd);
|
||||
s->listen_fd = -1;
|
||||
s->port = 0;
|
||||
}
|
||||
@@ -92,11 +113,10 @@ static bool header(const char* head, size_t head_len, const char* name, char* ou
|
||||
}
|
||||
|
||||
static void send_all(int fd, const char* data, size_t n) {
|
||||
int fl = fcntl(fd, F_GETFL, 0); /* a resposta sai inteira */
|
||||
if (fl >= 0) fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
|
||||
sock_nonblock(fd, false); /* a resposta sai inteira */
|
||||
while (n > 0) {
|
||||
ssize_t w = send(fd, data, n, MSG_NOSIGNAL);
|
||||
if (w <= 0) { if (w < 0 && errno == EINTR) continue; return; }
|
||||
long w = (long)send(fd, data, (int)n, MSG_NOSIGNAL);
|
||||
if (w <= 0) { if (w < 0 && sock_retry()) continue; return; }
|
||||
data += w;
|
||||
n -= (size_t)w;
|
||||
}
|
||||
@@ -180,9 +200,9 @@ static void serve(TbMcpServer* s, TbMcpConn* c, size_t head_len, size_t body_len
|
||||
int tb_mcp_server_poll(TbMcpServer* s) {
|
||||
if (!s || s->listen_fd < 0) return 0;
|
||||
for (;;) { /* conexoes novas */
|
||||
int fd = accept(s->listen_fd, NULL, NULL);
|
||||
int fd = (int)accept(s->listen_fd, NULL, NULL);
|
||||
if (fd < 0) break;
|
||||
if (s->conn_n >= TB_MCP_MAX_CONNS) { close(fd); continue; }
|
||||
if (s->conn_n >= TB_MCP_MAX_CONNS) { sock_close(fd); continue; }
|
||||
set_nonblock(fd);
|
||||
TbMcpConn* c = &s->conns[s->conn_n++];
|
||||
memset(c, 0, sizeof(*c));
|
||||
@@ -202,10 +222,10 @@ int tb_mcp_server_poll(TbMcpServer* s) {
|
||||
c->buf = nb;
|
||||
c->cap = cap;
|
||||
}
|
||||
ssize_t r = recv(c->fd, c->buf + c->len, c->cap - c->len - 1, 0);
|
||||
long r = (long)recv(c->fd, c->buf + c->len, (int)(c->cap - c->len - 1), 0);
|
||||
if (r > 0) { c->len += (size_t)r; continue; }
|
||||
if (r == 0) closed = true;
|
||||
else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) closed = true;
|
||||
else if (!sock_later()) closed = true;
|
||||
break;
|
||||
}
|
||||
size_t hl = 0, bl = 0;
|
||||
|
||||
@@ -288,8 +288,20 @@ const char* tb_opentype_default_font(void) {
|
||||
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
||||
"/usr/share/fonts/truetype/freefont/FreeSans.ttf",
|
||||
"/usr/share/fonts/TTF/DejaVuSans.ttf",
|
||||
"C:/Windows/Fonts/arial.ttf",
|
||||
NULL
|
||||
};
|
||||
/* primeiro a que vai junto do programa: <pasta do executavel>/fonts */
|
||||
static char bundled[1024];
|
||||
char exe[900];
|
||||
ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
|
||||
if (n > 0) {
|
||||
exe[n] = 0;
|
||||
char* sl = strrchr(exe, '/');
|
||||
if (sl) *sl = 0;
|
||||
snprintf(bundled, sizeof(bundled), "%s/fonts/DejaVuSans.ttf", exe);
|
||||
if (access(bundled, R_OK) == 0) return bundled;
|
||||
}
|
||||
for (int i = 0; cands[i]; i++) if (access(cands[i], R_OK) == 0) return cands[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ static void eval_handle_noop(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
* Fonte TTF - cache por tamanho
|
||||
* === */
|
||||
#define FONT_CACHE_CAP 8
|
||||
#define FONT_SEARCH_COUNT 6
|
||||
#define FONT_SEARCH_COUNT 9
|
||||
|
||||
typedef struct { int size; TTF_Font* font; } FontEntry;
|
||||
|
||||
@@ -62,13 +62,27 @@ static const char* s_font_paths[FONT_SEARCH_COUNT] = {
|
||||
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
||||
"/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf",
|
||||
"/System/Library/Fonts/Helvetica.ttc",
|
||||
"C:/Windows/Fonts/DejaVuSans.ttf",
|
||||
"C:/Windows/Fonts/segoeui.ttf",
|
||||
"C:/Windows/Fonts/arial.ttf",
|
||||
};
|
||||
|
||||
/* A fonte que vai junto do programa (pacote Windows, AppImage):
|
||||
<pasta do executavel>/fonts/DejaVuSans.ttf */
|
||||
static TTF_Font* bundled_font(int size) {
|
||||
char* base = SDL_GetBasePath();
|
||||
if (!base) return NULL;
|
||||
char path[1024];
|
||||
snprintf(path, sizeof(path), "%sfonts/DejaVuSans.ttf", base);
|
||||
SDL_free(base);
|
||||
return TTF_OpenFont(path, size);
|
||||
}
|
||||
|
||||
static TTF_Font* get_font(int size) {
|
||||
for (int i = 0; i < s_font_cache_n; i++)
|
||||
if (s_font_cache[i].size == size) return s_font_cache[i].font;
|
||||
|
||||
TTF_Font* f = NULL;
|
||||
TTF_Font* f = bundled_font(size);
|
||||
for (int i = 0; i < FONT_SEARCH_COUNT && !f; i++)
|
||||
f = TTF_OpenFont(s_font_paths[i], size);
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
@@ -89,6 +90,23 @@ static void eval_version(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* Certificados que vao junto do programa: <pasta do executavel>/certs/
|
||||
ca-bundle.crt. No Windows o OpenSSL nao conhece o repositorio do sistema,
|
||||
entao o pacote leva o arquivo (ver packaging/windows). */
|
||||
static void load_bundled_certs(SSL_CTX* ctx) {
|
||||
char exe[1024];
|
||||
ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
|
||||
if (n <= 0) return;
|
||||
exe[n] = 0;
|
||||
char* sl = strrchr(exe, '/');
|
||||
if (!sl) return;
|
||||
snprintf(sl, sizeof(exe) - (size_t)(sl - exe), "/certs/ca-bundle.crt");
|
||||
FILE* f = fopen(exe, "rb");
|
||||
if (!f) return;
|
||||
fclose(f);
|
||||
SSL_CTX_load_verify_locations(ctx, exe, NULL);
|
||||
}
|
||||
|
||||
static bool ssl_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = { "SSL", {0.40f, 0.55f, 0.30f, 1.f}, {0.29f, 0.42f, 0.21f, 1.f} };
|
||||
tb_library_set_category(lib, &cat);
|
||||
@@ -98,6 +116,7 @@ static bool ssl_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
SSL_CTX_set_min_proto_version(s_ctx, TLS1_2_VERSION);
|
||||
SSL_CTX_set_verify(s_ctx, SSL_VERIFY_PEER, NULL);
|
||||
SSL_CTX_set_default_verify_paths(s_ctx); /* certificados do sistema */
|
||||
load_bundled_certs(s_ctx);
|
||||
lib->lib_userdata = (void*)&s_api;
|
||||
static const TinyBlocks_SlotDesc out[] = {{"versão", TB_VALUE_TEXT}};
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, "SSL.Version", NULL, 0, out, 1, eval_version);
|
||||
|
||||
@@ -230,8 +230,11 @@ void tt_parallel_for(int n, TbParallelFn fn, void* ctx) {
|
||||
/* == GEMM ============================================================= */
|
||||
/* Nucleo: C[m,n] += A[m,k] B[k,n], tudo contiguo e row-major. Quatro
|
||||
linhas de C por vez reaproveitam cada linha de B; o laco de dentro
|
||||
vetoriza (AVX2/FMA escolhido em execucao, SSE2 nos outros). */
|
||||
vetoriza (AVX2/FMA escolhido em execucao, SSE2 nos outros). A escolha
|
||||
em execucao usa ifunc, que so existe no ELF: no Windows fica o SSE2. */
|
||||
#if defined(__ELF__) && defined(__x86_64__)
|
||||
__attribute__((target_clones("arch=haswell", "default")))
|
||||
#endif
|
||||
static void kernel(int m, int n, int k, const float* A, int lda, const float* B, int ldb, float* C, int ldc) {
|
||||
enum { KB = 256, NB = 512 };
|
||||
for (int j0 = 0; j0 < n; j0 += NB) {
|
||||
|
||||
Reference in new issue
Block a user