⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverdoc.cpp

📁 面向软件工程的Visual C++网络程序开发
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// ServerDoc.cpp : implementation of the CServerDoc class
//

#include "stdafx.h"
#include "Server.h"

#include "ServerDoc.h"
#include "ServerView.h"
#include "ParamDlg.h"
#include "mainfrm.h"

#define CHARFLAG "#@"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CServerDoc

IMPLEMENT_DYNCREATE(CServerDoc, CDocument)

BEGIN_MESSAGE_MAP(CServerDoc, CDocument)
	//{{AFX_MSG_MAP(CServerDoc)
	ON_COMMAND(ID_CONFIG, OnConfig)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDoc construction/destruction

CServerDoc::CServerDoc()
{
	// TODO: add one-time construction code here
	m_pSocket = NULL;
	m_pShortMessage = new CMessage();
	m_nPort = 8888;
}

CServerDoc::~CServerDoc()
{
	if(m_pSocket != NULL)
		delete m_pSocket;
	m_pSocket = NULL;
	if(m_pShortMessage != NULL)
	{
		delete m_pShortMessage;
	}

}

BOOL CServerDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	m_pSocket = new CAcceptSocket(this);
	if (m_pSocket->Create(m_nPort)){
		if (m_pSocket->Listen())
			return TRUE;
	}

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CServerDoc serialization

