// Server.cpp: implementation of the CNetServer class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "netapp.h" #include "netdlg.h" #include "Server.h" #include "Client.h" extern CNetApp theApp; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CNetServer::CNetServer(L_CHAR* pszPath) : LDicomNet(pszPath, DICOM_SECURE_NONE) { m_strTitle = ""; m_bAnonymous = FALSE; m_pAccept = NULL; m_nCount = 0; } CNetServer::~CNetServer() { Close(NULL); } ////////////////////////////////////////////////////////////////////// // Utilities ////////////////////////////////////////////////////////////////////// L_INT CNetServer::Listen(CString strTitle, L_UINT nPort, L_INT nNbClients, L_BOOL bAnonymous, CNetAccept *pAccept, L_INT nCount) { L_INT i; L_INT nResult; Close(NULL); m_strTitle = strTitle; m_bAnonymous = bAnonymous; m_nCount = nCount; if (m_nCount != 0) { m_pAccept = new CNetAccept[m_nCount]; if (m_pAccept == NULL) { m_nCount = 0; DisplayMessage("Listen", DICOM_ERROR_MEMORY, TVI_ROOT); return DICOM_ERROR_MEMORY; } } for (i = 0; i < m_nCount; i++) { m_pAccept[i].m_strTitle = pAccept[i].m_strTitle; m_pAccept[i].m_strAddress = pAccept[i].m_strAddress; m_pAccept[i].m_strMask = pAccept[i].m_strMask; m_pAccept[i].m_nPort = pAccept[i].m_nPort; m_pAccept[i].m_nVerify = pAccept[i].m_nVerify; } nResult = LDicomNet::Listen(NULL, nPort, nNbClients); if (nResult != DICOM_SUCCESS) { DisplayMessage("Listen", nResult, TVI_ROOT); Close(NULL); } DisplayMessage("Listen", DICOM_SUCCESS, TVI_ROOT); return DICOM_SUCCESS; } L_VOID CNetServer::Close(CNetClient *pClient) { if (pClient != NULL) { delete pClient; } else { while (GetClientCount() != 0) { pClient = (CNetClient *)GetClient(0); if (pClient != NULL) { delete pClient; } } LDicomNet::Close(); } if (m_pAccept != NULL) { delete []m_pAccept; m_pAccept = NULL; m_nCount = 0; } } L_INT CNetServer::Verify(CString strServerTitle, CString strClientAddress, CString strClientTitle, L_BOOL bTitle) { L_INT i; L_UCHAR nAddress[4]; L_UCHAR nMask[4]; CString strMask; CString strTemp; if (m_bAnonymous != FALSE) { return DICOM_SUCCESS; } if (ConvertAddress(strClientAddress, nAddress) == FALSE) { return DICOM_ERROR_USER_CLIENT_ADDRESS; } for (i = 0; i < m_nCount; i++) { if (m_pAccept[i].m_strAddress != "") { if (strClientAddress == m_pAccept[i].m_strAddress) { break; } } else if (m_pAccept[i].m_strMask != "") { if (ConvertAddress(m_pAccept[i].m_strMask, nMask) != FALSE) { if ((nAddress[0] & nMask[0]) && (nAddress[1] & nMask[1]) && (nAddress[2] & nMask[2]) && (nAddress[3] & nMask[3])) { break; } } } } if (i < m_nCount) { if ((m_pAccept[i].m_nVerify & ACCEPT_VERIFY_TITLE) && (bTitle != FALSE)) { if (strServerTitle != m_strTitle) { return DICOM_ERROR_USER_SERVER_TITLE; } if (strClientTitle != m_pAccept[i].m_strTitle) { return DICOM_ERROR_USER_CLIENT_TITLE; } } if (m_pAccept[i].m_nVerify & ACCEPT_VERIFY_ADDRESS) { if (strClientAddress != m_pAccept[i].m_strAddress) { return DICOM_ERROR_USER_CLIENT_ADDRESS; } } } else { return DICOM_ERROR_USER_CLIENT_ADDRESS; } return DICOM_SUCCESS; } CString CNetServer::GetTitle() const { return m_strTitle; } L_BOOL CNetServer::ConvertAddress(CString strAddress, L_UCHAR *nAddress) { L_INT i, j; CString strTemp; j = 0; while ((strAddress != "") && (j < 4)) { i = strAddress.Find('.'); if (i != -1) { strTemp = strAddress.Left(i); strAddress = strAddress.Right(strAddress.GetLength() - i - 1); } else { strTemp = strAddress; strAddress = ""; } nAddress[j++] = atoi(strTemp); } return (j == 4) ? TRUE : FALSE; } HTREEITEM CNetServer::DisplayMessage(CString strText, L_INT nError, HTREEITEM hParent) { CString strError=""; if (theApp.m_pDlg == NULL) { return NULL; } if (nError != -1) { theApp.m_pDlg->ConvertError(nError, strError); strError = ": " + strError; } hParent = theApp.m_pDlg->m_Message.InsertItem(strText+strError, hParent, TVI_LAST); theApp.m_pDlg->m_Message.RedrawWindow(); theApp.m_pDlg->m_Message.SelectItem(hParent); return hParent; } ////////////////////////////////////////////////////////////////////// // Events ////////////////////////////////////////////////////////////////////// L_VOID CNetServer::OnAccept(L_INT nError) { CNetClient *pClient; if (nError != DICOM_SUCCESS) { DisplayMessage("OnAccept", nError, TVI_ROOT); return; } pClient = new CNetClient((L_CHAR*)(LPCTSTR) theApp.m_pDlg->m_strTempFilesFolder); if (pClient == NULL) { DisplayMessage("OnAccept", DICOM_ERROR_MEMORY, TVI_ROOT); return; } nError = LDicomNet::Accept(pClient); if (nError != DICOM_SUCCESS) { DisplayMessage("Accept", nError, TVI_ROOT); delete pClient; return; } nError = pClient->Accept(); if (nError != DICOM_SUCCESS) { delete pClient; return; } } L_VOID CNetServer::OnClose(L_INT nError, LDicomNet *pClient) { pClient->OnClose(nError, pClient); delete (CNetClient *)pClient; } L_VOID CNetServer::OnSecureLinkReady(L_UINT32 nError) { CString str; L_CHAR authdata[128]; memset(authdata, 0, 128); L_UINT32 n = 128; GetPeerAuthDataISCL(authdata, &n); str.Format("Connected ISCL with peer identified by %s", authdata); // AfxMessageBox(str); }; L_VOID CNetServer::OnReceivedISCLPacket(L_INT nError, L_CHAR *pBuffer, L_UINT32 nBytes) { CString str, total; if(nError != 0) { str.Format("Error is: %d\n", nError); AfxMessageBox(str); // LDicomNet::OnReceivedISCLPacket(nError, pBuffer, nBytes); return; } str.Format("Recv ISCL packet, msg id: 0x%X\n", *((L_UINT32 *)&(pBuffer[4]))); total += str; str.Format("data length: %d\n", *((L_UINT32 *)&(pBuffer[8]))); total += str; str.Format("option: %d", *((L_UINT32 *)&(pBuffer[12]))); total += str; // AfxMessageBox(total); LDicomNet::OnReceivedISCLPacket(nError, pBuffer, nBytes); }