home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / GDBConnection.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  5.7 KB  |  262 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. // GDBConnection.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "vba.h"
  24. #include "GDBConnection.h"
  25.  
  26. #include <winsock.h>
  27.  
  28. #define SOCKET_MESSAGE WM_APP+1
  29.  
  30. #ifdef _DEBUG
  31. #define new DEBUG_NEW
  32. #undef THIS_FILE
  33. static char THIS_FILE[] = __FILE__;
  34. #endif
  35.  
  36. static bool initialized = false;
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // GDBPortDlg dialog
  40.  
  41.  
  42. GDBPortDlg::GDBPortDlg(CWnd* pParent /*=NULL*/)
  43.   : CDialog(GDBPortDlg::IDD, pParent)
  44. {
  45.   //{{AFX_DATA_INIT(GDBPortDlg)
  46.   // NOTE: the ClassWizard will add member initialization here
  47.   //}}AFX_DATA_INIT
  48.   port = 55555;
  49.   sock = INVALID_SOCKET;
  50.   
  51.   if(!initialized) {
  52.     WSADATA wsaData;
  53.  
  54.     int error = WSAStartup(MAKEWORD(1,1), &wsaData);
  55.  
  56.     initialized = true;
  57.   }
  58. }
  59.  
  60.  
  61. void GDBPortDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63.   CDialog::DoDataExchange(pDX);
  64.   //{{AFX_DATA_MAP(GDBPortDlg)
  65.   DDX_Control(pDX, IDC_PORT, m_port);
  66.   //}}AFX_DATA_MAP
  67. }
  68.  
  69.  
  70. BEGIN_MESSAGE_MAP(GDBPortDlg, CDialog)
  71.   //{{AFX_MSG_MAP(GDBPortDlg)
  72.   ON_BN_CLICKED(ID_OK, OnOk)
  73.   ON_BN_CLICKED(ID_CANCEL, OnCancel)
  74.   ON_WM_CLOSE()
  75.   //}}AFX_MSG_MAP
  76.   END_MESSAGE_MAP()
  77.  
  78.   /////////////////////////////////////////////////////////////////////////////
  79. // GDBPortDlg message handlers
  80.  
  81. int GDBPortDlg::getPort()
  82. {
  83.   return port;
  84. }
  85.  
  86.  
  87. SOCKET GDBPortDlg::getSocket()
  88. {
  89.   return sock;
  90. }
  91.  
  92.  
  93. BOOL GDBPortDlg::OnInitDialog() 
  94. {
  95.   CDialog::OnInitDialog();
  96.   
  97.   CString buffer;
  98.  
  99.   buffer.Format("%d", port);
  100.  
  101.   m_port.SetWindowText(buffer);
  102.  
  103.   CenterWindow();
  104.   
  105.   return TRUE;  // return TRUE unless you set the focus to a control
  106.                 // EXCEPTION: OCX Property Pages should return FALSE
  107. }
  108.  
  109. void GDBPortDlg::OnOk() 
  110. {
  111.   CString buffer;
  112.  
  113.   m_port.GetWindowText(buffer);
  114.   
  115.   sockaddr_in address;
  116.   address.sin_family = AF_INET;
  117.   address.sin_addr.s_addr = inet_addr("0.0.0.0");
  118.   address.sin_port = htons(atoi(buffer));
  119.   port = ntohs(address.sin_port);
  120.  
  121.   SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
  122.  
  123.   if(s != INVALID_SOCKET) {
  124.     int error = bind(s, (sockaddr *)&address, sizeof(address));
  125.  
  126.     if(error) {
  127.       systemMessage(IDS_ERROR_BINDING, "Error binding socket. Port probably in use.");
  128.       error = closesocket(s);
  129.       EndDialog(FALSE);
  130.     } else {
  131.       error = listen(s, 1);
  132.       if(!error) {
  133.         sock = s;
  134.         EndDialog(TRUE);
  135.       } else {
  136.         systemMessage(IDS_ERROR_LISTENING, "Error listening on socket.");
  137.         closesocket(s);
  138.         EndDialog(FALSE);
  139.       }
  140.     }
  141.   } else {
  142.     systemMessage(IDS_ERROR_CREATING_SOCKET, "Error creating socket.");
  143.     EndDialog(FALSE);
  144.   }
  145. }
  146.  
  147. void GDBPortDlg::OnCancel() 
  148. {
  149.   OnClose();
  150. }
  151.  
  152. void GDBPortDlg::OnClose() 
  153. {
  154.   EndDialog(FALSE);
  155. }
  156. /////////////////////////////////////////////////////////////////////////////
  157. // GDBWaitingDlg dialog
  158.  
  159.  
  160. GDBWaitingDlg::GDBWaitingDlg(SOCKET s, int p, CWnd* pParent /*=NULL*/)
  161.   : CDialog(GDBWaitingDlg::IDD, pParent)
  162. {
  163.   //{{AFX_DATA_INIT(GDBWaitingDlg)
  164.   //}}AFX_DATA_INIT
  165.   port = p & 65535;
  166.   listenSocket = s;
  167. }
  168.  
  169.  
  170. void GDBWaitingDlg::DoDataExchange(CDataExchange* pDX)
  171. {
  172.   CDialog::DoDataExchange(pDX);
  173.   //{{AFX_DATA_MAP(GDBWaitingDlg)
  174.   DDX_Control(pDX, IDC_PORT, m_port);
  175.   //}}AFX_DATA_MAP
  176. }
  177.  
  178.  
  179. BEGIN_MESSAGE_MAP(GDBWaitingDlg, CDialog)
  180.   //{{AFX_MSG_MAP(GDBWaitingDlg)
  181.   ON_BN_CLICKED(ID_CANCEL, OnCancel)
  182.   ON_WM_CLOSE()
  183.   //}}AFX_MSG_MAP
  184.   END_MESSAGE_MAP()
  185.  
  186.   /////////////////////////////////////////////////////////////////////////////
  187. // GDBWaitingDlg message handlers
  188.  
  189. BOOL GDBWaitingDlg::OnInitDialog() 
  190. {
  191.   CDialog::OnInitDialog();
  192.   
  193.   CString buffer;
  194.  
  195.   buffer.Format("%d", port);
  196.  
  197.   m_port.SetWindowText(buffer);
  198.  
  199.   CenterWindow();
  200.  
  201.   int error = WSAAsyncSelect(listenSocket,
  202.                              (HWND )*this,
  203.                              SOCKET_MESSAGE,
  204.                              FD_ACCEPT);
  205.   
  206.   return TRUE;  // return TRUE unless you set the focus to a control
  207.                 // EXCEPTION: OCX Property Pages should return FALSE
  208. }
  209.  
  210. LRESULT GDBWaitingDlg::OnSocketAccept(WPARAM wParam, LPARAM lParam)
  211. {
  212.   if(LOWORD(lParam) == FD_ACCEPT) {
  213.     WSAAsyncSelect(listenSocket, (HWND)*this, 0, 0);
  214.     
  215.     int flag = 0;    
  216.     ioctlsocket(listenSocket, FIONBIO, (unsigned long *)&flag);
  217.     
  218.     SOCKET s = accept(listenSocket, NULL, NULL);
  219.     if(s != INVALID_SOCKET) {
  220.       char dummy;
  221.       recv(s, &dummy, 1, 0);
  222.       if(dummy != '+') {
  223.         systemMessage(IDS_ACK_NOT_RECEIVED, "ACK not received from GDB.");
  224.         OnClose(); // calls EndDialog
  225.       } else {
  226.         sock = s;
  227.         EndDialog(TRUE);
  228.       }
  229.     }
  230.   }
  231.  
  232.   return TRUE;
  233. }
  234.  
  235. void GDBWaitingDlg::OnCancel() 
  236. {
  237.   OnClose();
  238. }
  239.  
  240. void GDBWaitingDlg::OnClose() 
  241. {
  242.   if(sock != INVALID_SOCKET) {
  243.     closesocket(sock);
  244.     sock = INVALID_SOCKET;
  245.   }
  246.   if(listenSocket != INVALID_SOCKET) {
  247.     closesocket(listenSocket);
  248.     listenSocket = INVALID_SOCKET;
  249.   }
  250.   EndDialog(FALSE);
  251. }
  252.  
  253. SOCKET GDBWaitingDlg::getListenSocket()
  254. {
  255.   return listenSocket;
  256. }
  257.  
  258. SOCKET GDBWaitingDlg::getSocket()
  259. {
  260.   return sock;
  261. }
  262.