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