Создание приложений с использованием многопоточности при обработки запросов клиентов, страница 5

                    break;

       }

       return pos;

}

inline bool enter(char c){

       if (c == '\n')

             return true;

       else

             return false;

}

inline bool notenter(char c){

       if (c != '\n')

             return true;

       else

             return false;

}

std::vector<std::string> Split(const std::string& s){

       typedef std::string::const_iterator iter;

       std::vector<std::string> ret;

       iter i = s.begin();

       while (i != s.end()){

             i = std::find_if(i, s.end(), notenter); // find the beginning of a word

             iter j = std::find_if(i, s.end(), enter); // find the end of the same word

             if (i != s.end()){

                    ret.push_back(std::string(i, j)); //insert the word into vector

                    i = j; // repeat 1,2,3 on the rest of the line.

             }

       }

       return ret;

}

void MakePostResponse()

{

       std::string request = "POST / HTTP/1.0\r\nContent-Length: 1024\r\nContent-Type: application/octet-stream\r\n\r\n";

       int iter = 0;

       for (; iter < request.length(); iter++)

             str[iter] = request[iter];

       for (int i = 0; i<7; i++)

             for (int j = 0; j<13; j++)

                    for (int k = 0; k<6; k++)

                    {

                          str[iter] = (char)(matrix[i][j][k]) + '0';

                          iter++;

                    }

       str[iter] = 0;

       //send(sock, message, sizeof(message), 0);

}

int ParseResponse(SOCKET sock)

{

       char message[1024];

       int result = recv(sock, message, sizeof(message), 0);

       if (result < 0)

             return result;

       std::string responseString;

       responseString.append(message);

       std::vector<std::string> responseLines = Split(responseString);

       char buffer[2];

       //strcpy(buffer, responseLines[4].c_str());

       //result = atoi(buffer);

       result = atoi(responseLines[4].c_str());

       return result;

}

void SendInfoGlobal()

{

       Sleep(100);

       MakePostResponse();

       for (int i = 0; i<totalCount; i++)

       {

             int clt = clientList[i];

             if (send(clt, str, 1024, 0) != 1024)

             {

             }

       }

}

void RunRecv(LPVOID param)

{

       SOCKET sock=(SOCKET) param;

       position point;

       char buffer[1024];

       int code;

       int iter;

       WaitForSingleObject(semaphore,INFINITE);

       point=GetPosition(playerCount+1);

       buffer[0]=(char) totalCount+'0';

       iter=playerCount;

       clientCodes[playerCount]=playerCount+1;

       clientList[playerCount]=sock;

       send(clientList[playerCount], buffer, 1024*sizeof(char), 0);

       matrix[point.y][point.x][playerCount+1]=playerCount+1;

       playerCount++;

       ReleaseSemaphore(semaphore,1,NULL);

       while(playerCount!=totalCount){}

       //MakePostResponse();

       SendInfoGlobal();

       while(true)

       {

             //code = ParseResponse(sock);

             char message[1024];

             int result = recv(sock, message, sizeof(message), 0);

             /*if (result < 0)

                    return result;*/

             std::string responseString;

             responseString.append(message);

             std::vector<std::string> responseLines = Split(responseString);

             char buffer[2];

             //strcpy(buffer, responseLines[4].c_str());

             //result = atoi(buffer);

             result = atoi(responseLines[4].c_str());

             code = result;

             int usercd = iter+1;

             if(code == SOCKET_ERROR)  

        {

                    KillPlayer(usercd);

                    char dMess[256];

                    sprintf(dMess, "Player%d disconnected\n", usercd);

                    PrintMessage(dMess);

                    Sleep(INFINITE);

        }

             else

             {

                    //code=atoi(buffer);

                    ProcessEnterKey(code,point,usercd);

                    //MakePostResponse();