view src/win32/GDBConnection.cpp @ 8:08a8e09ca414

add files required by automake
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:54:39 -0600
parents f9f4f1b99eed
children
line wrap: on
line source
1 // GDBConnection.cpp : implementation file
2 //
4 #include "stdafx.h"
5 #include "resource.h"
6 #include "GDBConnection.h"
8 #include "../common/System.h"
10 #define SOCKET_MESSAGE WM_APP+1
12 static bool initialized = false;
14 /////////////////////////////////////////////////////////////////////////////
15 // GDBPortDlg dialog
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;
26 if (!initialized)
27 {
28 WSADATA wsaData;
30 int error = WSAStartup(MAKEWORD(1, 1), &wsaData);
32 initialized = true;
33 }
34 }
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 }
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()
52 /////////////////////////////////////////////////////////////////////////////
53 // GDBPortDlg message handlers
55 int GDBPortDlg::getPort()
56 {
57 return port;
58 }
60 SOCKET GDBPortDlg::getSocket()
61 {
62 return sock;
63 }
65 BOOL GDBPortDlg::OnInitDialog()
66 {
67 CDialog::OnInitDialog();
69 CString buffer;
71 buffer.Format("%d", port);
73 m_port.SetWindowText(buffer);
75 CenterWindow();
77 return TRUE; // return TRUE unless you set the focus to a control
78 // EXCEPTION: OCX Property Pages should return FALSE
79 }
81 void GDBPortDlg::OnOk()
82 {
83 CString buffer;
85 m_port.GetWindowText(buffer);
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);
93 SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
95 if (s != INVALID_SOCKET)
96 {
97 int error = bind(s, (sockaddr *)&address, sizeof(address));
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 }
128 void GDBPortDlg::OnCancel()
129 {
130 OnClose();
131 }
133 void GDBPortDlg::OnClose()
134 {
135 EndDialog(FALSE);
136 }
138 /////////////////////////////////////////////////////////////////////////////
139 // GDBWaitingDlg dialog
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 }
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 }
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()
165 /////////////////////////////////////////////////////////////////////////////
166 // GDBWaitingDlg message handlers
168 BOOL GDBWaitingDlg::OnInitDialog()
169 {
170 CDialog::OnInitDialog();
172 CString buffer;
174 buffer.Format("%d", port);
176 m_port.SetWindowText(buffer);
178 CenterWindow();
180 int error = WSAAsyncSelect(listenSocket,
181 (HWND)*this,
182 SOCKET_MESSAGE,
183 FD_ACCEPT);
185 return TRUE; // return TRUE unless you set the focus to a control
186 // EXCEPTION: OCX Property Pages should return FALSE
187 }
189 LRESULT GDBWaitingDlg::OnSocketAccept(WPARAM wParam, LPARAM lParam)
190 {
191 if (LOWORD(lParam) == FD_ACCEPT)
192 {
193 WSAAsyncSelect(listenSocket, (HWND)*this, 0, 0);
195 int flag = 0;
196 ioctlsocket(listenSocket, FIONBIO, (unsigned long *)&flag);
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 }
216 return TRUE;
217 }
219 void GDBWaitingDlg::OnCancel()
220 {
221 OnClose();
222 }
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 }
239 SOCKET GDBWaitingDlg::getListenSocket()
240 {
241 return listenSocket;
242 }
244 SOCKET GDBWaitingDlg::getSocket()
245 {
246 return sock;
247 }