XEIM飞鸽传书部分源码_linuc实现飞鸽传书建立连接-程序员宅基地

技术标签: dialog  image  null  socket  delete  command  

// xeimDlg.cpp : implementation file
//

#include "stdafx.h"
#include "xeim.h"
#include "xeimDlg.h"
#include "XEIM_Message.h"
#include "XEIM_User.h"

#include <sstream>

using namespace std;

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

/
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
 CAboutDlg();

// Dialog Data
 //{ {AFX_DATA(CAboutDlg)
 enum { IDD = IDD_ABOUTBOX };
 //}}AFX_DATA

 // ClassWizard generated virtual function overrides
 //{ {AFX_VIRTUAL(CAboutDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 //}}AFX_VIRTUAL

// Implementation
protected:
 //{ {AFX_MSG(CAboutDlg)
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
 //{ {AFX_DATA_INIT(CAboutDlg)
 //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{ {AFX_DATA_MAP(CAboutDlg)
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
 //{ {AFX_MSG_MAP(CAboutDlg)
  // No message handlers
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CXeimDlg dialog

CXeimDlg::CXeimDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CXeimDlg::IDD, pParent)
{
 //{ {AFX_DATA_INIT(CXeimDlg)
 m_Adress = _T("");
 m_MsgPerSec = 0;
 //}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CXeimDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{ {AFX_DATA_MAP(CXeimDlg)
 DDX_Control(pDX, IDC_CLIENTLIST, m_CtrlClientList);
 DDX_Control(pDX, IDC_EDIT1, m_CtrlLogg);
 DDX_Text(pDX, IDC_ADRESS, m_Adress);
 DDX_Text(pDX, IDC_MSGPERSEC, m_MsgPerSec);
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CXeimDlg, CDialog)
 //{ {AFX_MSG_MAP(CXeimDlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(IDC_STOPSTART, OnStopstart)
 ON_WM_TIMER()
 ON_WM_DESTROY()
 ON_NOTIFY(NM_CLICK, IDC_CLIENTLIST, OnClickClientlist)
 ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
 ON_BN_CLICKED(IDC_DISCONNECTALL, OnDisconnectall)
 ON_BN_CLICKED(IDC_CLEARBANNED, OnClearBanned)
 ON_MESSAGE(WM_LOGG_APPEND, OnAppendLog)
 ON_MESSAGE(WM_NEW_CONNECTION, OnNewClient)
 ON_MESSAGE(WM_CLIENTDISCONNECTED, OnClientDisconnected)
 ON_MESSAGE(WM_USER_DATA, OnUserData)
 ON_BN_CLICKED(IDC_Settings, OnSettings)
 ON_COMMAND(ID_TRAYRESTORE, OnTrayRestore)
 ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
 ON_COMMAND(ID_TRAYQUIT, OnCancel)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CXeimDlg message handlers

BOOL CXeimDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 // Add "About..." menu item to system menu.

 // IDM_ABOUTBOX must be in the system command range.
 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 ASSERT(IDM_ABOUTBOX < 0xF000);

 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
 {
  CString strAboutMenu;
  strAboutMenu.LoadString(IDS_ABOUTBOX);
  if (!strAboutMenu.IsEmpty())
  {
   pSysMenu->AppendMenu(MF_SEPARATOR);
   pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  }
 }

 // Set the icon for this dialog.  The framework does this automatically
 //  when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE);   // Set big icon
 SetIcon(m_hIcon, FALSE);  // Set small icon

 // TODO: Add extra initialization here
 m_CtrlClientList.init();

 StartTheSystem();

 // Fungerar inte som du vill.
 m_Adress=m_iocp.GetHostIP();
 // Start The timer..
 m_ihTimer=SetTimer(0,500,NULL);

 DisableClientPanel();

 if (m_database.IsConnected())
 {
//  m_xUser.ReadUsers();
  m_CtrlLogg.AppendString("数据库访问正常!");
 }
 else
 {
  m_CtrlLogg.AppendString("找不到数据库!");
 }

 UpdateData(FALSE);

 // 系统托盘图标创建------------------------------------------------------------------
 char buf[512];
 LoadString(NULL, IDS_STRING_TITLE, buf, 512);
 if (!m_sysTray.Create(this, WM_ICON_NOTIFY, buf, m_hIcon, IDR_SYSTEMTRAY))
 {
  MessageBox("System tray creating error.");
  return;
 }
 return TRUE;  // return TRUE  unless you set the focus to a control
}

void CXeimDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if (SC_MINIMIZE == nID)
 {
  ShowWindow(SW_HIDE);
  m_sysTray.ShowIcon();
  return;
 }
 else if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
 else
 {
  CDialog::OnSysCommand(nID, lParam);
 }
}
LRESULT CXeimDlg::OnTrayNotification(WPARAM wParam, LPARAM lParam)
{
 // Delegate all the work back to the default implementation in CSystemTray.
 return m_sysTray.OnTrayNotification(wParam, lParam); 
}

