rlm@1: // GDBConnection.cpp : implementation file rlm@1: // rlm@1: rlm@1: #include "stdafx.h" rlm@1: #include "resource.h" rlm@1: #include "GDBConnection.h" rlm@1: rlm@1: #include "../common/System.h" rlm@1: rlm@1: #define SOCKET_MESSAGE WM_APP+1 rlm@1: rlm@1: static bool initialized = false; rlm@1: rlm@1: ///////////////////////////////////////////////////////////////////////////// rlm@1: // GDBPortDlg dialog rlm@1: rlm@1: GDBPortDlg::GDBPortDlg(CWnd*pParent /*=NULL*/) rlm@1: : CDialog(GDBPortDlg::IDD, pParent) rlm@1: { rlm@1: //{{AFX_DATA_INIT(GDBPortDlg) rlm@1: // NOTE: the ClassWizard will add member initialization here rlm@1: //}}AFX_DATA_INIT rlm@1: port = 55555; rlm@1: sock = INVALID_SOCKET; rlm@1: rlm@1: if (!initialized) rlm@1: { rlm@1: WSADATA wsaData; rlm@1: rlm@1: int error = WSAStartup(MAKEWORD(1, 1), &wsaData); rlm@1: rlm@1: initialized = true; rlm@1: } rlm@1: } rlm@1: rlm@1: void GDBPortDlg::DoDataExchange(CDataExchange*pDX) rlm@1: { rlm@1: CDialog::DoDataExchange(pDX); rlm@1: //{{AFX_DATA_MAP(GDBPortDlg) rlm@1: DDX_Control(pDX, IDC_PORT, m_port); rlm@1: //}}AFX_DATA_MAP rlm@1: } rlm@1: rlm@1: BEGIN_MESSAGE_MAP(GDBPortDlg, CDialog) rlm@1: //{{AFX_MSG_MAP(GDBPortDlg) rlm@1: ON_BN_CLICKED(ID_OK, OnOk) rlm@1: ON_BN_CLICKED(ID_CANCEL, OnCancel) rlm@1: ON_WM_CLOSE() rlm@1: //}}AFX_MSG_MAP rlm@1: END_MESSAGE_MAP() rlm@1: rlm@1: ///////////////////////////////////////////////////////////////////////////// rlm@1: // GDBPortDlg message handlers rlm@1: rlm@1: int GDBPortDlg::getPort() rlm@1: { rlm@1: return port; rlm@1: } rlm@1: rlm@1: SOCKET GDBPortDlg::getSocket() rlm@1: { rlm@1: return sock; rlm@1: } rlm@1: rlm@1: BOOL GDBPortDlg::OnInitDialog() rlm@1: { rlm@1: CDialog::OnInitDialog(); rlm@1: rlm@1: CString buffer; rlm@1: rlm@1: buffer.Format("%d", port); rlm@1: rlm@1: m_port.SetWindowText(buffer); rlm@1: rlm@1: CenterWindow(); rlm@1: rlm@1: return TRUE; // return TRUE unless you set the focus to a control rlm@1: // EXCEPTION: OCX Property Pages should return FALSE rlm@1: } rlm@1: rlm@1: void GDBPortDlg::OnOk() rlm@1: { rlm@1: CString buffer; rlm@1: rlm@1: m_port.GetWindowText(buffer); rlm@1: rlm@1: sockaddr_in address; rlm@1: address.sin_family = AF_INET; rlm@1: address.sin_addr.s_addr = inet_addr("0.0.0.0"); rlm@1: address.sin_port = htons(atoi(buffer)); rlm@1: port = ntohs(address.sin_port); rlm@1: rlm@1: SOCKET s = socket(AF_INET, SOCK_STREAM, 0); rlm@1: rlm@1: if (s != INVALID_SOCKET) rlm@1: { rlm@1: int error = bind(s, (sockaddr *)&address, sizeof(address)); rlm@1: rlm@1: if (error) rlm@1: { rlm@1: systemMessage(IDS_ERROR_BINDING, "Error binding socket. Port probably in use."); rlm@1: error = closesocket(s); rlm@1: EndDialog(FALSE); rlm@1: } rlm@1: else rlm@1: { rlm@1: error = listen(s, 1); rlm@1: if (!error) rlm@1: { rlm@1: sock = s; rlm@1: EndDialog(TRUE); rlm@1: } rlm@1: else rlm@1: { rlm@1: systemMessage(IDS_ERROR_LISTENING, "Error listening on socket."); rlm@1: closesocket(s); rlm@1: EndDialog(FALSE); rlm@1: } rlm@1: } rlm@1: } rlm@1: else rlm@1: { rlm@1: systemMessage(IDS_ERROR_CREATING_SOCKET, "Error creating socket."); rlm@1: EndDialog(FALSE); rlm@1: } rlm@1: } rlm@1: rlm@1: void GDBPortDlg::OnCancel() rlm@1: { rlm@1: OnClose(); rlm@1: } rlm@1: rlm@1: void GDBPortDlg::OnClose() rlm@1: { rlm@1: EndDialog(FALSE); rlm@1: } rlm@1: rlm@1: ///////////////////////////////////////////////////////////////////////////// rlm@1: // GDBWaitingDlg dialog rlm@1: rlm@1: GDBWaitingDlg::GDBWaitingDlg(SOCKET s, int p, CWnd*pParent /*=NULL*/) rlm@1: : CDialog(GDBWaitingDlg::IDD, pParent) rlm@1: { rlm@1: //{{AFX_DATA_INIT(GDBWaitingDlg) rlm@1: //}}AFX_DATA_INIT rlm@1: port = p & 65535; rlm@1: listenSocket = s; rlm@1: } rlm@1: rlm@1: void GDBWaitingDlg::DoDataExchange(CDataExchange*pDX) rlm@1: { rlm@1: CDialog::DoDataExchange(pDX); rlm@1: //{{AFX_DATA_MAP(GDBWaitingDlg) rlm@1: DDX_Control(pDX, IDC_PORT, m_port); rlm@1: //}}AFX_DATA_MAP rlm@1: } rlm@1: rlm@1: BEGIN_MESSAGE_MAP(GDBWaitingDlg, CDialog) rlm@1: //{{AFX_MSG_MAP(GDBWaitingDlg) rlm@1: ON_BN_CLICKED(ID_CANCEL, OnCancel) rlm@1: ON_WM_CLOSE() rlm@1: //}}AFX_MSG_MAP rlm@1: END_MESSAGE_MAP() rlm@1: rlm@1: ///////////////////////////////////////////////////////////////////////////// rlm@1: // GDBWaitingDlg message handlers rlm@1: rlm@1: BOOL GDBWaitingDlg::OnInitDialog() rlm@1: { rlm@1: CDialog::OnInitDialog(); rlm@1: rlm@1: CString buffer; rlm@1: rlm@1: buffer.Format("%d", port); rlm@1: rlm@1: m_port.SetWindowText(buffer); rlm@1: rlm@1: CenterWindow(); rlm@1: rlm@1: int error = WSAAsyncSelect(listenSocket, rlm@1: (HWND)*this, rlm@1: SOCKET_MESSAGE, rlm@1: FD_ACCEPT); rlm@1: rlm@1: return TRUE; // return TRUE unless you set the focus to a control rlm@1: // EXCEPTION: OCX Property Pages should return FALSE rlm@1: } rlm@1: rlm@1: LRESULT GDBWaitingDlg::OnSocketAccept(WPARAM wParam, LPARAM lParam) rlm@1: { rlm@1: if (LOWORD(lParam) == FD_ACCEPT) rlm@1: { rlm@1: WSAAsyncSelect(listenSocket, (HWND)*this, 0, 0); rlm@1: rlm@1: int flag = 0; rlm@1: ioctlsocket(listenSocket, FIONBIO, (unsigned long *)&flag); rlm@1: rlm@1: SOCKET s = accept(listenSocket, NULL, NULL); rlm@1: if (s != INVALID_SOCKET) rlm@1: { rlm@1: char dummy; rlm@1: recv(s, &dummy, 1, 0); rlm@1: if (dummy != '+') rlm@1: { rlm@1: systemMessage(IDS_ACK_NOT_RECEIVED, "ACK not received from GDB."); rlm@1: OnClose(); // calls EndDialog rlm@1: } rlm@1: else rlm@1: { rlm@1: sock = s; rlm@1: EndDialog(TRUE); rlm@1: } rlm@1: } rlm@1: } rlm@1: rlm@1: return TRUE; rlm@1: } rlm@1: rlm@1: void GDBWaitingDlg::OnCancel() rlm@1: { rlm@1: OnClose(); rlm@1: } rlm@1: rlm@1: void GDBWaitingDlg::OnClose() rlm@1: { rlm@1: if (sock != INVALID_SOCKET) rlm@1: { rlm@1: closesocket(sock); rlm@1: sock = INVALID_SOCKET; rlm@1: } rlm@1: if (listenSocket != INVALID_SOCKET) rlm@1: { rlm@1: closesocket(listenSocket); rlm@1: listenSocket = INVALID_SOCKET; rlm@1: } rlm@1: EndDialog(FALSE); rlm@1: } rlm@1: rlm@1: SOCKET GDBWaitingDlg::getListenSocket() rlm@1: { rlm@1: return listenSocket; rlm@1: } rlm@1: rlm@1: SOCKET GDBWaitingDlg::getSocket() rlm@1: { rlm@1: return sock; rlm@1: } rlm@1: