bool create(creation_params param) { if(param.w != CW_USEDEFAULT){ RECT rc = {0, 0, param.w, param.h}; ::AdjustWindowRectEx(&rc, param.style, FALSE, param.exstyle); param.w = rc.right - rc.left; param.h = rc.bottom - rc.top; } if(param.data == nullptr){ param.data = static_cast<Derived*>(this); } start_message_loop(); auto message = std::make_shared<async_message>(); std::unique_lock<std::mutex> lock(message->mutex); message->message = async_message::message_type::create; message->data = std::make_shared<creation_params>(param); queue.push(message); message->cv.wait_for(lock, std::chrono::seconds(3)); return message->result; }
int client() { int nret; // Used for error reporting char hostAddress[128]; unsigned int hostPort; printf("Enter the address of the host you'd like to connect to: "); scanf("%s", hostAddress); printf("Enter the port you'd like to make a connection on[0-65535]: "); do{ scanf("%d", &hostPort ); if( hostPort > 65535 ) printf("Port out of bounds! Please enter a number in the range [0-65535]: "); }while( hostPort > 65535 ); LPHOSTENT hostInfo; hostInfo = gethostbyname(hostAddress); //IN_ADDR iaHost; //iaHost.s_addr = inet_addr(hostAddress); //hostInfo = gethostbyaddr( (const char*)&iaHost.s_addr, sizeof( struct in_addr ), AF_INET ); if (!hostInfo) { nret = WSAGetLastError(); ReportError(nret, "gethostbyname()"); return NETWORK_ERROR; } SOCKET theSocket; theSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); if ( theSocket == INVALID_SOCKET ) { nret = WSAGetLastError(); ReportError(nret, "socket()"); return NETWORK_ERROR; } SOCKADDR_IN serverInfo; serverInfo.sin_family = AF_INET; serverInfo.sin_addr = *( (LPIN_ADDR)hostInfo->h_addr_list[0] ); serverInfo.sin_port = htons( hostPort ); nret = connect( theSocket, (LPSOCKADDR)&serverInfo, sizeof( struct sockaddr ) ); if ( nret == SOCKET_ERROR ) { nret = WSAGetLastError(); ReportError(nret, "connect()"); return NETWORK_ERROR; } char* message = readLine( theSocket ); printf( "%s\n", message ); start_message_loop( theSocket, MODE_SEND ); closesocket( theSocket ); system( "PAUSE" ); return NETWORK_OK; }