void CServerDoc::Serialize(CArchive& ar)
{
	// CEditView contains an edit control which handles all serialization
	((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CServerDoc diagnostics

#ifdef _DEBUG
void CServerDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CServerDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CServerDoc commands

void CServerDoc::ProcessAccept()
{
	CServiceSocket* pSocket = new CServiceSocket(this);
	//CServiceSocket* pSock;
//	CMainFrame* pwnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;
//	CServerView* pView=(CServerView*)pwnd->GetActiveView();
//	CServerDoc* pDoc=pView->GetDocument();
	if (m_pSocket->Accept(*pSocket))
	{
		
	
		try
		{
			pSocket->Init();
			m_connectionList.AddTail(pSocket);

		}
		catch(...)
		{
			CloseSocket(pSocket);
		}
//		AfxMessageBox("ok");
/*		POSITION pos,temp;
		for(pos = m_connectionList.GetHeadPosition(); pos != NULL;)
		{
			temp = pos;
			CServiceSocket* pSock = (CServiceSocket *)m_connectionList.GetNext(pos);
			if (pSock == pSocket){
				
				m_connectionList.RemoveAt(temp);

				break;
				return;
			}
		}
		pSocket->Init();
		m_connectionList.AddTail(pSocket);
*/
		/*
		CMessage *pMsg;
		pMsg = new CMessage();
		pMsg->ShortMessage = "Come in";
		pMsg->To = "";
		pMsg->From = "";
		pMsg->Type = 1;

		POSITION posname;

		for( posname=m_connectionList.GetHeadPosition();posname;)
		{				
			pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
			if(pSock->Name != pMsg->From  )
			{
				SendMsg(pSock, pMsg);
			}
		}
		delete pMsg;
		*/
	}
	else
		delete pSocket;

}

void CServerDoc::ProcessRecieveMessage(CServiceSocket *pSocket)
{
//	CServiceSocket *pSocketTemp;
	CString m_strAddrTemp;
//	long	m_nPortTemp;
	CServiceSocket *pSock;
	POSITION posname;
	CMessage* pMsg;

	if (pSocket == NULL)
		return;
	try
	{

	do{
		pMsg = ReadMsg(pSocket);
		if(pMsg == NULL) 
			return;
		switch(pMsg->Type)
		{
		case 9999:
//			pSocket->Name = pMsg->From ;
			for( posname=m_connectionList.GetHeadPosition();posname;)
			{				
				pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
				if(pSock==NULL ) 
					continue;
				if (pMsg == NULL) 
					continue;
				if(pSock->Name == pMsg->From )
				{
					pSock->m_bActive=true;
					m_connectionList.SetAt(m_connectionList.Find(pSock) ,pSock);
				}

			}
			break;	
		case 1:
			pSocket->Name = pMsg->From ;
			for( posname=m_connectionList.GetHeadPosition();posname;)
			{				
				pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
				pSock->m_bActive=true;
				m_connectionList.SetAt(m_connectionList.Find(pSock) ,pSock);
				if(pSock->Name != pMsg->From )
				{
					SendMsg(pSock, pMsg);

				}
			}
			break;
		case 2:
//			pMsg->ShortMessage = "Go with the wind ....";
			pMsg->To = "";
			for( posname=m_connectionList.GetHeadPosition();posname;)
			{				
				pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
				if(pSock->Name == pMsg->From )
				{
					CloseSocket(pSock);
//					m_connectionList.RemoveAt(posname);
//					SendMsg(pSock, pMsg);
				}
				else
				{
					SendMsg(pSock, pMsg);
				}
			}
			break;
		
		case 1320:
			for( posname=m_connectionList.GetHeadPosition();posname;)
			{				
				pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
				if(pSock->Name == pMsg->To )
				{
					CMainFrame* pwnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;
					CServerView* pView=(CServerView*)pwnd->GetActiveView();
					StringSeg(pMsg->ShortMessage,59);
					pView->InsertRow("订货合约",m_StrArray);
					pMsg->Type = 2320;
					pMsg->ShortMessage = "success";
					SendMsg(pSock, pMsg);
					break;
				}
			}
			break;
		case 13201:
			for( posname=m_connectionList.GetHeadPosition();posname;)
			{				
				pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
				if(pSock->Name == pMsg->To )
				{
					CMainFrame* pwnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;
					CServerView* pView=(CServerView*)pwnd->GetActiveView();
					
					CString strSelect3;
					CStringArray sResult3[100];
					ULONG ulColCount3;
					strSelect3="select COUNT(编号) from [订货合约]";//change max to count
					if(!pView->OpenCurRecordset(strSelect3)) return ;
					CString ss3[100];
					if(!pView->GetColumn(&ulColCount3,ss3)) return ;
					if(0==ulColCount3) return ;
					pView->RefreshData(ulColCount3,sResult3);
					if(sResult3[0][0]=="")
						sResult3[0][0]="0";
					pMsg->Type = 23201;
					pMsg->ShortMessage = sResult3[0][0];
					SendMsg(pSock, pMsg);
					break;
				}
			}
			break;
		case 1321:
			for( posname=m_connectionList.GetHeadPosition();posname;)
			{				
				pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
				if(pSock->Name == pMsg->To )
				{
					CMainFrame* pwnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;
					CServerView* pView=(CServerView*)pwnd->GetActiveView();

//					StringSeg(pMsg->ShortMessage,2);

					CString strSelect;
					CStringArray sResult[100];
					ULONG ulColCount;
					strSelect = "select * from [订货合约] where 编号 = '" + pMsg->ShortMessage + "'";
					if(!pView->OpenCurRecordset(strSelect)) return ;
					CString ss[100];
					if(!pView->GetColumn(&ulColCount,ss)) return ;
					if(0==ulColCount) return ;
					pView->RefreshData(ulColCount,sResult);

					if(sResult[0][0] == "")
					{
						pMsg->Type = 2321;
						pMsg->ShortMessage = "";
						SendMsg(pSock, pMsg);
						break;
					}
					
					CString strReturn = "";
					strReturn += sResult[2][0];//kehuxingming
					strReturn += CHARFLAG;
					strReturn += sResult[1][0];//xiaoshoubumen
					strReturn += CHARFLAG;

					int n = 0;
					for(int i = 0; i < 4; i++)
					{
						if(sResult[n+6][0] != "")
						{
							CStringArray sResultShangjia1[100];
							CString ss1[100];
							CString strtmp = sResult[n+6][0];
							strSelect = "select * from [商品基本资料] where 商品编码 = '" + strtmp + "'";
							if(!pView->OpenCurRecordset(strSelect)) return ;
							if(!pView->GetColumn(&ulColCount,ss1)) return ;
							if(0==ulColCount) return ;
							pView->RefreshData(ulColCount,sResultShangjia1);
							strReturn += sResult[n+6][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+7][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+9][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+8][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+10][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+12][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+14][0];
							strReturn += CHARFLAG;
							strReturn += sResultShangjia1[5][0];
							strReturn += CHARFLAG;
							strReturn += sResultShangjia1[6][0];
							strReturn += CHARFLAG;
						}
						else
						{
							strReturn += sResult[n+6][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+7][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+9][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+8][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+10][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+12][0];
							strReturn += CHARFLAG;
							strReturn += sResult[n+14][0];
							strReturn += CHARFLAG;
							strReturn += "";
							strReturn += CHARFLAG;
							strReturn += "";
							strReturn += CHARFLAG;

						}
						n += 11;
					}
					strReturn += sResult[52][0];//yifudingjin
					strReturn += CHARFLAG;
					strReturn += sResult[51][0];//heji
					strReturn += CHARFLAG;
					strReturn += sResult[57][0];//yingyeyuan
					strReturn += CHARFLAG;

					pMsg->Type = 2321;
					pMsg->ShortMessage = strReturn;
					SendMsg(pSock, pMsg);
						
					break;
				}
			}
			break;
		case 13221:
			for( posname=m_connectionList.GetHeadPosition();posname;)
			{				
				pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
				if(pSock->Name == pMsg->To )
				{
					CMainFrame* pwnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;
					CServerView* pView=(CServerView*)pwnd->GetActiveView();
					
					CString strSelect3;
					CStringArray sResult3[100];
					ULONG ulColCount3;
					strSelect3="select MAX(CAST(收款凭据编号 AS INT(20))) from [收款凭据]";//change max to count
					if(!pView->OpenCurRecordset(strSelect3)) return ;
					CString ss3[100];
					if(!pView->GetColumn(&ulColCount3,ss3)) return ;
					if(0==ulColCount3) return ;
					pView->RefreshData(ulColCount3,sResult3);
					if(sResult3[0][0]=="")
						sResult3[0][0]="0";
					pMsg->Type = 23221;
					pMsg->ShortMessage = sResult3[0][0];
					SendMsg(pSock, pMsg);
					break;
				}
			}
			break;
		case 1322:
			for( posname=m_connectionList.GetHeadPosition();posname;)
			{				
				pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
				if(pSock->Name == pMsg->To )
				{
					CMainFrame* pwnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;
					CServerView* pView=(CServerView*)pwnd->GetActiveView();
//					AfxMessageBox(pMsg->ShortMessage);
					StringSeg(pMsg->ShortMessage);

					CStringArray saTmp;
					for(int i = 0; i < m_iRow; i++)
					{
						for(int j = 0; j < m_iCol; j++)
						{
							saTmp.Add(m_StrArray.GetAt(j+i*m_iCol));
						}
						pView->InsertRow("收款凭据",saTmp);
						saTmp.RemoveAll();
					}
					
					pMsg->Type = 2322;
					pMsg->ShortMessage = "success";
					SendMsg(pSock, pMsg);
					break;
				}
			}
			break;
		case 13111:
			for( posname=m_connectionList.GetHeadPosition();posname;)
			{				
				pSock = (CServiceSocket *)m_connectionList.GetNext(posname);
				if(pSock->Name == pMsg->To )
				{
					CMainFrame* pwnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;
					CServerView* pView=(CServerView*)pwnd->GetActiveView();
					

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -