DEFINE_RESPONSE_TABLE1(game, TWindow) EV_WM_LBUTTONDOWN, EV_WM_RBUTTONDOWN, EV_WM_MOUSEMOVE, EV_WM_LBUTTONUP, EV_WM_RBUTTONUP, EV_COMMAND(CM_EXIT, CmExit), EV_BN_CLICKED(IDB_CMD, CmCmd), // Connect, start and stop EV_BN_CLICKED(IDB_SEND, CmSend), EV_BN_CLICKED(IDB_EXIT, CmExit), EV_MESSAGE(MSG_HOST_INFO_NOTIFY, DoHostNotification), EV_MESSAGE(MSG_SOCKET_NOTIFY, DoSocketNotification), END_RESPONSE_TABLE; else if (szData[0] == 'S') { // start game was received if (state == playing) return 1; Cmd->SetCaption("Stop"); state = playing; } else if (szData[0] == 'Q') { // stop/quit game was received if (state != playing) return 1; Cmd->SetCaption("Start"); state = waiting_for_start; } else if (szData[0] == 'L') { // a new line was received if (state != playing) return 1; add_rec(your_first, 'L', ((obj_rec *)szData)->p1, ((obj_rec *)szData)->p2); Invalidate(); } else if (szData[0] == 'R') { // a new linerectangle was received if (state != playing) return 1; add_rec(your_first, 'R', ((obj_rec *)szData)->p1, ((obj_rec *)szData)->p2); Invalidate(); } // Could not CONNECT. We will listen Cmd->SetCaption("Listening."); Cmd->EnableWindow(false); myStreamSocket->ShutDownSocket(); nError = myStreamSocket->CreateSocket(); //similar to 'socket()' if (nError == WINSOCK_ERROR){ //could also say 'if (nError){' MessageBox("Error on CreateSocket()", "Error", MB_OK); SendMessage(WM_CLOSE); } myStreamSocket->StartAcceptNotification(); //similar to 'WSAAsyncSelect()' myStreamSocket->SetNotificationWindow(this); //redirect socket FD_XXX notifications to us. ushort nPort = TWinSock::Dll()->htons(game_port); nError = myStreamSocket->BindSocket(TINetSocketAddress(nPort, INADDR_ANY)); //similar to 'bind()' if (nError == WINSOCK_ERROR){ //could also say 'if (nError){' MessageBox("Error on BindSocket()", "Error", MB_OK); SendMessage(WM_CLOSE); } nError = myStreamSocket->Listen(); if (nError == WINSOCK_ERROR){ //could also say 'if (nError){' MessageBox("Error on Listen()", "Error", MB_OK); SendMessage(WM_CLOSE); } else if (nEvent == FD_ACCEPT){ if (nError) { MessageBox("Error on receipt of FD_ACCEPT.", "Error", MB_OK); SendMessage(WM_CLOSE); } else { TStreamSocket newStreamSocket; //construct an empty StreamSocket object. newStreamSocket.SetSaveSocketOnDelete(); //Don't close the socket on deletion, b/c we're giving it to someone else. nError = myStreamSocket->Accept(newStreamSocket); //Accept() will completely fix up nStreamSocket. if (nError == WINSOCK_ERROR){ //could also say: 'if (nError){' MessageBox("Error on Accept().", "Error", MB_OK); SendMessage(WM_CLOSE); } char szPort[12]; int nError; *myStreamSocket = newStreamSocket; //StreamSocket::operator=() myStreamSocket->SetSaveSocketOnDelete(FALSE); //Now we own the socket descriptor. sAddressToConnectTo = newStreamSocket.PeerSocketAddress; // The 'newConnectedSocket' may or may not be set up for Notification, so // we specifically set it here to make sure it is the way we want it: // myStreamSocket->SetNotificationWindow(this); nError = myStreamSocket->StartRegularNotification(); if (nError == WINSOCK_ERROR) { MessageBox("Error on StartRegularNotification() in ConnectWithThis().", "Error", MB_OK); SendMessage(WM_CLOSE); } // Equivalent Winsock code: // nError = TWinSock::Dll()->WSAAsyncSelect(*myStreamSocket, *this, MSG_SOCKET_NOTIFY, FD_READ | FD_WRITE | FD_CLOSE | FD_CONNECT | FD_OOB); if (nError) { MessageBox("Error on WSASelect()", "Error", MB_OK); SendMessage(WM_CLOSE); } GoToConnectedState(); oid game::GoToConnectedState() { Label_1->SetCaption("Message to Send"); Label_2->SetCaption("Message Received"); Me->SetReadOnly(false); Me->SetCaption(""); You->SetCaption(""); You->SetReadOnly(true); Cmd->EnableWindow(true); Cmd->SetCaption("Start"); Send->EnableWindow(true); state = waiting_for_start; } send_data((char *)first, sizeof type + 2 * (sizeof p1)); else if (szData[0] == 'L') { // a new line was received if (state != playing) return 1; add_rec(your_first, 'L', ((obj_rec *)szData)->p1, ((obj_rec *)szData)->p2); Invalidate(); }