Serial comms: compilation in Linux (PR #1249)

* SerialComms: compilation in linux.
1) static variables must be defined (otherwise it becomes optimisation-dependent)
2) ensure the socket is closed if an error is detected

* SerialComms: avoid issues with order of global variables.
GetFrame() and the GetCardMgr() use global variables and the order of their destructor is not well defined.
CSuperSerialCard::CommTcpSerialCleanup() is called as part of the CardManager's destructor.
This commit is contained in:
Andrea 2024-01-06 14:50:10 +00:00 committed by GitHub
parent 7497aa9923
commit 4ab60cebf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View File

@ -47,6 +47,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#define TCP_SERIAL_PORT 1977 #define TCP_SERIAL_PORT 1977
const UINT CSuperSerialCard::SERIALPORTITEM_INVALID_COM_PORT = 0;
// Default: 9600-8-N-1 // Default: 9600-8-N-1
SSC_DIPSW CSuperSerialCard::m_DIPSWDefault = SSC_DIPSW CSuperSerialCard::m_DIPSWDefault =
{ {
@ -69,7 +71,8 @@ CSuperSerialCard::CSuperSerialCard(UINT slot) :
m_strSerialPortChoices(1, '\0'), // Combo box friendly, just in case. m_strSerialPortChoices(1, '\0'), // Combo box friendly, just in case.
m_uTCPChoiceItemIdx(0), m_uTCPChoiceItemIdx(0),
m_bCfgSupportDCD(false), m_bCfgSupportDCD(false),
m_pExpansionRom(NULL) m_pExpansionRom(NULL),
m_hFrameWindow(NULL)
{ {
if (m_slot != 2) // fixme if (m_slot != 2) // fixme
ThrowErrorInvalidSlot(); ThrowErrorInvalidSlot();
@ -241,6 +244,7 @@ bool CSuperSerialCard::CheckComm()
saAddress.sin_addr.s_addr = htonl(INADDR_ANY); saAddress.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(m_hCommListenSocket, (LPSOCKADDR)&saAddress, sizeof(saAddress)) == SOCKET_ERROR) if (bind(m_hCommListenSocket, (LPSOCKADDR)&saAddress, sizeof(saAddress)) == SOCKET_ERROR)
{ {
closesocket(m_hCommListenSocket);
m_hCommListenSocket = INVALID_SOCKET; m_hCommListenSocket = INVALID_SOCKET;
WSACleanup(); WSACleanup();
return false; return false;
@ -249,18 +253,21 @@ bool CSuperSerialCard::CheckComm()
// bound, so listen // bound, so listen
if (listen(m_hCommListenSocket, 1) == SOCKET_ERROR) if (listen(m_hCommListenSocket, 1) == SOCKET_ERROR)
{ {
closesocket(m_hCommListenSocket);
m_hCommListenSocket = INVALID_SOCKET; m_hCommListenSocket = INVALID_SOCKET;
WSACleanup(); WSACleanup();
return false; return false;
} }
// now send async events to our app's message handler // now send async events to our app's message handler
m_hFrameWindow = GetFrame().g_hFrameWindow;
if (WSAAsyncSelect( if (WSAAsyncSelect(
/* SOCKET s */ m_hCommListenSocket, /* SOCKET s */ m_hCommListenSocket,
/* HWND hWnd */ GetFrame().g_hFrameWindow, /* HWND hWnd */ m_hFrameWindow,
/* unsigned int wMsg */ WM_USER_TCP_SERIAL, /* unsigned int wMsg */ WM_USER_TCP_SERIAL,
/* long lEvent */ (FD_ACCEPT | FD_CONNECT | FD_READ | FD_CLOSE)) != 0) /* long lEvent */ (FD_ACCEPT | FD_CONNECT | FD_READ | FD_CLOSE)) != 0)
{ {
closesocket(m_hCommListenSocket);
m_hCommListenSocket = INVALID_SOCKET; m_hCommListenSocket = INVALID_SOCKET;
WSACleanup(); WSACleanup();
return false; return false;
@ -328,7 +335,7 @@ void CSuperSerialCard::CommTcpSerialCleanup()
{ {
if (m_hCommListenSocket != INVALID_SOCKET) if (m_hCommListenSocket != INVALID_SOCKET)
{ {
WSAAsyncSelect(m_hCommListenSocket, GetFrame().g_hFrameWindow, 0, 0); // Stop event messages WSAAsyncSelect(m_hCommListenSocket, m_hFrameWindow, 0, 0); // Stop event messages
closesocket(m_hCommListenSocket); closesocket(m_hCommListenSocket);
m_hCommListenSocket = INVALID_SOCKET; m_hCommListenSocket = INVALID_SOCKET;

View File

@ -88,7 +88,7 @@ private:
std::string m_currentSerialPortName; std::string m_currentSerialPortName;
DWORD m_dwSerialPortItem; DWORD m_dwSerialPortItem;
static const UINT SERIALPORTITEM_INVALID_COM_PORT = 0; static const UINT SERIALPORTITEM_INVALID_COM_PORT;
std::vector<UINT> m_vecSerialPortsItems; // Includes "None" & "TCP" items std::vector<UINT> m_vecSerialPortsItems; // Includes "None" & "TCP" items
std::string m_strSerialPortChoices; std::string m_strSerialPortChoices;
UINT m_uTCPChoiceItemIdx; UINT m_uTCPChoiceItemIdx;
@ -111,6 +111,7 @@ private:
HANDLE m_hCommHandle; HANDLE m_hCommHandle;
SOCKET m_hCommListenSocket; SOCKET m_hCommListenSocket;
SOCKET m_hCommAcceptSocket; SOCKET m_hCommAcceptSocket;
HWND m_hFrameWindow; // to avoid variable (de)-initialisation order issues
// //