From 05c8e0a31a0d4192c590e833b24393208e1c751f Mon Sep 17 00:00:00 2001 From: tanaka takashi Date: Tue, 30 Jun 2026 19:03:25 +0900 Subject: fix: MCPCをvendored fork化しMCP準拠と接続安定性を改善 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Git submoduleを廃止してMCPCを直接管理 - Windowsで254リクエスト付近に発生する切断を修正 - JSON-RPC 2.0エラー応答を標準化 - tool schemaとruntimeの必須・任意引数を一致 - 1000回連続呼び出しの回帰テストを追加 --- third_party/mcpc/tst/commtst.c | 181 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 third_party/mcpc/tst/commtst.c (limited to 'third_party/mcpc/tst/commtst.c') diff --git a/third_party/mcpc/tst/commtst.c b/third_party/mcpc/tst/commtst.c new file mode 100644 index 0000000..a2ba8dc --- /dev/null +++ b/third_party/mcpc/tst/commtst.c @@ -0,0 +1,181 @@ +#include "commtst.h" + +#include + +i32_t +fsize (FILE *strm) +{ + fseek (strm, 0, SEEK_SET); + fseek (strm, 0, SEEK_END); + long sz = ftell (strm); + assert (sz > 0); + fseek (strm, 0, SEEK_SET); + return sz; +} + +void +read_atmost (void *src, char *rbuf, const size_t nmost, size_t *nread) +{ +#ifdef is_win + HANDLE *hdlp = (HANDLE *) src; +#else + int *fdp = (int *) src; +#endif + size_t nread_accm = 0; + size_t nleft = nmost; + while (nleft > 0) + { + ssize_t nbuffered = nmost - nleft; +#ifdef is_win + ssize_t nread_cur = 0; + assT (ReadFile + (*hdlp, rbuf + nbuffered, (DWORD)nleft, (LPDWORD) (&nread_cur), + nullptr)); +#else + ssize_t nread_cur = read (*fdp, rbuf + nbuffered, nleft); +#endif + if (nread_cur <= 0) + break; + nread_accm += nread_cur; + nleft -= nread_cur; + } + *nread = nread_accm; +} + +void +recv_atmost ( +#if is_unix + int csock, +#elif is_win + SOCKET csock, +#endif + char *rbuf, const size_t nmost, size_t *nread) +{ + size_t nread_accm = 0; + size_t nleft = nmost; + while (nleft > 0) + { + ssize_t nbuffered = nmost - nleft; + ssize_t nread_cur = recv (csock, rbuf + nbuffered, (int)nleft, 0); + if (nread_cur <= 0) + break; + nread_accm += nread_cur; + nleft -= nread_cur; + } + *nread = nread_accm; +} + +i32_t +read_u8str (const char8_t *const filename, char8_t *const destbuf) +{ + FILE *file; + +#if is_win + fopen_s (&file, (const char*) filename, "rb,ccs=UTF-8"); +#else + file = fopen ((const char*) filename, "rb"); +#endif + assert (file != nullptr); + + i32_t fsz = fsize (file); + + size_t bytes_read = fread (destbuf, 1, fsz, file); + assert (bytes_read > 0); + + // null-terminate the buffer + destbuf[bytes_read] = 0; + // printf("File content: %s\n", destbuf); + + fclose (file); + + return 0; +} + +i32_t +read_u8str_sz (const char8_t *const filename, char8_t *const destbuf, + size_t *nread) +{ + FILE *file; + +#if is_win + fopen_s (&file, (const char*) filename, "rb,ccs=UTF-8"); +#else + file = fopen ((const char*) filename, "rb"); +#endif + assTcb (file != nullptr, + { + fprintf (stderr, "filename:%s\n", filename); + }); + + i32_t fsz = fsize (file); + + size_t bytes_read = fread (destbuf, 1, fsz, file); + assert (bytes_read > 0); + + // null-terminate the buffer + destbuf[bytes_read] = 0; + *nread = bytes_read; + // printf("File content: %s\n", destbuf); + + fclose (file); + + return 0; +} + +void +zero_trailing_white_char (char8_t *buf, const size_t len) +{ + for (size_t i = len - 1; i != 0; i--) // TODO ckd_add + { + if (buf[i] == '\n' || buf[i] == '\r' || buf[i] == '\t' || buf[i] == ' ') + buf[i] = 0; + else + break; + } +} + +void +zero_trailing_white_char2 (char8_t *buf, size_t *len) +{ + for (size_t i = *len - 1; i != 0; i--) // TODO ckd_add + { + if (buf[i] == '\n' || buf[i] == '\r' || buf[i] == '\t' || buf[i] == ' ') + { + buf[i] = 0; + *len -= 1; + } + else + break; + } +} + +#ifdef is_unix +FILE * +unix_fd2strm_r (int fd) +{ + return fdopen (fd, "r"); +} +#endif + +#ifdef is_win +FILE * +win_handle2strm (HANDLE h) +{ + int fd = _open_osfhandle ((intptr_t) h, _O_RDONLY); + assT (fd > 0); + + FILE *file = _fdopen (fd, "r+"); + assT (file != nullptr); + return file; +} +#endif + +#ifdef is_win +int +win_handle2fd (HANDLE h) +{ + int fd = _open_osfhandle ((intptr_t) h, _O_RDONLY); + assT (fd > 0); + return fd; +} +#endif -- cgit v1.2.3