尝试将基于 POSIX 的系统转换为 Windows

问题描述

我目前正在尝试转换多线程 LAN tic tac toe 游戏,以便它可以在我的 Windows 系统上编译/运行。我对网络很陌生,但我已经设法将很多系统调用转换为 Socket API 调用,但是我无法理解如何转换“pthread”函数。我在网上看到的大多数问题都是问如何编译它,但我想改变它而不是添加新的库等。

有人可以解释一下 pthread 到底是做什么的,以及我如何更改它以在本机 Windows 环境中编译。 谢谢!

主要功能

pthread_mutex_t games_lock = PTHREAD_MUTEX_INITIALIZER;

int main() {

    //Initialise Winsock
    WSAData data;
    WORD ver = MAKEWORD(2,2);
    int wsResult = WSAStartup(ver,&data);

    if (wsResult != 0) {
        std::cerr << "Can't start Winsock,Err #" << wsResult << endl;
    }

    // Signals used to kill the server gracefully
    if (signal(SIGINT,sig_handler) == SIG_ERR)
    {
        perror("Can't catch SIGINT");
        exit(1);
    }

    if (signal(SIGTERM,sig_handler) == SIG_ERR)
    {
        perror("Can't catch SIGTERM");
        exit(1);
    }

    // Initialize the server and get the server's socket
    server_sock = init_server();

    int new_socket = 0;

    // Infinitely accept clients and spawning threads
    while (true)
    {
        // Wait for a client,and then accept
        if ((new_socket = accept(server_sock,NULL,NULL)) < 0)
        {
            perror("Failed to accept client");
            closesocket(server_sock);
            exit(1);
        }

        cout << "New client connected" << endl;

        // Spawn thread to handle the client
        pthread_t threadid;
        pthread_create(&threadid,handle_client,(void*)&new_socket);
    }

    return 0;
}

客户端处理函数

{
    int client_sock = *(int*)arg;
    char buffer[BUF_SIZE];
    bool client_connected = true;
    char temp = '\0';
    int row = 0,col = 0;
    int i = 0;

    // Create the player
    Player player(client_sock);

    // Always handle the client
    while (client_connected)
    {
        // Process commands or pass game data
        if (player.GetMode() == COMMAND)
        {
            // Read a line of text or until the buffer is full
            for (i = 0; (i < (BUF_SIZE - 1)) && temp != '\n' && client_connected; ++i)
            {
                // Receive a single character and make sure the client is still connected
                if (recv(client_sock,&temp,1,0) == 0)
                    client_connected = false;
                else
                    buffer[i] = temp;
            }

            // Reset temp so we don't get an infinite loop
            temp = '\0';
            buffer[i] = '\0';
            buffer[i - 1] = '\0';
            cout << "Received command \"" << buffer << "\" from " << player.GetName() << endl;
            buffer[i - 1] = '\n';

            // If there's an invalid command,tell the client
            if (!ProcessCommand(buffer,player,client_connected))
                SendStatus(player.GetSocket(),INVALID_CMD);
        }
        else if (player.GetMode() == INGAME)
        {
            // Get the game the player is a part of
            pthread_mutex_lock(&games_lock);
            auto game = find_if(game_list.begin(),game_list.end(),[player](TTTGame* game) { return game->HasPlayer(player); });
            auto end = game_list.end();
            pthread_mutex_unlock(&games_lock);

            // Something horrible has gone wrong
            if (game == end)
                cout << "Somehow Player " << player.GetName() << " isn't a part of a game but is INGAME" << endl;
            else
            {
                StatusCode status;
                client_connected = ReceiveStatus(player.GetSocket(),&status);

                // If the player is still connected,then perform the move
                if (client_connected)
                {
                    switch (status)
                    {
                    case MOVE:
                        // Pass the row and column right along
                        ReceiveInt(player.GetSocket(),&row);
                        ReceiveInt(player.GetSocket(),&col);
                        cout << "Received moved from " << player.GetName()
                            << ": row=" << row << ",col=" << col << endl;

                        SendStatus((*game)->GetotherPlayer(player).GetSocket(),MOVE);
                        SendInt((*game)->GetotherPlayer(player).GetSocket(),row);
                        client_connected = SendInt((*game)->GetotherPlayer(player).GetSocket(),col);
                        cout << "Sent move to " << (*game)->GetotherPlayer(player).GetName() << endl;

                        break;
                    case WIN:
                        cout << player.GetName() << " won a game against " << (*game)->GetotherPlayer(player).GetName() << endl;
                        client_connected = false;
                        break;
                    case DRAW:
                        cout << player.GetName() << " tied against " << (*game)->GetotherPlayer(player).GetName() << endl;
                        client_connected = false;
                        break;
                    default:
                        client_connected = SendStatus(player.GetSocket(),INVALID_CMD);
                    }
                }
            }
        }
    }

    // The client disconnected on us D:
    cout << "Player \"" << player.GetName() << "\" has disconnected" << endl;
    disconnectPlayer(player);
    closesocket(client_sock);
    WSACleanup();

    return (void*)0;
}

这些只是代码的一些示例。如果需要,我很乐意发布更多信息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)