annotate src/win32/GDBConnection.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
rev   line source
rlm@1 1 // GDBConnection.cpp : implementation file
rlm@1 2 //
rlm@1 3
rlm@1 4 #include "stdafx.h"
rlm@1 5 #include "resource.h"
rlm@1 6 #include "GDBConnection.h"
rlm@1 7
rlm@1 8 #include "../common/System.h"
rlm@1 9
rlm@1 10 #define SOCKET_MESSAGE WM_APP+1
rlm@1 11
rlm@1 12 static bool initialized = false;
rlm@1 13
rlm@1 14 /////////////////////////////////////////////////////////////////////////////
rlm@1 15 // GDBPortDlg dialog
rlm@1 16
rlm@1 17 GDBPortDlg::GDBPortDlg(CWnd*pParent /*=NULL*/)
rlm@1 18 : CDialog(GDBPortDlg::IDD, pParent)
rlm@1 19 {
rlm@1 20 //{{AFX_DATA_INIT(GDBPortDlg)
rlm@1 21 // NOTE: the ClassWizard will add member initialization here
rlm@1 22 //}}AFX_DATA_INIT
rlm@1 23 port = 55555;
rlm@1 24 sock = INVALID_SOCKET;
rlm@1 25
rlm@1 26 if (!initialized)
rlm@1 27 {
rlm@1 28 WSADATA wsaData;
rlm@1 29
rlm@1 30 int error = WSAStartup(MAKEWORD(1, 1), &wsaData);
rlm@1 31
rlm@1 32 initialized = true;
rlm@1 33 }
rlm@1 34 }
rlm@1 35
rlm@1 36 void GDBPortDlg::DoDataExchange(CDataExchange*pDX)
rlm@1 37 {
rlm@1 38 CDialog::DoDataExchange(pDX);
rlm@1 39 //{{AFX_DATA_MAP(GDBPortDlg)
rlm@1 40 DDX_Control(pDX, IDC_PORT, m_port);
rlm@1 41 //}}AFX_DATA_MAP
rlm@1 42 }
rlm@1 43
rlm@1 44 BEGIN_MESSAGE_MAP(GDBPortDlg, CDialog)
rlm@1 45 //{{AFX_MSG_MAP(GDBPortDlg)
rlm@1 46 ON_BN_CLICKED(ID_OK, OnOk)
rlm@1 47 ON_BN_CLICKED(ID_CANCEL, OnCancel)
rlm@1 48 ON_WM_CLOSE()
rlm@1 49 //}}AFX_MSG_MAP
rlm@1 50 END_MESSAGE_MAP()
rlm@1 51
rlm@1 52 /////////////////////////////////////////////////////////////////////////////
rlm@1 53 // GDBPortDlg message handlers
rlm@1 54
rlm@1 55 int GDBPortDlg::getPort()
rlm@1 56 {
rlm@1 57 return port;
rlm@1 58 }
rlm@1 59
rlm@1 60 SOCKET GDBPortDlg::getSocket()
rlm@1 61 {
rlm@1 62 return sock;
rlm@1 63 }
rlm@1 64
rlm@1 65 BOOL GDBPortDlg::OnInitDialog()
rlm@1 66 {
rlm@1 67 CDialog::OnInitDialog();
rlm@1 68
rlm@1 69 CString buffer;
rlm@1 70
rlm@1 71 buffer.Format("%d", port);
rlm@1 72
rlm@1 73 m_port.SetWindowText(buffer);
rlm@1 74
rlm@1 75 CenterWindow();
rlm@1 76
rlm@1 77 return TRUE; // return TRUE unless you set the focus to a control
rlm@1 78 // EXCEPTION: OCX Property Pages should return FALSE
rlm@1 79 }
rlm@1 80
rlm@1 81 void GDBPortDlg::OnOk()
rlm@1 82 {
rlm@1 83 CString buffer;
rlm@1 84
rlm@1 85 m_port.GetWindowText(buffer);
rlm@1 86
rlm@1 87 sockaddr_in address;
rlm@1 88 address.sin_family = AF_INET;
rlm@1 89 address.sin_addr.s_addr = inet_addr("0.0.0.0");
rlm@1 90 address.sin_port = htons(atoi(buffer));
rlm@1 91 port = ntohs(address.sin_port);
rlm@1 92
rlm@1 93 SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
rlm@1 94
rlm@1 95 if (s != INVALID_SOCKET)
rlm@1 96 {
rlm@1 97 int error = bind(s, (sockaddr *)&address, sizeof(address));
rlm@1 98
rlm@1 99 if (error)
rlm@1 100 {
rlm@1 101 systemMessage(IDS_ERROR_BINDING, "Error binding socket. Port probably in use.");
rlm@1 102 error = closesocket(s);
rlm@1 103 EndDialog(FALSE);
rlm@1 104 }
rlm@1 105 else
rlm@1 106 {
rlm@1 107 error = listen(s, 1);
rlm@1 108 if (!error)
rlm@1 109 {
rlm@1 110 sock = s;
rlm@1 111 EndDialog(TRUE);
rlm@1 112 }
rlm@1 113 else
rlm@1 114 {
rlm@1 115 systemMessage(IDS_ERROR_LISTENING, "Error listening on socket.");
rlm@1 116 closesocket(s);
rlm@1 117 EndDialog(FALSE);
rlm@1 118 }
rlm@1 119 }
rlm@1 120 }
rlm@1 121 else
rlm@1 122 {
rlm@1 123 systemMessage(IDS_ERROR_CREATING_SOCKET, "Error creating socket.");
rlm@1 124 EndDialog(FALSE);
rlm@1 125 }
rlm@1 126 }
rlm@1 127
rlm@1 128 void GDBPortDlg::OnCancel()
rlm@1 129 {
rlm@1 130 OnClose();
rlm@1 131 }
rlm@1 132
rlm@1 133 void GDBPortDlg::OnClose()
rlm@1 134 {
rlm@1 135 EndDialog(FALSE);
rlm@1 136 }
rlm@1 137
rlm@1 138 /////////////////////////////////////////////////////////////////////////////
rlm@1 139 // GDBWaitingDlg dialog
rlm@1 140
rlm@1 141 GDBWaitingDlg::GDBWaitingDlg(SOCKET s, int p, CWnd*pParent /*=NULL*/)
rlm@1 142 : CDialog(GDBWaitingDlg::IDD, pParent)
rlm@1 143 {
rlm@1 144 //{{AFX_DATA_INIT(GDBWaitingDlg)
rlm@1 145 //}}AFX_DATA_INIT
rlm@1 146 port = p & 65535;
rlm@1 147 listenSocket = s;
rlm@1 148 }
rlm@1 149
rlm@1 150 void GDBWaitingDlg::DoDataExchange(CDataExchange*pDX)
rlm@1 151 {
rlm@1 152 CDialog::DoDataExchange(pDX);
rlm@1 153 //{{AFX_DATA_MAP(GDBWaitingDlg)
rlm@1 154 DDX_Control(pDX, IDC_PORT, m_port);
rlm@1 155 //}}AFX_DATA_MAP
rlm@1 156 }
rlm@1 157
rlm@1 158 BEGIN_MESSAGE_MAP(GDBWaitingDlg, CDialog)
rlm@1 159 //{{AFX_MSG_MAP(GDBWaitingDlg)
rlm@1 160 ON_BN_CLICKED(ID_CANCEL, OnCancel)
rlm@1 161 ON_WM_CLOSE()
rlm@1 162 //}}AFX_MSG_MAP
rlm@1 163 END_MESSAGE_MAP()
rlm@1 164
rlm@1 165 /////////////////////////////////////////////////////////////////////////////
rlm@1 166 // GDBWaitingDlg message handlers
rlm@1 167
rlm@1 168 BOOL GDBWaitingDlg::OnInitDialog()
rlm@1 169 {
rlm@1 170 CDialog::OnInitDialog();
rlm@1 171
rlm@1 172 CString buffer;
rlm@1 173
rlm@1 174 buffer.Format("%d", port);
rlm@1 175
rlm@1 176 m_port.SetWindowText(buffer);
rlm@1 177
rlm@1 178 CenterWindow();
rlm@1 179
rlm@1 180 int error = WSAAsyncSelect(listenSocket,
rlm@1 181 (HWND)*this,
rlm@1 182 SOCKET_MESSAGE,
rlm@1 183 FD_ACCEPT);
rlm@1 184
rlm@1 185 return TRUE; // return TRUE unless you set the focus to a control
rlm@1 186 // EXCEPTION: OCX Property Pages should return FALSE
rlm@1 187 }
rlm@1 188
rlm@1 189 LRESULT GDBWaitingDlg::OnSocketAccept(WPARAM wParam, LPARAM lParam)
rlm@1 190 {
rlm@1 191 if (LOWORD(lParam) == FD_ACCEPT)
rlm@1 192 {
rlm@1 193 WSAAsyncSelect(listenSocket, (HWND)*this, 0, 0);
rlm@1 194
rlm@1 195 int flag = 0;
rlm@1 196 ioctlsocket(listenSocket, FIONBIO, (unsigned long *)&flag);
rlm@1 197
rlm@1 198 SOCKET s = accept(listenSocket, NULL, NULL);
rlm@1 199 if (s != INVALID_SOCKET)
rlm@1 200 {
rlm@1 201 char dummy;
rlm@1 202 recv(s, &dummy, 1, 0);
rlm@1 203 if (dummy != '+')
rlm@1 204 {
rlm@1 205 systemMessage(IDS_ACK_NOT_RECEIVED, "ACK not received from GDB.");
rlm@1 206 OnClose(); // calls EndDialog
rlm@1 207 }
rlm@1 208 else
rlm@1 209 {
rlm@1 210 sock = s;
rlm@1 211 EndDialog(TRUE);
rlm@1 212 }
rlm@1 213 }
rlm@1 214 }
rlm@1 215
rlm@1 216 return TRUE;
rlm@1 217 }
rlm@1 218
rlm@1 219 void GDBWaitingDlg::OnCancel()
rlm@1 220 {
rlm@1 221 OnClose();
rlm@1 222 }
rlm@1 223
rlm@1 224 void GDBWaitingDlg::OnClose()
rlm@1 225 {
rlm@1 226 if (sock != INVALID_SOCKET)
rlm@1 227 {
rlm@1 228 closesocket(sock);
rlm@1 229 sock = INVALID_SOCKET;
rlm@1 230 }
rlm@1 231 if (listenSocket != INVALID_SOCKET)
rlm@1 232 {
rlm@1 233 closesocket(listenSocket);
rlm@1 234 listenSocket = INVALID_SOCKET;
rlm@1 235 }
rlm@1 236 EndDialog(FALSE);
rlm@1 237 }
rlm@1 238
rlm@1 239 SOCKET GDBWaitingDlg::getListenSocket()
rlm@1 240 {
rlm@1 241 return listenSocket;
rlm@1 242 }
rlm@1 243
rlm@1 244 SOCKET GDBWaitingDlg::getSocket()
rlm@1 245 {
rlm@1 246 return sock;
rlm@1 247 }
rlm@1 248