diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/GDBConnection.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,248 @@
     1.4 +// GDBConnection.cpp : implementation file
     1.5 +//
     1.6 +
     1.7 +#include "stdafx.h"
     1.8 +#include "resource.h"
     1.9 +#include "GDBConnection.h"
    1.10 +
    1.11 +#include "../common/System.h"
    1.12 +
    1.13 +#define SOCKET_MESSAGE WM_APP+1
    1.14 +
    1.15 +static bool initialized = false;
    1.16 +
    1.17 +/////////////////////////////////////////////////////////////////////////////
    1.18 +// GDBPortDlg dialog
    1.19 +
    1.20 +GDBPortDlg::GDBPortDlg(CWnd*pParent /*=NULL*/)
    1.21 +	: CDialog(GDBPortDlg::IDD, pParent)
    1.22 +{
    1.23 +	//{{AFX_DATA_INIT(GDBPortDlg)
    1.24 +	// NOTE: the ClassWizard will add member initialization here
    1.25 +	//}}AFX_DATA_INIT
    1.26 +	port = 55555;
    1.27 +	sock = INVALID_SOCKET;
    1.28 +
    1.29 +	if (!initialized)
    1.30 +	{
    1.31 +		WSADATA wsaData;
    1.32 +
    1.33 +		int error = WSAStartup(MAKEWORD(1, 1), &wsaData);
    1.34 +
    1.35 +		initialized = true;
    1.36 +	}
    1.37 +}
    1.38 +
    1.39 +void GDBPortDlg::DoDataExchange(CDataExchange*pDX)
    1.40 +{
    1.41 +	CDialog::DoDataExchange(pDX);
    1.42 +	//{{AFX_DATA_MAP(GDBPortDlg)
    1.43 +	DDX_Control(pDX, IDC_PORT, m_port);
    1.44 +	//}}AFX_DATA_MAP
    1.45 +}
    1.46 +
    1.47 +BEGIN_MESSAGE_MAP(GDBPortDlg, CDialog)
    1.48 +//{{AFX_MSG_MAP(GDBPortDlg)
    1.49 +ON_BN_CLICKED(ID_OK, OnOk)
    1.50 +ON_BN_CLICKED(ID_CANCEL, OnCancel)
    1.51 +ON_WM_CLOSE()
    1.52 +//}}AFX_MSG_MAP
    1.53 +END_MESSAGE_MAP()
    1.54 +
    1.55 +/////////////////////////////////////////////////////////////////////////////
    1.56 +// GDBPortDlg message handlers
    1.57 +
    1.58 +int GDBPortDlg::getPort()
    1.59 +{
    1.60 +	return port;
    1.61 +}
    1.62 +
    1.63 +SOCKET GDBPortDlg::getSocket()
    1.64 +{
    1.65 +	return sock;
    1.66 +}
    1.67 +
    1.68 +BOOL GDBPortDlg::OnInitDialog()
    1.69 +{
    1.70 +	CDialog::OnInitDialog();
    1.71 +
    1.72 +	CString buffer;
    1.73 +
    1.74 +	buffer.Format("%d", port);
    1.75 +
    1.76 +	m_port.SetWindowText(buffer);
    1.77 +
    1.78 +	CenterWindow();
    1.79 +
    1.80 +	return TRUE; // return TRUE unless you set the focus to a control
    1.81 +	             // EXCEPTION: OCX Property Pages should return FALSE
    1.82 +}
    1.83 +
    1.84 +void GDBPortDlg::OnOk()
    1.85 +{
    1.86 +	CString buffer;
    1.87 +
    1.88 +	m_port.GetWindowText(buffer);
    1.89 +
    1.90 +	sockaddr_in address;
    1.91 +	address.sin_family      = AF_INET;
    1.92 +	address.sin_addr.s_addr = inet_addr("0.0.0.0");
    1.93 +	address.sin_port        = htons(atoi(buffer));
    1.94 +	port = ntohs(address.sin_port);
    1.95 +
    1.96 +	SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
    1.97 +
    1.98 +	if (s != INVALID_SOCKET)
    1.99 +	{
   1.100 +		int error = bind(s, (sockaddr *)&address, sizeof(address));
   1.101 +
   1.102 +		if (error)
   1.103 +		{
   1.104 +			systemMessage(IDS_ERROR_BINDING, "Error binding socket. Port probably in use.");
   1.105 +			error = closesocket(s);
   1.106 +			EndDialog(FALSE);
   1.107 +		}
   1.108 +		else
   1.109 +		{
   1.110 +			error = listen(s, 1);
   1.111 +			if (!error)
   1.112 +			{
   1.113 +				sock = s;
   1.114 +				EndDialog(TRUE);
   1.115 +			}
   1.116 +			else
   1.117 +			{
   1.118 +				systemMessage(IDS_ERROR_LISTENING, "Error listening on socket.");
   1.119 +				closesocket(s);
   1.120 +				EndDialog(FALSE);
   1.121 +			}
   1.122 +		}
   1.123 +	}
   1.124 +	else
   1.125 +	{
   1.126 +		systemMessage(IDS_ERROR_CREATING_SOCKET, "Error creating socket.");
   1.127 +		EndDialog(FALSE);
   1.128 +	}
   1.129 +}
   1.130 +
   1.131 +void GDBPortDlg::OnCancel()
   1.132 +{
   1.133 +	OnClose();
   1.134 +}
   1.135 +
   1.136 +void GDBPortDlg::OnClose()
   1.137 +{
   1.138 +	EndDialog(FALSE);
   1.139 +}
   1.140 +
   1.141 +/////////////////////////////////////////////////////////////////////////////
   1.142 +// GDBWaitingDlg dialog
   1.143 +
   1.144 +GDBWaitingDlg::GDBWaitingDlg(SOCKET s, int p, CWnd*pParent /*=NULL*/)
   1.145 +	: CDialog(GDBWaitingDlg::IDD, pParent)
   1.146 +{
   1.147 +	//{{AFX_DATA_INIT(GDBWaitingDlg)
   1.148 +	//}}AFX_DATA_INIT
   1.149 +	port         = p & 65535;
   1.150 +	listenSocket = s;
   1.151 +}
   1.152 +
   1.153 +void GDBWaitingDlg::DoDataExchange(CDataExchange*pDX)
   1.154 +{
   1.155 +	CDialog::DoDataExchange(pDX);
   1.156 +	//{{AFX_DATA_MAP(GDBWaitingDlg)
   1.157 +	DDX_Control(pDX, IDC_PORT, m_port);
   1.158 +	//}}AFX_DATA_MAP
   1.159 +}
   1.160 +
   1.161 +BEGIN_MESSAGE_MAP(GDBWaitingDlg, CDialog)
   1.162 +//{{AFX_MSG_MAP(GDBWaitingDlg)
   1.163 +ON_BN_CLICKED(ID_CANCEL, OnCancel)
   1.164 +ON_WM_CLOSE()
   1.165 +//}}AFX_MSG_MAP
   1.166 +END_MESSAGE_MAP()
   1.167 +
   1.168 +/////////////////////////////////////////////////////////////////////////////
   1.169 +// GDBWaitingDlg message handlers
   1.170 +
   1.171 +BOOL GDBWaitingDlg::OnInitDialog()
   1.172 +{
   1.173 +	CDialog::OnInitDialog();
   1.174 +
   1.175 +	CString buffer;
   1.176 +
   1.177 +	buffer.Format("%d", port);
   1.178 +
   1.179 +	m_port.SetWindowText(buffer);
   1.180 +
   1.181 +	CenterWindow();
   1.182 +
   1.183 +	int error = WSAAsyncSelect(listenSocket,
   1.184 +	                           (HWND)*this,
   1.185 +	                           SOCKET_MESSAGE,
   1.186 +	                           FD_ACCEPT);
   1.187 +
   1.188 +	return TRUE; // return TRUE unless you set the focus to a control
   1.189 +	             // EXCEPTION: OCX Property Pages should return FALSE
   1.190 +}
   1.191 +
   1.192 +LRESULT GDBWaitingDlg::OnSocketAccept(WPARAM wParam, LPARAM lParam)
   1.193 +{
   1.194 +	if (LOWORD(lParam) == FD_ACCEPT)
   1.195 +	{
   1.196 +		WSAAsyncSelect(listenSocket, (HWND)*this, 0, 0);
   1.197 +
   1.198 +		int flag = 0;
   1.199 +		ioctlsocket(listenSocket, FIONBIO, (unsigned long *)&flag);
   1.200 +
   1.201 +		SOCKET s = accept(listenSocket, NULL, NULL);
   1.202 +		if (s != INVALID_SOCKET)
   1.203 +		{
   1.204 +			char dummy;
   1.205 +			recv(s, &dummy, 1, 0);
   1.206 +			if (dummy != '+')
   1.207 +			{
   1.208 +				systemMessage(IDS_ACK_NOT_RECEIVED, "ACK not received from GDB.");
   1.209 +				OnClose(); // calls EndDialog
   1.210 +			}
   1.211 +			else
   1.212 +			{
   1.213 +				sock = s;
   1.214 +				EndDialog(TRUE);
   1.215 +			}
   1.216 +		}
   1.217 +	}
   1.218 +
   1.219 +	return TRUE;
   1.220 +}
   1.221 +
   1.222 +void GDBWaitingDlg::OnCancel()
   1.223 +{
   1.224 +	OnClose();
   1.225 +}
   1.226 +
   1.227 +void GDBWaitingDlg::OnClose()
   1.228 +{
   1.229 +	if (sock != INVALID_SOCKET)
   1.230 +	{
   1.231 +		closesocket(sock);
   1.232 +		sock = INVALID_SOCKET;
   1.233 +	}
   1.234 +	if (listenSocket != INVALID_SOCKET)
   1.235 +	{
   1.236 +		closesocket(listenSocket);
   1.237 +		listenSocket = INVALID_SOCKET;
   1.238 +	}
   1.239 +	EndDialog(FALSE);
   1.240 +}
   1.241 +
   1.242 +SOCKET GDBWaitingDlg::getListenSocket()
   1.243 +{
   1.244 +	return listenSocket;
   1.245 +}
   1.246 +
   1.247 +SOCKET GDBWaitingDlg::getSocket()
   1.248 +{
   1.249 +	return sock;
   1.250 +}
   1.251 +