home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / ReplicaNetFreewareV5_4.exe / data1.cab / Program_Executable_Files / Example5 / ChatDlg.cpp next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  7.1 KB  |  294 lines

  1. /* START_LICENSE_HEADER
  2.  
  3. Copyright (C) 2000 Martin Piper, original design and program code
  4. Copyright (C) 2001-2005 Replica Software
  5.  
  6. This program file is copyright (C) Replica Software and can only be used under license.
  7. For more information visit: http://www.replicanet.com/
  8. Or email: info@replicanet.com
  9.  
  10. END_LICENSE_HEADER */
  11. // ChatDlg.cpp : implementation file
  12. // The chat screen dialog
  13. //
  14.  
  15. #include "stdafx.h"
  16. #include "Example5.h"
  17. #include "ChatDlg.h"
  18.  
  19. using namespace RNReplicaNet;
  20.  
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CChatDlg dialog
  29.  
  30.  
  31. CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/)
  32.     : CDialog(CChatDlg::IDD, pParent)
  33. {
  34.     //{{AFX_DATA_INIT(CChatDlg)
  35.         // NOTE: the ClassWizard will add member initialization here
  36.     //}}AFX_DATA_INIT
  37. }
  38.  
  39.  
  40. void CChatDlg::DoDataExchange(CDataExchange* pDX)
  41. {
  42.     CDialog::DoDataExchange(pDX);
  43.     //{{AFX_DATA_MAP(CChatDlg)
  44.     DDX_Control(pDX, IDC_LIST1, mUsersList);
  45.     DDX_Control(pDX, IDC_EDIT1, mChatEdit);
  46.     DDX_Control(pDX, IDC_RICHTEXTCTRL1, mChatWindow);
  47.     //}}AFX_DATA_MAP
  48. }
  49.  
  50.  
  51. BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
  52.     //{{AFX_MSG_MAP(CChatDlg)
  53.     ON_WM_TIMER()
  54.     ON_BN_CLICKED(IDC_WHISPER, OnWhisper)
  55.     ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
  56.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  57.     //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CChatDlg message handlers
  62.  
  63. BOOL CChatDlg::OnInitDialog() 
  64. {
  65.     CDialog::OnInitDialog();
  66.     
  67.     mXPSession = ((CExample5App *)AfxGetApp())->mXPSession;
  68.  
  69.     mIsStable = false;
  70.     mError = false;
  71.  
  72.     ShowWindow(SW_SHOW);
  73.     ShowWindow(SW_RESTORE);
  74.     ShowWindow(SW_SHOWDEFAULT);
  75.  
  76.     SetForegroundWindow();
  77.  
  78.     SetTimer(1,100,0);
  79.  
  80.     return TRUE;  // return TRUE unless you set the focus to a control
  81.                   // EXCEPTION: OCX Property Pages should return FALSE
  82. }
  83.  
  84. void CChatDlg::OnTimer(UINT nIDEvent) 
  85. {
  86.     // TODO: Add your message handler code here and/or call default
  87.     int sessionid;
  88.     bool change = false;
  89.  
  90.     char buffer[512];
  91.  
  92.     while ( (sessionid = mXPSession->GetJoiner()) != kXPSessionUnknownID)
  93.     {
  94.         char buffer[64];
  95.         sprintf(buffer,"%d:",sessionid);
  96.         mUsersList.AddString(buffer);
  97.  
  98.         mChatText += std::string("SessionID '")+std::string(buffer)+std::string("' Joined\n");
  99.         change = true;
  100.  
  101.         strcpy(buffer+4,mName);
  102.         ((int *)buffer)[0] = kChatCommand_Name;
  103.         mXPSession->DataSendToSession(sessionid,buffer,strlen(buffer+4)+4+1,true);
  104.     }
  105.  
  106.     while ( (sessionid = mXPSession->GetLeaver()) != kXPSessionUnknownID)
  107.     {
  108.         char buffer[64];
  109.         sprintf(buffer,"%d:",sessionid);
  110.         int sel = mUsersList.FindString(0,buffer);
  111.         if (sel != LB_ERR)
  112.         {
  113.             char full[64];
  114.             mUsersList.GetText(sel,full);
  115.             mUsersList.DeleteString(sel);
  116.             mChatText += std::string("User '")+std::string(full)+std::string("' Left\n");
  117.             change = true;
  118.         }
  119.         else
  120.         {
  121.             mChatText += std::string("Unknown sessionID ");
  122.             mChatText += std::string(buffer);
  123.             mChatText += std::string(" Left\n");
  124.             change = true;
  125.         }
  126.     }
  127.  
  128.     if (!mIsStable)
  129.     {
  130.         if (mXPSession->IsStable())
  131.         {
  132.             mIsMaster = mXPSession->IsMaster();
  133.  
  134.             mIsStable = true;
  135.             mChatText.erase();
  136.             if (mXPSession->IsMaster())
  137.             {
  138.                 mChatText += std::string("New server started for you '")+std::string(mName)+std::string("'\n");
  139.                 mChatText += mXPSession->ExportURL();
  140.                 change = true;
  141.             }
  142.             else
  143.             {
  144.                 mChatText += std::string("Server joined for you '")+std::string(mName)+std::string("'\n");
  145.                 change = true;
  146.             }
  147.  
  148.             mChatText += std::string("Session params:");
  149.             if (mXPSession->GetCanBecomeMaster())
  150.             {
  151.                 mChatText += std::string(" Can become master.");
  152.             }
  153.             if (mXPSession->GetCanSpider())
  154.             {
  155.                 mChatText += std::string(" Can spider.");
  156.             }
  157.             mChatText += std::string("\n");
  158.  
  159.             if (mIsMaster)
  160.             {
  161.                 mChatText += std::string("Session: Is master\n");
  162.             }
  163.             else
  164.             {
  165.                 mChatText += std::string("Session: Is not master\n");
  166.             }
  167.         }
  168.     }
  169.  
  170.     if (mXPSession->GetStatus() == XPSession::Error::kXPSession_EERROR && !mError)
  171.     {
  172.         mError = true;
  173.         mChatText += std::string("The master session left and this session is not able to become a master. Chat has ended.\n");
  174.         mUsersList.EnableWindow(false);
  175.     }
  176.  
  177.     if (mIsMaster != mXPSession->IsMaster())
  178.     {
  179.         mChatText += std::string("Session state changed\n");
  180.         mIsMaster = mXPSession->IsMaster();
  181.         if (mIsMaster)
  182.         {
  183.             mChatText += std::string("Session: Is master\n");
  184.         }
  185.         else
  186.         {
  187.             mChatText += std::string("Session: Is not master\n");
  188.         }
  189.         change = true;
  190.     }
  191.  
  192.     int length;
  193.     while (mXPSession->DataReceive(&sessionid,buffer,&length))
  194.     {
  195.         int command = ((int *)buffer)[0];
  196.         switch(command)
  197.         {
  198.             case kChatCommand_Name:
  199.             {
  200.                 char tempbuffer[64];
  201.                 sprintf(tempbuffer,"%d:",sessionid);
  202.                 int sel = mUsersList.FindString(0,tempbuffer);
  203.                 if (sel != LB_ERR)
  204.                 {
  205.                     mUsersList.DeleteString(sel);
  206.                     sprintf(tempbuffer,"%d:%s",sessionid,buffer+4);
  207.                     mUsersList.InsertString(sel,tempbuffer);
  208.                     sprintf(tempbuffer,"SessionID %d is now known as '%s'\n",sessionid,buffer+4);
  209.                     mChatText += std::string(tempbuffer);
  210.                     change = true;
  211.                 }                
  212.                 break;
  213.             }
  214.             case kChatCommand_Chat:
  215.             {
  216.                 char tempbuffer[256];
  217.                 sprintf(tempbuffer,"%d:",sessionid);
  218.                 int sel = mUsersList.FindString(0,tempbuffer);
  219.                 if (sel != LB_ERR)
  220.                 {
  221.                     char full[64];
  222.                     mUsersList.GetText(sel,full);
  223.                     sprintf(tempbuffer,"'%s' Said '%s'\n",full,buffer+4);
  224.                     mChatText += std::string(tempbuffer);
  225.                     change = true;
  226.                 }                
  227.                 break;
  228.             }
  229.             case kChatCommand_Whisper:
  230.             {
  231.                 char tempbuffer[256];
  232.                 sprintf(tempbuffer,"%d:",sessionid);
  233.                 int sel = mUsersList.FindString(0,tempbuffer);
  234.                 if (sel != LB_ERR)
  235.                 {
  236.                     char full[64];
  237.                     mUsersList.GetText(sel,full);
  238.                     sprintf(tempbuffer,"'%s' Whispered '%s'\n",full,buffer+4);
  239.                     mChatText += std::string(tempbuffer);
  240.                     change = true;
  241.                 }                
  242.                 break;
  243.             }
  244.             default:
  245.                 break;
  246.         }
  247.     }
  248.  
  249.  
  250.     if (change)
  251.     {
  252.         mChatWindow.SetText(mChatText.c_str());
  253.     }
  254.  
  255.     CDialog::OnTimer(nIDEvent);
  256. }
  257.  
  258. void CChatDlg::OnWhisper() 
  259. {
  260.     int sel;
  261.     if ( (sel = mUsersList.GetCurSel()) == LB_ERR)
  262.     {
  263.         return;
  264.     }
  265.     char name[64];
  266.     mUsersList.GetText(sel,name);
  267.     char buffer[256];
  268.     int sessionid;
  269.     mChatEdit.GetWindowText(buffer+4,sizeof(buffer)-4);
  270.     sscanf(name,"%d:",&sessionid);
  271.     ((int *)buffer)[0] = kChatCommand_Whisper;
  272.     mXPSession->DataSendToSession(sessionid,buffer,strlen(buffer+4)+4+1,true);
  273.  
  274.     mChatText += std::string("Whisper to '")+std::string(name)+std::string("' this: '")+std::string(buffer+4)+std::string("'\n");
  275.  
  276.     mChatWindow.SetText(mChatText.c_str());
  277. }
  278.  
  279. void CChatDlg::OnChangeEdit1() 
  280. {
  281. }
  282.  
  283. void CChatDlg::OnButton1() 
  284. {
  285.     char buffer[256];
  286.     mChatEdit.GetWindowText(buffer+4,sizeof(buffer)-4);
  287.     ((int *)buffer)[0] = kChatCommand_Chat;
  288.     mXPSession->DataSendToSession(kXPSessionBroadcastID,buffer,strlen(buffer+4)+4+1,true);
  289.  
  290.     mChatText += std::string("To everyone '")+std::string(buffer+4)+std::string("'\n");
  291.  
  292.     mChatWindow.SetText(mChatText.c_str());
  293. }
  294.