// Server.cpp : implementation file // #include "stdafx.h" #include "dicomsrv.h" #include "Server.h" #include "SrvDlg.h" #include "client.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif class CDICOMSRVApp; extern CDICOMSRVApp theApp; ///////////////////////////////////////////////////////////////////////////// // CNetServer CNetServer::CNetServer() : LDicomNet((L_CHAR*)(LPCTSTR) theApp.m_sTempFilesFolder, DICOM_SECURE_NONE) { } CNetServer::~CNetServer() { while(GetClientCount() > 0) { CNetClient *pClient=NULL; pClient = (CNetClient*)GetClient(0); if(pClient) { pClient->Close(); delete pClient; pClient = NULL; } } Close(); } ///////////////////////////////////////////////////////////////////////////// // Events L_VOID CNetServer::OnAccept(L_INT nError) { CNetClient* pClient = NULL; HTREEITEM hItem=NULL; HTREEITEM hChild=NULL; CDICOMSRVDlg *pDlg=(CDICOMSRVDlg *)AfxGetMainWnd(); L_UINT32 nMode = pDlg->m_Security; if(nError != DICOM_SUCCESS) return; pClient = new CNetClient(nMode); if (!pClient) { pDlg->LogEvent("", "Connection Rejected - Out of Memory"); return; } /* if (pClient->m_nValid != DICOM_SUCCESS) { L_CHAR *pszError = CNetClient::GetTLSErrorString(pClient->m_nValid); pDlg->LogEvent("", pszError); delete pClient; return; } */ pClient->LoadDefaultSettings(); CClientData* pClientData = new CClientData(); if (!pClientData) { delete pClient; pDlg->LogEvent("", "Connection Rejected - Out of Memory"); return; } // Accept the connection attempt nError = Accept(pClient); if (nError != DICOM_SUCCESS) { delete pClient; delete pClientData; pDlg->LogEvent("", "Connection Rejected - Error during Accept"); return; } // *** if (pClient->m_nValid != DICOM_SUCCESS) { L_CHAR *pszError = CNetClient::GetTLSErrorString(pClient->m_nValid); pDlg->LogEvent("", pszError); pClient->Close(); delete pClient; delete pClientData; return; } L_CHAR szPeerAddress[200]; pClient->GetPeerInfo(szPeerAddress, NULL); CString sIPAddress = szPeerAddress; if (GetClientCount() > L_UINT32(theApp.m_nMaxClients)) // Too many connections! { pClient->Close(); delete pClient; delete pClientData; pDlg->LogEvent(sIPAddress, "Connection Rejected - Max Connections Reached"); return; } if (!pDlg->FindClient(sIPAddress)) { // We don't know this user; close the connection pClient->Close(); delete pClient; delete pClientData; pDlg->LogEvent(sIPAddress, "Connection Rejected - Unknown User"); return; } pDlg->LogEvent(sIPAddress, "Connection Accepted"); // Display some information about the connection: CString sOut; CTime theTime = CTime::GetCurrentTime(); CString sDate = theTime.Format("%m/%d/%Y %I:%M:%S %p"); sOut.Format("0x%X", pClient); hItem = pDlg->m_ConnectionList.InsertItem(sOut, 0, 0); pClientData->m_pClient = pClient; pDlg->m_ConnectionList.SetItemData(hItem, (DWORD) pClientData); hChild = pDlg->m_ConnectionList.InsertItem("Remote AE: ", 2, 2, hItem); hChild = pDlg->m_ConnectionList.InsertItem("Remote Host: " + sIPAddress, 2, 2, hItem); hChild = pDlg->m_ConnectionList.InsertItem("Connection Time: " + sDate, 2, 2, hItem); hChild = pDlg->m_ConnectionList.InsertItem("Number of Messages: 0", 2, 2, hItem); pDlg->m_ConnectionList.SetItemData(hChild, 0); hChild = pDlg->m_ConnectionList.InsertItem("Last Action: Connect", 2, 2, hItem); if (GetClientCount() == 1) { pDlg->SetTimer(CLIENTS_TIMER_ID, 1000, NULL); } } L_VOID CNetServer::OnClose(L_INT nError, LDicomNet* pClient) { CDICOMSRVDlg* pDlg = (CDICOMSRVDlg*) AfxGetMainWnd(); CClientData* pClientData; // A client has closed HTREEITEM hItem = pDlg->m_ConnectionList.GetRootItem(); while (hItem) { pClientData = (CClientData*) pDlg->m_ConnectionList.GetItemData(hItem); if ((LDicomNet*) pClientData->m_pClient == pClient) { pDlg->m_ConnectionList.DeleteItem(hItem); delete pClientData; break; } hItem = pDlg->m_ConnectionList.GetNextItem(hItem, TVGN_NEXT); } } 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); }; 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); }