<commit_before>#define WIN32_LEAN_AND_MEAN
#define STRICT
#define UNICODE
#include <winsock2.h>
#include <shellapi.h>
#include <stdio.h>
#include <time.h>

// int(md5.md5('ibb').hexdigest()[-4:], 16)
const int IBB_PORT = 26830;

int error(const char* msg) {
    fprintf(stderr, "ibb *** error: %s\n", msg);
    return 1;
}

bool openServerConnection(SOCKET* s) {
    *s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (*s == INVALID_SOCKET) {
        // TODO: print error code
        error("Failed to create socket");
        return false;
    }

    sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(IBB_PORT);
    
    int result = connect(*s, reinterpret_cast<sockaddr*>(&address), sizeof(address));
    if (result) {
        error("Failed to connect to IBB server");
        return false;
    }

    return true;
}

bool startServer() {
    WCHAR python_path[MAX_PATH + 1] = {0};
    LONG size = sizeof(python_path);
    LONG success = RegQueryValueW(
        HKEY_LOCAL_MACHINE,
        L"SOFTWARE\\Python\\PythonCore\\3.1\\InstallPath",
        python_path,
        &size);
    if (success) {
        // TODO: print error
        fprintf(stderr, "ibb *** failed to locate Python 3.1\n");
        return false;
    }

    wcsncat(python_path, L"\\python.exe", MAX_PATH);

    // TODO: print error code
    HINSTANCE result = ShellExecuteW(0, L"open", python_path, L"", NULL, SW_SHOW);
    return result > reinterpret_cast<HINSTANCE>(32);
}

void sendString(SOCKET connection, const WCHAR* begin, const WCHAR* end = 0) {
    if (!end) {
        end = begin + wcslen(begin);
    }
    
    // UTF-16 over the wire
    send(
        connection,<commit_msg>Actually start the build server if ibb.exe detects that it's not running.<commit_after>#define WIN32_LEAN_AND_MEAN
#define STRICT
#define UNICODE
#include <winsock2.h>
#include <shellapi.h>
#include <stdio.h>
#include <time.h>

// int(md5.md5('ibb').hexdigest()[-4:], 16)
const int IBB_PORT = 26830;

int error(const char* msg) {
    fprintf(stderr, "ibb *** error: %s\n", msg);
    return 1;
}

bool openServerConnection(SOCKET* s) {
    *s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (*s == INVALID_SOCKET) {
        // TODO: print error code
        error("Failed to create socket");
        return false;
    }

    sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(IBB_PORT);
    
    int result = connect(*s, reinterpret_cast<sockaddr*>(&address), sizeof(address));
    if (result) {
        error("Failed to connect to IBB server");
        return false;
    }

    return true;
}

bool startServer() {
    WCHAR python_path[MAX_PATH + 1] = {0};
    LONG size = sizeof(python_path);
    LONG success = RegQueryValueW(
        HKEY_LOCAL_MACHINE,
        L"SOFTWARE\\Python\\PythonCore\\3.1\\InstallPath",
        python_path,
        &size);
    if (success) {
        // TODO: print error
        fprintf(stderr, "ibb *** failed to locate Python 3.1\n");
        return false;
    }

    // TODO: use safe strings
    wcsncat(python_path, L"\\python.exe", MAX_PATH);

    WCHAR executable_path[MAX_PATH * 2];
    DWORD length = GetModuleFileNameW(GetModuleHandle(NULL), executable_path, MAX_PATH);
    if (length == 0 || length == MAX_PATH) {
        fprintf(stderr, "ibb *** failed to get executable path\n");
        return false;
    }

    wchar_t* last_slash = wcsrchr(executable_path, '\\');
    if (last_slash) {
        *last_slash = 0;
    }
    wcsncat(executable_path, L"\\src\\ibb.py", MAX_PATH * 2);
    
    // TODO: use CreateProcess instead of ShellExecute
    // TODO: print error code
    HINSTANCE result = ShellExecuteW(0, L"open", python_path, executable_path, NULL, SW_SHOW);
    return result > reinterpret_cast<HINSTANCE>(32);
}

void sendString(SOCKET connection, const WCHAR* begin, const WCHAR* end = 0) {
    if (!end) {
        end = begin + wcslen(begin);
    }
    
    // UTF-16 over the wire
    send(
        connection,
{'fraction_non_alphanumeric': 0.13482599172548065, 'fraction_numerical': 0.016549038695546362, 'mean_word_length': 2.844714686623012, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '873544', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1410, 'n_words': 343}