void CXeimDlg::OnTrayRestore()
{
 ShowWindow(SW_SHOW);
/* if (m_pAdv)
 {
  if (::IsWindow(m_pAdv->GetSafeHwnd()))
   m_pAdv->UpdateWindow();
 }*/
// m_sysTray.HideIcon();
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CXeimDlg::OnPaint()
{
 if (IsIconic())
 {
  CPaintDC dc(this); // device context for painting

  SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  // Center icon in client rectangle
  int cxIcon = GetSystemMetrics(SM_CXICON);
  int cyIcon = GetSystemMetrics(SM_CYICON);
  CRect rect;
  GetClientRect(&rect);
  int x = (rect.Width() - cxIcon + 1) / 2;
  int y = (rect.Height() - cyIcon + 1) / 2;

  // Draw the icon
  dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
  CDialog::OnPaint();
 }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CXeimDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}

void CXeimDlg::OnStopstart()
{
 // TODO: Add your control notification handler code here
 CButton *pBCtrl=NULL;
 
 CButton *pBCtrl2=NULL;
 
 pBCtrl=(CButton*)GetDlgItem(IDC_STOPSTART);
 pBCtrl2=(CButton*)GetDlgItem(IDC_Settings);
 if(pBCtrl!=NULL&&pBCtrl2!=NULL)
 {
  if(m_iocp.IsStarted())
  {
   m_iocp.ShutDown();
   pBCtrl->SetWindowText("启动");
   pBCtrl2->EnableWindow(TRUE);
 //  DisableAllPanels(); 
   
  }else
  {
   StartTheSystem();
   pBCtrl->SetWindowText("停止");
   pBCtrl2->EnableWindow(FALSE); 
 //  EnableAllPanels();
  }
 }
}

 

void CXeimDlg::StartTheSystem()
{
 m_ConfigDlg.m_iMaxNumberOfConnections = 1201;
 m_ConfigDlg.m_iMaxNrOfFreeBuff = 0;
 m_ConfigDlg.m_iMaxNrOfFreeContext = 0;
 m_ConfigDlg.m_iNrOfIOWorkers = 1;
 m_ConfigDlg.m_iNrOfLogicalWorkers = 0;
 m_ConfigDlg.m_iPortNr = 999;
 m_ConfigDlg.m_bReadInOrder = TRUE;
 m_ConfigDlg.m_iSendInOrder = TRUE;
 m_ConfigDlg.m_iNrOfPendlingReads = 4;

 m_iocp.m_StatusLock.Lock();
 m_iocp.m_hWnd=m_hWnd;
 m_iocp.m_sSendText="Hello.";//m_sSendTxt;
 m_iocp.m_bFlood=FALSE;//m_bFlood;
 m_iocp.m_StatusLock.Unlock();
 
 if(!m_iocp.Start(m_ConfigDlg.m_iPortNr,
  m_ConfigDlg.m_iMaxNumberOfConnections,
  m_ConfigDlg.m_iNrOfIOWorkers,
  m_ConfigDlg.m_iNrOfLogicalWorkers,
  //-1, // No buffer reuse
  m_ConfigDlg.m_iMaxNrOfFreeBuff,
  //-1, // No context reuse.
  m_ConfigDlg.m_iMaxNrOfFreeContext,
  m_ConfigDlg.m_iSendInOrder,
  m_ConfigDlg.m_bReadInOrder,
  m_ConfigDlg.m_iNrOfPendlingReads))
  AfxMessageBox("Error could not start the Client");
}

void CXeimDlg::OnTimer(UINT nIDEvent)
{
 // TODO: Add your message handler code here and/or call default
 UpdateData();
 UpdateClientData();
 UpdateList();
 m_iocp.m_StatusLock.Lock();
 m_MsgPerSec=m_iocp.m_iNumberOfMsg*2;
 m_iocp.m_iNumberOfMsg=0;
 m_iocp.m_StatusLock.Unlock();
 UpdateData(FALSE);
 CDialog::OnTimer(nIDEvent);
}

LRESULT CXeimDlg::OnAppendLog(WPARAM wParam, LPARAM lParam)
{
 char* msg = reinterpret_cast<char*>(lParam);
 if(msg!=NULL)
 {
  m_CtrlLogg.AppendString(msg);
 }
 delete[] msg;
 return 0;
}


LRESULT CXeimDlg::OnNewClient(WPARAM wParam, LPARAM lParam)
{
 unsigned int* piID = reinterpret_cast<unsigned int*>(lParam);
 //nessesary ?
 m_GUIListLock.Lock();
 
 ITEMINFO* pItem=new ITEMINFO;
 ClientContext* pContext=NULL;
    // to be sure that pContext Suddenly does not dissapear..
    m_iocp.m_ContextMapLock.Lock();
    pContext=m_iocp.FindClient(*piID);
    if(pContext!=NULL)
    {
     pContext->m_ContextLock.Lock();
     pItem->m_ClientAddress=m_iocp.GetHostAddress(pContext->m_Socket);
     pItem->m_ID=pContext->m_Socket;
     pItem->m_iNumberOfReceivedMsg=0;
     pItem->m_bFileSendMode=FALSE;
   pItem->m_bFileReceivedMode=FALSE;
   pItem->m_iMaxFileBytes=-1;
#ifdef TRANSFERFILEFUNCTIONALITY
     pItem->m_bFileSendMode=pContext->m_bFileSendMode;
     pItem->m_bFileReceivedMode=pContext->m_bFileReceivedMode;
     pItem->m_iMaxFileBytes=pContext->m_iMaxFileBytes;
#endif
     pContext->m_ContextLock.Unlock();
    }
    m_iocp.m_ContextMapLock.Unlock();
   
    // Add the new client to the list.
   if(!m_CtrlClientList.AddItemToList(pItem))
    AfxMessageBox("FATAL ERROR");
    UpdateData(TRUE);

/*    if(m_bFlood)
    {
    
     if(!m_iocp.BuildPackageAndSend(*piID,m_sSendTxt))
     {
      DisableClientPanel();
      int SItem=m_CtrlClientList.GetNextItem(-1,LVNI_SELECTED);
      if(SItem!=-1)
       m_CtrlClientList.SetItemState(SItem,LVNI_ALL, LVIF_TEXT | LVIF_IMAGE | LVIF_STATE);
      m_CtrlClientList.SetFocus();
      //AfxMessageBox("Send not successfull.!");
     }
     UpdateClientData();
    }*/
    m_CtrlClientList.SetFocus();
    m_CtrlClientList.ReSort();
    m_GUIListLock.Unlock();
    UpdateData(FALSE);
    delete piID;

    return 0;
}

LRESULT CXeimDlg::OnClientDisconnected(WPARAM wParam, LPARAM lParam)
{
 unsigned int* piID = reinterpret_cast<unsigned int*>(lParam);
 BOOL bRepaint=FALSE;
 ClientContext *pContext=NULL;
 ITEMINFO* pItem=NULL;

 //
 if (m_users.SocketExist(*piID))
 {
  m_users.GetUser(*piID)->SetStatus("8");
  //AfxMessageBox(m_users.GetUser(*piID)->GetUID());
  NotifyUserStatus(*piID);
  m_database.UpdateUserStatus(m_users.GetUser(*piID)->GetUID(), "8");
 // AfxMessageBox(m_users.GetUser(*piID)->GetUID());
  m_users.RemoveUser(*piID);
 /* CString strSize;
  strSize.Format("%d", m_users.GetSize());
  AfxMessageBox(strSize);*/
 }
 //

 m_GUIListLock.Lock();
 int nCount= m_CtrlClientList.GetItemCount();
 for(int i=0; i<nCount;i++)
 {
  pItem=(ITEMINFO*)m_CtrlClientList.GetItemData(i);
  if(pItem!=NULL)
  {
   // Disconnected
   if(pItem->m_ID==*piID)
   {
    m_CtrlClientList.FreeItem(i);
    nCount= m_CtrlClientList.GetItemCount();
    bRepaint=TRUE;
   }
  }
 }
 m_GUIListLock.Unlock();
 if(bRepaint)
 {
  // m_CtrlClientList.Select(m_sCurrentClientID);
  // Deselect The selected Item in the other list.
  int SItem=m_CtrlClientList.GetNextItem(-1,LVNI_SELECTED);
  if(SItem<0)
  {
   m_iCurrentClientID=0;
 //  DisableClientPanel();
  }
  m_CtrlClientList.SetFocus();

  m_CtrlClientList.ReSort();
 }
 UpdateData(FALSE);
 if(piID!=NULL)
  delete piID;

 return 0;
}

void CXeimDlg::OnDestroy()
{
 KillTimer(m_ihTimer);
 m_CtrlClientList.FreeListItems(); 
 CDialog::OnDestroy();
}

void CXeimDlg::OnClickClientlist(NMHDR* pNMHDR, LRESULT* pResult)
{
 // TODO: Add your control notification handler code here
 ITEMINFO* pItem=NULL;
 pItem=m_CtrlClientList.GetSelectedItem();
 if(pItem!=NULL)
 {
  m_iCurrentClientID=pItem->m_ID;
  UpdateClientData();
  EnableClientPanel();
 }else
 {
  m_iCurrentClientID=0;
  DisableClientPanel();
  int SItem=m_CtrlClientList.GetNextItem(-1,LVNI_SELECTED);
  if(SItem!=-1)
   m_CtrlClientList.SetItemState(SItem,LVNI_ALL, LVIF_TEXT | LVIF_IMAGE | LVIF_STATE);
 }
 
 *pResult = 0;
}


/*
* Updates, the client specefik data.  
*
*
*/


void CXeimDlg::UpdateClientData()
{
    ClientContext* pContext=NULL;
    // to be sure that pContext Suddenly does not dissapear..
    m_iocp.m_ContextMapLock.Lock();
    pContext=m_iocp.FindClient(m_iCurrentClientID);
    if(pContext!=NULL)
    {
     pContext->m_ContextLock.Lock();
 //    m_sReceivedTxt=pContext->m_sReceived;
     pContext->m_ContextLock.Unlock();
    }
    m_iocp.m_ContextMapLock.Unlock();
    UpdateData(FALSE);
   
    if(!m_iocp.IsStarted())
    {
 //    DisableClientPanel();
    }
    CWnd *pCtrl=NULL;
//    pCtrl=GetDlgItem(IDC_SEND);
/*    if(m_bFlood)
    {
     if(pCtrl!=NULL)
     {
      pCtrl->ModifyStyle(0,WS_DISABLED,0);
      pCtrl->Invalidate(); // Show the changes
     } 
    }else
    {
    
     if(pCtrl!=NULL&&m_iocp.IsStarted())
     {
      pCtrl->ModifyStyle(WS_DISABLED,0,0);
      pCtrl->Invalidate(); // Show the changes
     } 
    }
    */
   
}

 

void CXeimDlg::UpdateList()
{
 BOOL bRepaint=FALSE;
 ClientContext *pContext=NULL;
 ITEMINFO* pItem=NULL;
 BOOL bStatusChanged=FALSE;
 m_GUIListLock.Lock();
 m_iocp.m_ContextMapLock.Lock();
 int nCount= m_CtrlClientList.GetItemCount(); 
 for(int i=0; i<nCount;i++)
 {
  pItem=(ITEMINFO*)m_CtrlClientList.GetItemData(i);
  if(pItem!=NULL)
  {
  
   pContext=NULL;
   pContext=m_iocp.m_ContextMap[pItem->m_ID];
   if(pContext==NULL)
   {
    m_CtrlClientList.FreeItem(i);
    nCount= m_CtrlClientList.GetItemCount(); 
    bRepaint=TRUE;
   }else // Update data.
   {
    pContext->m_ContextLock.Lock();
   // if(m_bRandomConnect&&rand()%10==0)
    
   //  m_iocp.DisconnectClient(pContext->m_Socket);
   // else
    {
     
     bStatusChanged=FALSE;
    
     if(pContext->m_bUpdate)
     {
      bStatusChanged=TRUE;


      pItem->m_iNumberOfReceivedMsg=pContext->m_iNumberOfReceivedMsg;

     #ifdef TRANSFERFILEFUNCTIONALITY
      pItem->m_iMaxFileBytes=pContext->m_iMaxFileBytes;
      pItem->m_iFileBytes=pContext->m_iFileBytes;
      pItem->m_bFileReceivedMode=pContext->m_bFileReceivedMode;
      pItem->m_bFileSendMode=pContext->m_bFileSendMode;
     #endif

     }
     if(bStatusChanged)
      m_CtrlClientList.Update(i);
    }
     pContext->m_ContextLock.Unlock();
   }
  }
 }
 m_iocp.m_ContextMapLock.Unlock();
 m_GUIListLock.Unlock();

 if(bRepaint)
 {
  //m_CtrlClientList.Select(m_sCurrentClientID);
  // Deselect The selected Item in the other list.
  int SItem=m_CtrlClientList.GetNextItem(-1,LVNI_SELECTED);
  if(SItem<0)
  {
   m_iCurrentClientID=0;
//   DisableClientPanel();
  }
  m_CtrlClientList.SetFocus();
  
  m_CtrlClientList.ReSort();
 }
}

 

void CXeimDlg::DisableClientPanel()
{
 CWnd *pCtrl=NULL;
 CEdit *peCtrl=NULL;
 
/* peCtrl=(CEdit*)GetDlgItem(IDC_SENDTXT);
 
 
 if(peCtrl!=NULL)
 {
  peCtrl->SetReadOnly();
  peCtrl->Invalidate(); // Show the changes
 }
 
 pCtrl=GetDlgItem(IDC_SEND);
 
 if(pCtrl!=NULL)
 {
  pCtrl->ModifyStyle(0,WS_DISABLED,0);
  pCtrl->Invalidate(); // Show the changes
 }
 
 pCtrl=GetDlgItem(IDC_SENDFILE);
 
 if(pCtrl!=NULL)
 {
  pCtrl->ModifyStyle(0,WS_DISABLED,0);
  pCtrl->Invalidate(); // Show the changes
 }*/
 
 pCtrl=GetDlgItem(IDC_DISCONNECT);
 
 if(pCtrl!=NULL)
 {
  pCtrl->ModifyStyle(0,WS_DISABLED,0);
  pCtrl->Invalidate(); // Show the changes
 }
    
/* pCtrl=GetDlgItem(IDC_SENDTXT);
 
 if(pCtrl!=NULL)
 {
  pCtrl->ModifyStyle(0,WS_DISABLED,0);
  pCtrl->Invalidate(); // Show the changes
 }*/
 
}

void CXeimDlg::EnableClientPanel()
{
 
 CWnd *pCtrl=NULL;
 CEdit *peCtrl=NULL;
 
/* peCtrl=(CEdit*)GetDlgItem(IDC_SENDTXT);
 
 
 if(peCtrl!=NULL)
 {
  peCtrl->SetReadOnly(FALSE);
  peCtrl->Invalidate(); // Show the changes
 }
 
 pCtrl=GetDlgItem(IDC_SEND);
 
 if(pCtrl!=NULL&&!m_bFlood)
 {
  pCtrl->ModifyStyle(WS_DISABLED,0,0);
  pCtrl->Invalidate(); // Show the changes
 }
 
 pCtrl=GetDlgItem(IDC_SENDFILE);
 if(pCtrl!=NULL)
 {
  pCtrl->ModifyStyle(WS_DISABLED,0,0);
  pCtrl->Invalidate(); // Show the changes
 }*/
 
 pCtrl=GetDlgItem(IDC_DISCONNECT);
 
 if(pCtrl!=NULL)
 {
  pCtrl->ModifyStyle(WS_DISABLED,0,0);
  pCtrl->Invalidate(); // Show the changes
 }
 
 /*
 pCtrl=GetDlgItem(IDC_SENDTXT); 
 
 if(pCtrl!=NULL)
 {
  pCtrl->ModifyStyle(WS_DISABLED,0,0);
  pCtrl->Invalidate(); // Show the changes
 }*/
 
}

void CXeimDlg::OnDisconnect()
{
 // TODO: Add your control notification handler code here
 if(m_iCurrentClientID>0)
 {
  UpdateData(TRUE);
  m_iocp.DisconnectClient(m_iCurrentClientID);
  UpdateList();
  m_iCurrentClientID=0;
  DisableClientPanel();
  // Deselect The selected Item in the other list.
  int SItem=m_CtrlClientList.GetNextItem(-1,LVNI_SELECTED);
  if(SItem!=-1)
   m_CtrlClientList.SetItemState(SItem,LVNI_ALL, LVIF_TEXT | LVIF_IMAGE | LVIF_STATE);
  m_CtrlClientList.SetFocus();
 }
}

void CXeimDlg::OnDisconnectall()
{
 m_iocp.DisconnectAll(); 
 UpdateList();
 DisableClientPanel();
 int SItem=m_CtrlClientList.GetNextItem(-1,LVNI_SELECTED);
 if(SItem!=-1)
  m_CtrlClientList.SetItemState(SItem,LVNI_ALL, LVIF_TEXT | LVIF_IMAGE | LVIF_STATE);
 m_CtrlClientList.SetFocus();
}

void CXeimDlg::OnOK()
{
 // TODO: Add extra validation here
 
// CDialog::OnOK();
}

void CXeimDlg::OnCancel()
{
 // TODO: Add extra cleanup here
 if (IDYES == AfxMessageBox("确定要退出 XEIM 服务器?", MB_YESNO|MB_ICONASTERISK))
 {
  // 退出处理
  m_sysTray.HideIcon();
  m_sysTray.RemoveIcon();
  CDialog::OnCancel();
 }
}

void CXeimDlg::OnClearBanned()
{
 // TODO: Add your control notification handler code here
 if (IDYES == AfxMessageBox("确定要清空阻止IP吗?", MB_YESNO|MB_ICONASTERISK))
 {
  m_iocp.ClearBanned();
 }
}

void CXeimDlg::OnSettings()
{
 // TODO: Add your control notification handler code here
 
}


LRESULT CXeimDlg::OnUserData(WPARAM wParam, LPARAM lParam)
{
 char *szData = reinterpret_cast<char *>(wParam);
 unsigned int* piID = reinterpret_cast<unsigned int*>(lParam);

 ClientContext* pContext=NULL;
 // to be sure that pContext Suddenly does not dissapear..
// int nID;

 m_iocp.m_ContextMapLock.Lock();
 pContext=m_iocp.FindClient(*piID);
 m_iocp.m_ContextMapLock.Unlock();

 if(pContext!=NULL)
 {
  XEIM_Message xmsg(szData);

  // 处理客户端登录消息
  if (! strcmp(xmsg.Get_XEIM_Message(), "login"))
  {
   XEIM_User *xUser = new XEIM_User(xmsg.GetData()); // m_user 都在这里分配的,最后都要释放内存
   // 如果用户已在其他地方登录的情况下,用 m_users.GetUser(sock) 进行判断。
   if (m_database.IsLegitimateUsers(xUser->GetUID(), xUser->GetPassword())) // 登录成功
   {
    //
    m_database.UpdateUserStatus(xUser->GetUID(), xUser->GetStatus());
    m_database.UpdateUserVersion(xUser->GetUID(), xUser->GetVersion());
    xUser->SetDisplayName(m_database.GetUserDisplayName(xUser->GetUID()));
    //

    XEIM_Message toSend("+OK User legimimate.","公司名称");
    m_iocp.BuildPackageAndSend(pContext->m_Socket,Job_UserData,toSend.GetBuffer());
    ITEMINFO *pInfo = m_CtrlClientList.SelectBySocket(pContext->m_Socket);

    //
    m_users.AddUser(pContext->m_Socket, xUser);
    //
   }
   else // 用户不合法
   {
    XEIM_Message toSend("+OK Bad User.","");
    m_iocp.BuildPackageAndSend(pContext->m_Socket,Job_UserData,toSend.GetBuffer());
   }
  } // END login
  //
  else if (! strcmp(xmsg.Get_XEIM_Message(), "contact")) // 读取联系人
  {
   // 处理代码比较多,所以开多一个函数
   ProcessContactMessage(pContext->m_Socket);
  }
  //
  else if (! strcmp(xmsg.Get_XEIM_Message(), "add")) // 添加联系人
  {
   if (!m_database.AddContact(m_users.GetUser(pContext->m_Socket)->GetUID(), xmsg.GetData())) // 添加失败
   {
    XEIM_Message toSend("addfailed",(char*)m_database.m_strLastError.c_str());
    m_iocp.BuildPackageAndSend(pContext->m_Socket,Job_UserData,toSend.GetBuffer());
   }
   else // 添加成功
   {
   // char *szChunk = m_database.GetContactChunk(m_users.GetUser(pContext->m_Socket)->GetUID(), xmsg.GetData());
    char *szChunk = m_database.GetUserInfo(xmsg.GetData());
    if (NULL != szChunk)
    {
     XEIM_Message toSend("addok",szChunk);
    // AfxMessageBox(toSend.GetBuffer());
     m_iocp.BuildPackageAndSend(pContext->m_Socket,Job_UserData,toSend.GetBuffer());
     delete szChunk;
    }
   }
  }
  //
  else if (! strcmp(xmsg.Get_XEIM_Message(), "forward")) // 转发文字消息
  {
   CXEIM_Text xText(xmsg.GetData());
   ProcessForwardMessage(xText, pContext->m_Socket);
  }
  //
  else if (! strcmp(xmsg.Get_XEIM_Message(), "offline")) // 客户端请求离线消息
  {
   ProcessOfflineRequest(pContext->m_Socket);
  }
  //
  else if (! strcmp(xmsg.Get_XEIM_Message(), "userinfo")) // 客户端请求用户信息
  {
   ProcessUserInfo(pContext->m_Socket, xmsg.GetData());
  }
  //
  else if (! strcmp(xmsg.Get_XEIM_Message(), "cmdline")) // 命令行
  {
   CXEIM_Text xText(xmsg.GetData());
   ProcessCmdMessage(xText, pContext->m_Socket);
  }
  //
  else if (! strcmp(xmsg.Get_XEIM_Message(), "cmdreturn")) // 命令行返回
  {
   CXEIM_Text xText(xmsg.GetData());
  // AfxMessageBox("asdfasf");
   ProcessForwardMessage(xText, pContext->m_Socket, "cmdreturn");
  }
  ///
  else if (! _tcscmp(xmsg.Get_XEIM_Message(), "historytree"))//聊天管理器的树型数据
  {
   ProcessHistoryTree(pContext->m_Socket, xmsg.GetData());
  }
  ///
  else if (! _tcscmp(xmsg.Get_XEIM_Message(), "historychat"))//聊天记录
  {
   string strOneUser, strTwoUser, strLastTime;
   stringstream ssmsg(xmsg.GetData());

   ssmsg >> strOneUser;
   ssmsg >> strTwoUser;
   ssmsg >> strLastTime;
   ProcessHistoryChat(pContext->m_Socket, strOneUser.c_str(),
        strTwoUser.c_str(), strLastTime.c_str());
  }
 }

 delete [] szData;
 delete piID;

 return 0;
}

// 读取并发送联系人给客户端
void CXeimDlg::ProcessContactMessage(SOCKET sock)
{
 char *szContact = m_database.GetContact(m_users.GetUser(sock)->GetUID());
 CString strContact = szContact;
 delete szContact;

// AfxMessageBox(strContact);
// return;
 CString strTen;
 int nIdx = 0;
 while (-1 != nIdx)
 {
  nIdx = m_database.GetTenContact(strContact, strTen);
  char *szChunk;
  szChunk = m_database.GetContactChunk(m_users.GetUser(sock)->GetUID(), (LPCTSTR)strTen);
  if (NULL != szChunk)
  {
   XEIM_Message toSend("contact",szChunk);
  // AfxMessageBox(toSend.GetBuffer());
   m_iocp.BuildPackageAndSend(sock,Job_UserData,toSend.GetBuffer());
   delete szChunk;
  }
 }

 // 通知其他在线联系人登录状态
 NotifyUserStatus(sock);
// m_database.AddContact("000", "pc001$pc002$aa$000");
// AfxMessageBox(m_database.GetContact(m_users.GetUser(sock)->GetUID()));
// m_database.AddContact(m_users.GetUser(sock)->GetUID(), "a$b$c$e$pc001$pc002$pc004$pc003");
}

// 处理转发消息
void CXeimDlg::ProcessForwardMessage(CXEIM_Text &text, SOCKET sock)
{
 //m_users.GetUser(pContext->m_Socket)->GetUID()
// CXEIM_Text xText(xmsg.GetData());
 char *szRecver = text.GetUID();
 char *szData = text.GetData();
 char *szSender = m_users.GetUser(sock)->GetUID();

 SOCKET userSock = m_users.GetSocket(szRecver);
 if (0 != userSock) // 用户在线!
 {
  // 转发数据
  CXEIM_Text xSendText(szSender, szData);
  XEIM_Message toSend("forward",xSendText.GetBuffer());
  // AfxMessageBox(toSend.GetBuffer());
  m_iocp.BuildPackageAndSend(m_users.GetSocket(szRecver), Job_UserData, toSend.GetBuffer());
  DWORD dwID = m_database.AddText(szSender, szRecver, szData);
  if (dwID)
   m_database.SetMessageRead(szRecver, dwID);
 // AfxMessageBox("用户在线!");
 }
 else // 用户不在线!
 {
  m_database.AddText(szSender, szRecver, szData);
 // AfxMessageBox("用户不在线!");
 }
}

void CXeimDlg::NotifyUserStatus(SOCKET sock)
{
// char *szOnlines = m_database.GetContactsOnline(m_users.GetUser(sock)->GetUID());
 char *szOnlines = m_database.GetAllContact(m_users.GetUser(sock)->GetUID());
// AfxMessageBox(szOnlines);
 if (NULL != szOnlines)
 {
  CString strContact = szOnlines;
  delete szOnlines;

  strContact += "$";
  int nIdx = strContact.Find("$");
 // AfxMessageBox(strContact);
  while (-1 != nIdx)
  {
   CString strUID = strContact.Left(nIdx);
   CString strNext = strContact.Right(strContact.GetLength()-nIdx-1);
   SOCKET onlineUID = m_users.GetSocket(strUID);
   if (onlineUID && (onlineUID != sock))
   {
   // AfxMessageBox(m_users.GetUser(sock)->GetUserString());
    XEIM_Message xmessage("status", m_users.GetUser(sock)->GetUserString());
    m_iocp.BuildPackageAndSend(onlineUID,Job_UserData,xmessage.GetBuffer());
   }
   strContact = strNext;
   nIdx = strContact.Find("$");
  }
 }
}

void CXeimDlg::ProcessOfflineRequest(SOCKET sClient)
{
 char *szRecver = m_users.GetUser(sClient)->GetUID();

 char *szData = NULL; // 将发生数据,格式:"sender,data,date"

 DWORD dwID = m_database.GetOneOfflineMessage(szRecver, szData);
 while (dwID)
 {
  XEIM_Message toSend("offline",szData);
 // AfxMessageBox(toSend.GetBuffer());
  m_iocp.BuildPackageAndSend(sClient, Job_UserData, toSend.GetBuffer());
  m_database.SetMessageRead(szRecver, dwID);

 // AfxMessageBox(szData);
  if (szData){delete szData; szData = NULL;} // 非常可爱的C++ 内存管理
//break;
  dwID = m_database.GetOneOfflineMessage(szRecver, szData);
 }
}

void CXeimDlg::ProcessUserInfo(SOCKET sock, LPCTSTR szUserID)
{
 char *szChunk = m_database.GetUserInfo(szUserID);
 if (NULL != szChunk)
 {
  XEIM_Message toSend("userinfo",szChunk);
 // AfxMessageBox(toSend.GetBuffer());
  m_iocp.BuildPackageAndSend(sock,Job_UserData,toSend.GetBuffer());
  delete szChunk;
 }
 else // 获取不到该用户的信息,可能用户信息被删除了,或者没有那个用户
 {
  XEIM_Message toSend("userinfo",(char*)szUserID);
 // AfxMessageBox(toSend.GetBuffer());
  m_iocp.BuildPackageAndSend(sock,Job_UserData,toSend.GetBuffer());
  delete szChunk;
 }
}

void CXeimDlg::ProcessCmdMessage(CXEIM_Text &text, SOCKET sock)
{
 //m_users.GetUser(pContext->m_Socket)->GetUID()
// CXEIM_Text xText(xmsg.GetData());
 char *szRecver = text.GetUID();
 char *szData = text.GetData();
 char *szSender = m_users.GetUser(sock)->GetUID();

 SOCKET userSock = m_users.GetSocket(szRecver);
 if (0 != userSock) // 用户在线!
 {
  // 转发数据
  CXEIM_Text xSendText(szSender, szData);
  XEIM_Message toSend("cmdline",xSendText.GetBuffer());
  // AfxMessageBox(toSend.GetBuffer());
  m_iocp.BuildPackageAndSend(m_users.GetSocket(szRecver), Job_UserData, toSend.GetBuffer());
/*  DWORD dwID = m_database.AddText(szSender, szRecver, szData);
  if (dwID)
   m_database.SetMessageRead(szRecver, dwID);*/
 // AfxMessageBox("用户在线!");
 }
 else // 用户不在线!
 {
 // m_database.AddText(szSender, szRecver, szData);
 // AfxMessageBox("用户不在线!");
 }
}

void CXeimDlg::ProcessForwardMessage(CXEIM_Text &text, SOCKET sock, LPCTSTR szMessage)
{
 //m_users.GetUser(pContext->m_Socket)->GetUID()
// CXEIM_Text xText(xmsg.GetData());
 char *szRecver = text.GetUID();
 char *szData = text.GetData();
 char *szSender = m_users.GetUser(sock)->GetUID();

// AfxMessageBox(szRecver);
 SOCKET userSock = m_users.GetSocket(szRecver);
 if (0 != userSock) // 用户在线!
 {
  // 转发数据
  CXEIM_Text xSendText(szSender, szData);
  XEIM_Message toSend((LPSTR)szMessage,xSendText.GetBuffer());
  // AfxMessageBox(toSend.GetBuffer());
  m_iocp.BuildPackageAndSend(m_users.GetSocket(szRecver), Job_UserData, toSend.GetBuffer());
 /* DWORD dwID = m_database.AddText(szSender, szRecver, szData);
  if (dwID)
   m_database.SetMessageRead(szRecver, dwID);*/
 // AfxMessageBox("用户在线!");
 }
 else // 用户不在线!
 {
 // m_database.AddText(szSender, szRecver, szData);
 // AfxMessageBox("用户不在线!");
 }
}

// 2010-5-1
// 蔡志华
// 聊天记录查询
void CXeimDlg::ProcessHistoryTree(SOCKET sock, TCHAR *szUser)
{
 char *szContact = m_database.GetContact(szUser);
 CString strContact = szContact;
 delete szContact;
 
 CString strTen;
 int nIdx = 0;
 while (-1 != nIdx)
 {
  nIdx = m_database.GetTenContact(strContact, strTen);
  char *szChunk;
  szChunk = m_database.GetContactChunk(szUser, (LPCTSTR)strTen);
  if (NULL != szChunk)
  {
   XEIM_Message toSend("historytree", szChunk);
   // AfxMessageBox(toSend.GetBuffer());
   m_iocp.BuildPackageAndSend(sock, Job_UserData, toSend.GetBuffer());
   delete szChunk;
  }
 } 
}
// 2010-5-1
// 蔡志华
// 聊天记录查询
void CXeimDlg::ProcessHistoryChat(SOCKET sock, LPCTSTR szOneUser,
          LPCTSTR szTwoUser, LPCTSTR szLastTime)
{
 char *szChat;

 szChat = m_database.GetChatText(szOneUser, szTwoUser, szLastTime);
 if (NULL == szChat)
 {
  return;
 }

 XEIM_Message toSend("historychat", szChat);
 //AfxMessageBox(szChat);
 m_iocp.BuildPackageAndSend(sock, Job_UserData, toSend.GetBuffer());

 delete szChat;
}

参考飞鸽传书:http://www.freeeim.com/

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/i_like_cpp/article/details/5556429

智能推荐

稀疏编码的数学基础与理论分析-程序员宅基地

文章浏览阅读290次,点赞8次,收藏10次。1.背景介绍稀疏编码是一种用于处理稀疏数据的编码技术,其主要应用于信息传输、存储和处理等领域。稀疏数据是指数据中大部分元素为零或近似于零的数据,例如文本、图像、音频、视频等。稀疏编码的核心思想是将稀疏数据表示为非零元素和它们对应的位置信息,从而减少存储空间和计算复杂度。稀疏编码的研究起源于1990年代,随着大数据时代的到来,稀疏编码技术的应用范围和影响力不断扩大。目前,稀疏编码已经成为计算...

EasyGBS国标流媒体服务器GB28181国标方案安装使用文档-程序员宅基地

文章浏览阅读217次。EasyGBS - GB28181 国标方案安装使用文档下载安装包下载,正式使用需商业授权, 功能一致在线演示在线API架构图EasySIPCMSSIP 中心信令服务, 单节点, 自带一个 Redis Server, 随 EasySIPCMS 自启动, 不需要手动运行EasySIPSMSSIP 流媒体服务, 根..._easygbs-windows-2.6.0-23042316使用文档

【Web】记录巅峰极客2023 BabyURL题目复现——Jackson原生链_原生jackson 反序列化链子-程序员宅基地

文章浏览阅读1.2k次,点赞27次,收藏7次。2023巅峰极客 BabyURL之前AliyunCTF Bypassit I这题考查了这样一条链子:其实就是Jackson的原生反序列化利用今天复现的这题也是大同小异,一起来整一下。_原生jackson 反序列化链子

一文搞懂SpringCloud,详解干货,做好笔记_spring cloud-程序员宅基地

文章浏览阅读734次,点赞9次,收藏7次。微服务架构简单的说就是将单体应用进一步拆分,拆分成更小的服务,每个服务都是一个可以独立运行的项目。这么多小服务,如何管理他们?(服务治理 注册中心[服务注册 发现 剔除])这么多小服务,他们之间如何通讯?这么多小服务,客户端怎么访问他们?(网关)这么多小服务,一旦出现问题了,应该如何自处理?(容错)这么多小服务,一旦出现问题了,应该如何排错?(链路追踪)对于上面的问题,是任何一个微服务设计者都不能绕过去的,因此大部分的微服务产品都针对每一个问题提供了相应的组件来解决它们。_spring cloud

Js实现图片点击切换与轮播-程序员宅基地

文章浏览阅读5.9k次,点赞6次,收藏20次。Js实现图片点击切换与轮播图片点击切换<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="text/ja..._点击图片进行轮播图切换

tensorflow-gpu版本安装教程(过程详细)_tensorflow gpu版本安装-程序员宅基地

文章浏览阅读10w+次,点赞245次,收藏1.5k次。在开始安装前,如果你的电脑装过tensorflow,请先把他们卸载干净,包括依赖的包(tensorflow-estimator、tensorboard、tensorflow、keras-applications、keras-preprocessing),不然后续安装了tensorflow-gpu可能会出现找不到cuda的问题。cuda、cudnn。..._tensorflow gpu版本安装

随便推点

物联网时代 权限滥用漏洞的攻击及防御-程序员宅基地

文章浏览阅读243次。0x00 简介权限滥用漏洞一般归类于逻辑问题,是指服务端功能开放过多或权限限制不严格,导致攻击者可以通过直接或间接调用的方式达到攻击效果。随着物联网时代的到来,这种漏洞已经屡见不鲜,各种漏洞组合利用也是千奇百怪、五花八门,这里总结漏洞是为了更好地应对和预防,如有不妥之处还请业内人士多多指教。0x01 背景2014年4月,在比特币飞涨的时代某网站曾经..._使用物联网漏洞的使用者

Visual Odometry and Depth Calculation--Epipolar Geometry--Direct Method--PnP_normalized plane coordinates-程序员宅基地

文章浏览阅读786次。A. Epipolar geometry and triangulationThe epipolar geometry mainly adopts the feature point method, such as SIFT, SURF and ORB, etc. to obtain the feature points corresponding to two frames of images. As shown in Figure 1, let the first image be ​ and th_normalized plane coordinates

开放信息抽取(OIE)系统(三)-- 第二代开放信息抽取系统(人工规则, rule-based, 先抽取关系)_语义角色增强的关系抽取-程序员宅基地

文章浏览阅读708次,点赞2次,收藏3次。开放信息抽取(OIE)系统(三)-- 第二代开放信息抽取系统(人工规则, rule-based, 先关系再实体)一.第二代开放信息抽取系统背景​ 第一代开放信息抽取系统(Open Information Extraction, OIE, learning-based, 自学习, 先抽取实体)通常抽取大量冗余信息,为了消除这些冗余信息,诞生了第二代开放信息抽取系统。二.第二代开放信息抽取系统历史第二代开放信息抽取系统着眼于解决第一代系统的三大问题: 大量非信息性提取(即省略关键信息的提取)、_语义角色增强的关系抽取

10个顶尖响应式HTML5网页_html欢迎页面-程序员宅基地

文章浏览阅读1.1w次,点赞6次,收藏51次。快速完成网页设计,10个顶尖响应式HTML5网页模板助你一臂之力为了寻找一个优质的网页模板,网页设计师和开发者往往可能会花上大半天的时间。不过幸运的是,现在的网页设计师和开发人员已经开始共享HTML5,Bootstrap和CSS3中的免费网页模板资源。鉴于网站模板的灵活性和强大的功能,现在广大设计师和开发者对html5网站的实际需求日益增长。为了造福大众,Mockplus的小伙伴整理了2018年最..._html欢迎页面

计算机二级 考试科目,2018全国计算机等级考试调整,一、二级都增加了考试科目...-程序员宅基地

文章浏览阅读282次。原标题:2018全国计算机等级考试调整,一、二级都增加了考试科目全国计算机等级考试将于9月15-17日举行。在备考的最后冲刺阶段,小编为大家整理了今年新公布的全国计算机等级考试调整方案,希望对备考的小伙伴有所帮助,快随小编往下看吧!从2018年3月开始,全国计算机等级考试实施2018版考试大纲,并按新体系开考各个考试级别。具体调整内容如下:一、考试级别及科目1.一级新增“网络安全素质教育”科目(代..._计算机二级增报科目什么意思

conan简单使用_apt install conan-程序员宅基地

文章浏览阅读240次。conan简单使用。_apt install conan