cMultiplayer.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:29k
- // CMAIN LIB - APPLICATION AND DIRECT WRAPPER
- //
- // Written by Mauricio Teichmann Ritter
- //
- // Copyright (C) 2002, Brazil. All rights reserved.
- //
- //
- // cMultiplayer.cpp: implementation of the cMultiplayer class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "cMultiplayer.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- cMultiplayer::cMultiplayer()
- {
- m_pdnSPInfo = NULL;
- m_pdnDeviceInfo = NULL;
- m_pDP = NULL;
- m_pDeviceAddress = NULL;
- m_iSPIndex = 0;
- m_iSerialDevice = NULL;
- m_iModemDevice = NULL;
- m_dwTcpPort = NULL;
- m_dwBaudRate = NULL;
- m_sFlowControl[0] = NULL;
- m_sParity[0] = NULL;
- m_sStopBits[0] = NULL;
- m_pHostList = NULL;
- m_iNumHosts = 0;
- m_bCanConnect = TRUE;
- m_iPlayerType = 0;
- m_dpnidHost = 0;
- m_dpnidSelf = 0;
- m_pDP = NULL;
- m_pHandler = NULL;
- }
- cMultiplayer::~cMultiplayer()
- {
- }
- BOOL cMultiplayer::Initialize()
- {
- HRESULT hr;
- hr = CoCreateInstance( CLSID_DirectPlay8Peer, NULL,
- CLSCTX_INPROC_SERVER,
- IID_IDirectPlay8Peer,
- (LPVOID*) &m_pDP);
- if(hr != 0)
- {
- #ifdef _DEBUG
- Log("HRES: %d - %sn", hr, DXGetErrorString8(hr));
- #endif
- return FALSE;
- }
- // Initialize DirectPlay
- hr = m_pDP->Initialize(this, StaticDirectPlayMessageHandler, 0);
- if(hr != 0)
- {
- #ifdef _DEBUG
- Log("HRES: %d - %sn", hr, DXGetErrorString8(hr));
- #endif
- return FALSE;
- }
- m_lstPlayers.clear();
- InitializeCriticalSection(&m_csMessages);
- m_dpnidSelf = 0;
- m_dpnidHost = 0;
- return TRUE;
- }
- HRESULT WINAPI cMultiplayer::StaticDirectPlayMessageHandler(
- PVOID pvUserContext,
- DWORD dwMessageId,
- PVOID pMsgBuffer )
- {
- return ((cMultiplayer*)pvUserContext)->DirectPlayMessageHandler(pvUserContext, dwMessageId, pMsgBuffer);
- }
- HRESULT WINAPI cMultiplayer::DirectPlayMessageHandler(
- PVOID pvUserContext,
- DWORD dwMessageId,
- PVOID pMsgBuffer )
- {
- switch( dwMessageId )
- {
- case DPN_MSGID_CREATE_PLAYER:
- DPNMSG_CREATE_PLAYER* pCreatePlayer;
- pCreatePlayer = (DPNMSG_CREATE_PLAYER*)pMsgBuffer;
- if(m_iPlayerType == PLAYERTYPE_HOST)
- {
- PLAYER_INFO* pPlayerInfo;
- pPlayerInfo = new PLAYER_INFO;
- pPlayerInfo->dpnidPlayer = pCreatePlayer->dpnidPlayer;
- PLAYERINFO_VECTOR::iterator theIterator;
- BOOL bAdd = TRUE;
- for(theIterator = m_lstPlayers.begin();theIterator != m_lstPlayers.end();theIterator++)
- {
- if( (*theIterator)->dpnidPlayer == pCreatePlayer->dpnidPlayer)
- bAdd = FALSE;
- }
- if(bAdd)
- {
- if(m_dpnidSelf == 0)
- m_dpnidSelf = pCreatePlayer->dpnidPlayer;
- else
- m_lstPlayers.push_back(pPlayerInfo);
- #ifdef _DEBUG
- Log("DPN_MSGID_CREATE_PLAYER: %dn", pCreatePlayer->dpnidPlayer);
- #endif
- }
- }
- else
- {
- if(m_dpnidSelf == 0)
- m_dpnidSelf = pCreatePlayer->dpnidPlayer;
- #ifdef _DEBUG
- Log("DPN_MSGID_CREATE_PLAYER SELF: %dn", m_dpnidSelf);
- #endif
- FindHost();
- }
- break;
- case DPN_MSGID_CONNECT_COMPLETE:
- DPNMSG_CONNECT_COMPLETE* pConnect;
- pConnect = (DPNMSG_CONNECT_COMPLETE*)pMsgBuffer;
- //m_iMyId = *((int*)pConnect->pvApplicationReplyData);
- #ifdef _DEBUG
- Log("DPN_MSGID_CONNECT_COMPLETEn");
- #endif
- break;
- case DPN_MSGID_INDICATE_CONNECT:
- #ifdef _DEBUG
- Log("DPN_MSGID_INDICATE_CONNECT: %dn", m_bCanConnect);
- #endif
- if(m_bCanConnect)
- return DPN_OK;
- else
- return DPNERR_GENERIC;
- break;
- case DPN_MSGID_ENUM_HOSTS_QUERY:
- return DPN_OK;
- break;
- case DPN_MSGID_ENUM_HOSTS_RESPONSE:
- PDPNMSG_ENUM_HOSTS_RESPONSE pEnumHostsResponseMsg;
- const DPN_APPLICATION_DESC* pAppDesc;
- HOST_NODE* pHostNode;
- WCHAR* pwszSession;
- pwszSession = NULL;
- pHostNode = NULL;
-
- pEnumHostsResponseMsg = (PDPNMSG_ENUM_HOSTS_RESPONSE) pMsgBuffer;
- pAppDesc = pEnumHostsResponseMsg->pApplicationDescription;
-
- // Insert each host response if it isn't already present
- //EnterCriticalSection(&g_csHostList);
-
- for (pHostNode = m_pHostList; pHostNode; pHostNode = pHostNode->pNext)
- {
- if( pAppDesc->guidInstance == pHostNode->pAppDesc->guidInstance)
- {
- // This host is already in the list
- pHostNode = NULL;
- goto Break_ENUM_HOSTS_RESPONSE;
- }
- }
-
- // This host session is not in the list then so insert it.
- pHostNode = new HOST_NODE;
- if( pHostNode == NULL)
- {
- goto Break_ENUM_HOSTS_RESPONSE;
- }
-
- ZeroMemory(pHostNode, sizeof(HOST_NODE));
-
- // Copy the Host Address
- if( FAILED( pEnumHostsResponseMsg->pAddressSender->Duplicate(&pHostNode->pHostAddress ) ) )
- {
- goto Break_ENUM_HOSTS_RESPONSE;
- }
-
- pHostNode->pAppDesc = new DPN_APPLICATION_DESC;
-
- if( pHostNode == NULL)
- {
- goto Break_ENUM_HOSTS_RESPONSE;
- }
-
- ZeroMemory(pHostNode->pAppDesc, sizeof(DPN_APPLICATION_DESC));
- memcpy(pHostNode->pAppDesc, pAppDesc, sizeof(DPN_APPLICATION_DESC));
-
- // Null out all the pointers we aren't copying
- pHostNode->pAppDesc->pwszSessionName = NULL;
- pHostNode->pAppDesc->pwszPassword = NULL;
- pHostNode->pAppDesc->pvReservedData = NULL;
- pHostNode->pAppDesc->dwReservedDataSize = 0;
- pHostNode->pAppDesc->pvApplicationReservedData = NULL;
- pHostNode->pAppDesc->dwApplicationReservedDataSize = 0;
-
- m_iNumHosts++;
- if( pAppDesc->pwszSessionName)
- {
- pwszSession = new WCHAR[wcslen(pAppDesc->pwszSessionName) + 1];
-
- if( pwszSession)
- {
- wcscpy(pwszSession, pAppDesc->pwszSessionName);
- }
- }
-
- pHostNode->pwszSessionName = pwszSession;
-
- // Insert it onto the front of the list
- pHostNode->pNext = m_pHostList;// ? m_pHostList->pNext : NULL;
- m_pHostList = pHostNode;
- pHostNode = NULL;
-
- Break_ENUM_HOSTS_RESPONSE:
- //LeaveCriticalSection(&g_csHostList);
- if( pHostNode )
- {
- if(pHostNode->pHostAddress)
- pHostNode->pHostAddress->Release();
- if(pHostNode->pAppDesc)
- {
- delete[] pHostNode->pAppDesc;
- }
- delete pHostNode;
- }
- break;
- case DPN_MSGID_RECEIVE:
- {
- PDPNMSG_RECEIVE pMsg;
-
- PLAYER_MESSAGE* pPlayerMessage;
- pMsg = (PDPNMSG_RECEIVE) pMsgBuffer;
- //int iTmp;
- pPlayerMessage = (PLAYER_MESSAGE*) pMsg->pReceiveData;
- #ifdef _DEBUG
- //Log("DPN_MSGID_RECEIVE - dwSize:%d dwDataSize: %d Type %dn", pMsg->dwSize, pMsg->dwReceiveDataSize, pPlayerMessage->m_iType);
- #endif
- if(m_pHandler)
- {
- m_pHandler->IncomingMessage(pPlayerMessage->m_iType, pMsg->dpnidSender, pMsg->pReceiveData+sizeof(int), pMsg->dwReceiveDataSize-sizeof(int));
- }
- break;
- }
- }
- return DPN_OK;
- }
- void cMultiplayer::EnumServiceProviders()
- {
- HRESULT hr;
- DWORD dwSize = 0;
- // 确定需要的buffer大小
- hr = m_pDP->EnumServiceProviders(NULL, NULL, NULL, &dwSize, &m_dwNumSP, 0);
- m_pdnSPInfo = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[dwSize];
- #ifdef _DEBUG
- Log("SP Size: %d NumSP: %dn", dwSize, m_dwNumSP);
- #endif
- //Fill the buffer with service provider information
- hr = m_pDP->EnumServiceProviders(NULL, NULL, m_pdnSPInfo, &dwSize, &m_dwNumSP, 0);
-
- DWORD dwSp;
- dwSp = GetRegDWValue("ServiceProvider");
- if(dwSp != NULL)
- {
- int i = 0;
- SetRegDWValue("ServiceProvider", (DWORD) i);
- for(i=0;i<(int)m_dwNumSP;i++)
- {
- if(*GetSPGuid(i) == CLSID_DP8SP_TCPIP)
- {
- SetRegDWValue("ServiceProvider", (DWORD) i);
- m_iSPIndex = i;
- return;
- }
- }
- }
- else
- {
- m_iSPIndex = dwSp;
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_MODEM ||
- *GetSPGuid(m_iSPIndex) == CLSID_DP8SP_SERIAL)
- {
- EnumDevices();
- }
- }
- void cMultiplayer::Destroy()
- {
- HRESULT hr;
- if(m_pdnSPInfo)
- delete[] m_pdnSPInfo;
- if(m_pdnDeviceInfo)
- delete[] m_pdnDeviceInfo;
-
- if(m_pDP)
- {
- hr = m_pDP->Close(0);
- }
- if(m_pDeviceAddress)
- m_pDeviceAddress->Release();
- if(m_pDP)
- {
- m_pDP->Release();
- m_pDP = NULL;
- }
- if(m_pHostList)
- {
- HOST_NODE* pTmp;
- pTmp = m_pHostList;
- while(pTmp != NULL)
- {
- pTmp = m_pHostList->pNext;
- delete m_pHostList;
- m_pHostList = pTmp;
- }
- }
- PLAYERINFO_VECTOR::iterator theIterator;
- for(theIterator = m_lstPlayers.begin();theIterator != m_lstPlayers.end();theIterator++)
- {
- delete *theIterator;
- }
- m_lstPlayers.clear();
- PLAYERMESSAGE_ELEMENT MessageIterator;
- for(MessageIterator = m_lstMessages.begin();MessageIterator != m_lstMessages.end();MessageIterator++)
- {
- free( (*MessageIterator)->pReceiveData);
- delete *MessageIterator;
- }
- m_lstMessages.clear();
- }
- long cMultiplayer::GetNumSPs()
- {
- return m_dwNumSP;
- }
- char* cMultiplayer::GetSPName(int iIndex)
- {
- if(iIndex >= (int) m_dwNumSP)
- return NULL;
- DPN_SERVICE_PROVIDER_INFO* pSPInfoEnum;
- pSPInfoEnum = m_pdnSPInfo;
- int iTmp = 0;
- while(iTmp != iIndex)
- {
- pSPInfoEnum++;
- iTmp++;
- }
- if(pSPInfoEnum->guid == CLSID_DP8SP_MODEM)
- return "MODEM";
- if(pSPInfoEnum->guid == CLSID_DP8SP_TCPIP)
- return "TCP-IP NETWORK";
- if(pSPInfoEnum->guid == CLSID_DP8SP_IPX)
- return "IPX NETWORK";
- if(pSPInfoEnum->guid == CLSID_DP8SP_SERIAL)
- return "SERIAL PORT";
-
- /*case CLSID_DP8SP_TCPIP:
- return "TCPIP NETWORK";
- case CLSID_DP8SP_SERIAL:
- return "SERIAL PORT";*/
- return NULL;
- }
- GUID* cMultiplayer::GetSPGuid(int iIndex)
- {
- if(iIndex >= (int) m_dwNumSP)
- return NULL;
- DPN_SERVICE_PROVIDER_INFO* pSPInfoEnum;
- pSPInfoEnum = m_pdnSPInfo;
- int iTmp = 0;
- while(iTmp != iIndex)
- {
- pSPInfoEnum++;
- iTmp++;
- }
- return &pSPInfoEnum->guid;
- }
- int cMultiplayer::GetNumSPOptions()
- {
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_MODEM)
- return 2;
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_TCPIP)
- return 1;
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_SERIAL)
- return 5;
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_IPX)
- return 1;
-
- return 0;
- }
- void cMultiplayer::NextSP()
- {
- m_iSPIndex++;
- if(m_iSPIndex >= (int) m_dwNumSP)
- m_iSPIndex = 0;
- SetRegDWValue("ServiceProvider", (DWORD) m_iSPIndex);
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_MODEM ||
- *GetSPGuid(m_iSPIndex) == CLSID_DP8SP_SERIAL)
- {
- EnumDevices();
- }
- }
- int cMultiplayer::GetSPIndex()
- {
- return m_iSPIndex;
- }
- void cMultiplayer::GetSPOption(int iOption, char* szBuffer, DWORD dwBufferSize)
- {
- DPN_SERVICE_PROVIDER_INFO* pDeviceInfoEnum;
- pDeviceInfoEnum = m_pdnDeviceInfo;
- int iTmp = 0;
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_MODEM)
- {
- switch(iOption)
- {
- case 0:
- return;
- case 1:
- return;
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_TCPIP)
- {
- switch(iOption)
- {
- case 0:
- if(!m_dwTcpPort)
- m_dwTcpPort = GetRegDWValue("TcpIpPort");
- if(!m_dwTcpPort)
- {
- m_dwTcpPort = -1;
- SetRegDWValue("TcpIpPort", m_dwTcpPort);
- }
- if(m_dwTcpPort == -1)
- sprintf(szBuffer, "DEFAULT");
- else
- sprintf(szBuffer, "%d", m_dwTcpPort);
- break;
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_SERIAL)
- {
- switch(iOption)
- {
- case 0:
- if(!m_iSerialDevice)
- m_iSerialDevice = GetRegDWValue("SerialDevice");
- if(!m_iSerialDevice)
- {
- m_iSerialDevice = 0;
- SetRegDWValue("SerialDevice", m_iSerialDevice);
- }
- while(iTmp != m_iSerialDevice)
- {
- pDeviceInfoEnum++;
- iTmp++;
- }
- sprintf(szBuffer, "%S", pDeviceInfoEnum->pwszName);
- break;
- case 1:
- if(!m_dwBaudRate)
- m_dwBaudRate = GetRegDWValue("BaudRate");
- if(!m_dwBaudRate)
- {
- m_dwBaudRate = DPNA_BAUD_RATE_9600;
- SetRegDWValue("BaudRate", m_dwBaudRate);
- }
- sprintf(szBuffer, "%d BPS", m_dwBaudRate);
- break;
- case 2:
- if(!m_sFlowControl[0])
- GetRegStringValue("FlowControl", m_sFlowControl, 10);
- if(!m_sFlowControl[0])
- {
- sprintf(m_sFlowControl, "%S", DPNA_FLOW_CONTROL_NONE);
- SetRegStringValue("FlowControl", m_sFlowControl);
- }
- sprintf(szBuffer, "%s", m_sFlowControl);
- break;
- case 3:
- if(!m_sParity[0])
- GetRegStringValue("Parity", m_sParity, 10);
- if(!m_sParity[0])
- {
- sprintf(m_sParity, "%S", DPNA_PARITY_NONE);
- SetRegStringValue("Parity", m_sParity);
- }
- sprintf(szBuffer, "%s", m_sParity);
- break;
- case 4:
- if(!m_sStopBits[0])
- GetRegStringValue("StopBits", m_sStopBits, 10);
- if(!m_sStopBits[0])
- {
- sprintf(m_sStopBits, "%S", DPNA_STOP_BITS_ONE);
- SetRegStringValue("StopBits", m_sStopBits);
- }
- sprintf(szBuffer, "%s", m_sStopBits);
- break;
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_IPX)
- {
- switch(iOption)
- {
- case 0:
- break;
- }
- }
- return;
- }
- char* cMultiplayer::GetSPOptionName(int iOption)
- {
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_MODEM)
- {
- switch(iOption)
- {
- case 0:
- return "DEVICE";
- case 1:
- return "PHONE NUMBER";
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_TCPIP)
- {
- switch(iOption)
- {
- case 0:
- return "PORT NUMBER";
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_SERIAL)
- {
- switch(iOption)
- {
- case 0:
- return "DEVICE";
- case 1:
- return "BAUD RATE";
- case 2:
- return "FLOW CONTROL";
- case 3:
- return "PARITY";
- case 4:
- return "STOP BITS";
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_IPX)
- {
- switch(iOption)
- {
- case 0:
- return "PORT NUMBER";
- }
- }
- return NULL;
- }
- int cMultiplayer::GetSPOptionType(int iOption)
- {
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_MODEM)
- {
- switch(iOption)
- {
- case 0:
- return 0;
- case 1:
- return 1;
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_TCPIP)
- {
- switch(iOption)
- {
- case 0:
- return 1;
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_SERIAL)
- {
- return 0;
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_IPX)
- {
- return 1;
- }
- return 0;
- }
- void cMultiplayer::EnumDevices()
- {
- if(m_pdnDeviceInfo)
- delete[] m_pdnDeviceInfo;
-
- HRESULT hr;
- DWORD dwSize = 0;
- // Determine the required buffer size
- hr = m_pDP->EnumServiceProviders(GetSPGuid(m_iSPIndex), NULL, NULL, &dwSize, &m_dwNumDevices, 0);
- #ifdef _DEBUG
- Log("Devices: Size: %d NumDevices: %dn", dwSize, m_dwNumSP);
- #endif
- m_pdnDeviceInfo = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[dwSize];
- //Fill the buffer with service provider information
- hr = m_pDP->EnumServiceProviders(GetSPGuid(m_iSPIndex), NULL, m_pdnDeviceInfo, &dwSize, &m_dwNumDevices, 0);
- }
- char* cMultiplayer::GetRegStringValue(char* lpszValueName, char* szBuffer, DWORD dwBufLen)
- {
- HKEY hKey;
- LONG lRet;
- if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
- "SOFTWARE\RaceX",
- 0,
- KEY_QUERY_VALUE,
- &hKey) != ERROR_SUCCESS)
- return NULL;
- lRet = RegQueryValueEx(hKey,
- lpszValueName,
- NULL,
- NULL,
- (LPBYTE)szBuffer,
- &dwBufLen);
- if(lRet != ERROR_SUCCESS) return NULL;
- RegCloseKey(hKey);
- return szBuffer;
- }
- DWORD cMultiplayer::GetRegDWValue(char* lpszValueName)
- {
- DWORD dwSize=sizeof(DWORD), dwValue, dwType=REG_DWORD;
- HKEY hKey;
- LONG lRet;
- if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
- "SOFTWARE\RaceX",
- 0,
- KEY_QUERY_VALUE,
- &hKey) != ERROR_SUCCESS)
- return NULL;
- lRet=RegQueryValueEx(hKey,
- lpszValueName,
- 0,
- &dwType,
- (LPBYTE)&dwValue,
- &dwSize );
- if(lRet != ERROR_SUCCESS) return NULL;
- RegCloseKey(hKey);
- return dwValue;
- }
- void cMultiplayer::SetRegDWValue(char* lpszValueName, DWORD dwValue)
- {
- DWORD dwSize=sizeof(DWORD), dwType=REG_DWORD;
- HKEY hKey;
- LONG lRet;
- if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
- "SOFTWARE\RaceX",
- 0,
- KEY_WRITE,
- &hKey) != ERROR_SUCCESS)
- {
- if(RegCreateKey(HKEY_LOCAL_MACHINE,
- "SOFTWARE\RaceX", &hKey) != ERROR_SUCCESS)
- return;
- }
- lRet=RegSetValueEx(hKey,
- lpszValueName,
- 0,
- dwType,
- (LPBYTE)&dwValue,
- dwSize );
- if(lRet != ERROR_SUCCESS) return;
- RegCloseKey(hKey);
- return;
- }
- void cMultiplayer::SetRegStringValue(char* lpszValueName, char* lpszValue)
- {
- DWORD dwSize=sizeof(DWORD), dwType=REG_SZ;
- HKEY hKey;
- LONG lRet;
- if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
- "SOFTWARE\RaceX",
- 0,
- KEY_WRITE,
- &hKey) != ERROR_SUCCESS)
- {
- if(RegCreateKey(HKEY_LOCAL_MACHINE,
- "SOFTWARE\RaceX", &hKey) != ERROR_SUCCESS)
- return;
- }
- dwSize = strlen(lpszValue) + 1;
- lRet=RegSetValueEx(hKey,
- lpszValueName,
- 0,
- dwType,
- (LPBYTE)lpszValue,
- dwSize );
- if(lRet != ERROR_SUCCESS) return;
- RegCloseKey(hKey);
- return;
- }
- void cMultiplayer::AdvanceSPOption(int iOption)
- {
- char buffer[20];
- int iTmp = 0;
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_MODEM)
- {
- switch(iOption)
- {
- case 0:
- m_iModemDevice++;
- if(m_iModemDevice >= (int)m_dwNumDevices)
- m_iModemDevice = 0;
- SetRegDWValue("ModemDevice", (DWORD) m_iModemDevice);
- return;
- case 1:
- return;
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_TCPIP)
- {
- return;
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_SERIAL)
- {
- switch(iOption)
- {
- case 0:
- m_iSerialDevice++;
- if(m_iSerialDevice >= (int)m_dwNumDevices)
- m_iSerialDevice = 0;
- SetRegDWValue("SerialDevice", (DWORD) m_iSerialDevice);
- break;
- case 1:
- switch(m_dwBaudRate)
- {
- case DPNA_BAUD_RATE_9600:
- m_dwBaudRate = DPNA_BAUD_RATE_14400;
- SetRegDWValue("BaudRate", m_dwBaudRate);
- break;
- case DPNA_BAUD_RATE_14400:
- m_dwBaudRate = DPNA_BAUD_RATE_19200;
- SetRegDWValue("BaudRate", m_dwBaudRate);
- break;
- case DPNA_BAUD_RATE_19200:
- m_dwBaudRate = DPNA_BAUD_RATE_38400;
- SetRegDWValue("BaudRate", m_dwBaudRate);
- break;
- case DPNA_BAUD_RATE_38400:
- m_dwBaudRate = DPNA_BAUD_RATE_56000;
- SetRegDWValue("BaudRate", m_dwBaudRate);
- break;
- case DPNA_BAUD_RATE_56000:
- m_dwBaudRate = DPNA_BAUD_RATE_9600;
- SetRegDWValue("BaudRate", m_dwBaudRate);
- break;
- }
- break;
- case 2:
- sprintf(buffer, "%S", DPNA_FLOW_CONTROL_NONE);
- if(strcmp(m_sFlowControl, buffer) == 0)
- {
- sprintf(m_sFlowControl, "%S", DPNA_FLOW_CONTROL_DTR);
- SetRegStringValue("FlowControl", m_sFlowControl);
- break;
- }
- sprintf(buffer, "%S", DPNA_FLOW_CONTROL_DTR);
- if(strcmp(m_sFlowControl, buffer) == 0)
- {
- sprintf(m_sFlowControl, "%S", DPNA_FLOW_CONTROL_XONXOFF);
- SetRegStringValue("FlowControl", m_sFlowControl);
- break;
- }
- sprintf(buffer, "%S", DPNA_FLOW_CONTROL_XONXOFF);
- if(strcmp(m_sFlowControl, buffer) == 0)
- {
- sprintf(m_sFlowControl, "%S", DPNA_FLOW_CONTROL_RTSDTR);
- SetRegStringValue("FlowControl", m_sFlowControl);
- break;
- }
- sprintf(buffer, "%S", DPNA_FLOW_CONTROL_RTSDTR);
- if(strcmp(m_sFlowControl, buffer) == 0)
- {
- sprintf(m_sFlowControl, "%S", DPNA_FLOW_CONTROL_RTS);
- SetRegStringValue("FlowControl", m_sFlowControl);
- break;
- }
- sprintf(buffer, "%S", DPNA_FLOW_CONTROL_RTS);
- if(strcmp(m_sFlowControl, buffer) == 0)
- {
- sprintf(m_sFlowControl, "%S", DPNA_FLOW_CONTROL_NONE);
- SetRegStringValue("FlowControl", m_sFlowControl);
- break;
- }
- break;
- case 3:
- sprintf(buffer, "%S", DPNA_PARITY_NONE);
- if(strcmp(m_sParity, buffer) == 0)
- {
- sprintf(m_sParity, "%S", DPNA_PARITY_MARK);
- SetRegStringValue("Parity", m_sParity);
- break;
- }
- sprintf(buffer, "%S", DPNA_PARITY_MARK);
- if(strcmp(m_sParity, buffer) == 0)
- {
- sprintf(m_sParity, "%S", DPNA_PARITY_EVEN);
- SetRegStringValue("Parity", m_sParity);
- break;
- }
- sprintf(buffer, "%S", DPNA_PARITY_EVEN);
- if(strcmp(m_sParity, buffer) == 0)
- {
- sprintf(m_sParity, "%S", DPNA_PARITY_SPACE);
- SetRegStringValue("Parity", m_sParity);
- break;
- }
- sprintf(buffer, "%S", DPNA_PARITY_SPACE);
- if(strcmp(m_sParity, buffer) == 0)
- {
- sprintf(m_sParity, "%S", DPNA_PARITY_ODD);
- SetRegStringValue("Parity", m_sParity);
- break;
- }
- sprintf(buffer, "%S", DPNA_PARITY_ODD);
- if(strcmp(m_sParity, buffer) == 0)
- {
- sprintf(m_sParity, "%S", DPNA_PARITY_NONE);
- SetRegStringValue("Parity", m_sParity);
- break;
- }
- break;
- case 4:
- sprintf(buffer, "%S", DPNA_STOP_BITS_ONE);
- if(strcmp(m_sStopBits, buffer) == 0)
- {
- sprintf(m_sStopBits, "%S", DPNA_STOP_BITS_ONE_FIVE);
- SetRegStringValue("StopBits", m_sStopBits);
- break;
- }
- sprintf(buffer, "%S", DPNA_STOP_BITS_ONE_FIVE);
- if(strcmp(m_sStopBits, buffer) == 0)
- {
- sprintf(m_sStopBits, "%S", DPNA_STOP_BITS_TWO);
- SetRegStringValue("StopBits", m_sStopBits);
- break;
- }
- sprintf(buffer, "%S", DPNA_STOP_BITS_TWO);
- if(strcmp(m_sStopBits, buffer) == 0)
- {
- sprintf(m_sStopBits, "%S", DPNA_STOP_BITS_ONE);
- SetRegStringValue("StopBits", m_sStopBits);
- break;
- }
- break;
- }
- }
- if(*GetSPGuid(m_iSPIndex) == CLSID_DP8SP_IPX)
- {
- return;
- }
- }
- BOOL cMultiplayer::Host(char *sSessionName)
- {
- m_iPlayerType = PLAYERTYPE_HOST;
- HRESULT hr;
- // Create our IDirectPlay8Address Device Address
- hr = CoCreateInstance( CLSID_DirectPlay8Address, NULL,
- CLSCTX_INPROC_SERVER,
- IID_IDirectPlay8Address,
- (LPVOID*) &m_pDeviceAddress );
-
- if(SUCCEEDED(hr))
- {
- }
-
- // Set the SP for our Device Address
- hr = m_pDeviceAddress->SetSP(GetSPGuid(m_iSPIndex));
- DPN_APPLICATION_DESC dpAppDesc;
- // Set up the application description.
- ZeroMemory(&dpAppDesc, sizeof(DPN_APPLICATION_DESC));
- dpAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
- dpAppDesc.guidApplication = g_guidApp;
- dpAppDesc.dwMaxPlayers = 4;
- WCHAR wstrSessionName[MAX_PATH];
- MultiByteToWideChar(CP_ACP, 0, sSessionName,
- strlen(sSessionName),
- wstrSessionName,
- MAX_PATH);
- dpAppDesc.pwszSessionName = new WCHAR[wcslen(wstrSessionName)+1];
- wcscpy( dpAppDesc.pwszSessionName, wstrSessionName );
- // We are now ready to host the application.
- hr = m_pDP->Host( &dpAppDesc, // AppDesc
- &m_pDeviceAddress, 1,// Device Address
- NULL, NULL, // Reserved
- NULL, // Player Context
- 0); // dwFlags
- delete[] dpAppDesc.pwszSessionName;
- return TRUE;
- }
- BOOL cMultiplayer::EnumSessions()
- {
- DPN_APPLICATION_DESC dpAppDesc;
- HRESULT hr;
- HOST_NODE* pHostNode = NULL;
- WCHAR* pwszURL = NULL;
- DWORD dwNumChars = 0;
- // Create our IDirectPlay8Address Device Address
- hr = CoCreateInstance( CLSID_DirectPlay8Address, NULL,
- CLSCTX_INPROC_SERVER,
- IID_IDirectPlay8Address,
- (LPVOID*) &m_pDeviceAddress );
- // Set the SP for our Device Address
- hr = m_pDeviceAddress->SetSP(GetSPGuid(m_iSPIndex));
- // TODO: Add your control notification handler code here
- ZeroMemory(&dpAppDesc, sizeof(DPN_APPLICATION_DESC));
- dpAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
- dpAppDesc.guidApplication = g_guidApp;
-
- m_iNumHosts = 0;
- hr = m_pDP->EnumHosts( &dpAppDesc, // pApplicationDesc
- NULL, // Host Address
- m_pDeviceAddress,// Device Address
- NULL, 0, // pvUserEnumData, size
- 4, // dwEnumCount
- 0, // dwRetryInterval
- 0, // dwTimeOut
- NULL, // pvUserContext
- NULL, // pAsyncHandle
- DPNENUMHOSTS_SYNC); // dwFlags
-
- return TRUE;
- }
- int cMultiplayer::GetNumSessions()
- {
- return m_iNumHosts;
- }
- void cMultiplayer::GetSessionName(int iIndex, char *sValue, int iSize)
- {
- HOST_NODE* pHostNode = NULL;
- pHostNode = m_pHostList;
- for(int i=0;i<iIndex;i++)
- {
- pHostNode = pHostNode->pNext;
- }
- sprintf(sValue, "%S", pHostNode->pwszSessionName);
- return;
- }
- void cMultiplayer::SetCanConnect(BOOL bValue)
- {
- m_bCanConnect = bValue;
- }
- BOOL cMultiplayer::GetCanConnect()
- {
- return m_bCanConnect;
- }
- void cMultiplayer::Connect(int iHost)
- {
- // TODO: Add your control notification handler code here
- DPN_APPLICATION_DESC dpnAppDesc;
- IDirectPlay8Address* pHostAddress = NULL;
- HRESULT hr;
- HOST_NODE* pHostNode = NULL;
- m_iPlayerType = PLAYERTYPE_PEER;
- m_dpnidSelf = 0;
- ZeroMemory(&dpnAppDesc, sizeof(DPN_APPLICATION_DESC));
- dpnAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
- dpnAppDesc.guidApplication = g_guidApp;
- pHostNode = m_pHostList;
- for(int i=0;i<iHost;i++)
- {
- pHostNode = pHostNode->pNext;
- }
- hr = pHostNode->pHostAddress->Duplicate(&pHostAddress);
- hr = m_pDP->Connect(&dpnAppDesc, // Application Description
- pHostAddress, // Host Address
- m_pDeviceAddress,
- NULL,
- NULL,
- NULL, 0,
- NULL,
- NULL,
- NULL,
- DPNCONNECT_SYNC);
- pHostAddress->Release();
- }
- BOOL cMultiplayer::IsHosting()
- {
- if(m_iPlayerType == PLAYERTYPE_HOST)
- return TRUE;
- else
- return FALSE;
- }
- int cMultiplayer::GetNumConnected()
- {
- return m_lstPlayers.size();
- }
- PLAYER_INFO* cMultiplayer::GetPlayerInfo(int iIndex)
- {
- return m_lstPlayers[iIndex];
- }
- void cMultiplayer::SendTo(DPNID dpnidTarget, int iType, LPBYTE lpBuffer, DWORD dwSize, DWORD dwFlags)
- {
- DPNHANDLE hAsync;
- DPNHANDLE* phAsync;
- DPN_BUFFER_DESC dpnBuffer;
- HRESULT hr;
- LPBYTE pTmpBuffer;
- pTmpBuffer = (LPBYTE) malloc(sizeof(int) + dwSize);
- memcpy(pTmpBuffer, &iType, sizeof(int));
- memcpy(pTmpBuffer+sizeof(int), lpBuffer, dwSize);
- dpnBuffer.pBufferData = pTmpBuffer;
- dpnBuffer.dwBufferSize = sizeof(int) + dwSize;
- if(dwFlags & DPNSEND_SYNC)
- phAsync = NULL;
- else
- phAsync = &hAsync;
- hr = m_pDP->SendTo( dpnidTarget, // dpnid
- &dpnBuffer, // pBufferDesc
- 1, // cBufferDesc
- 0, // dwTimeOut
- NULL, // pvAsyncContext
- phAsync, // pvAsyncHandle
- dwFlags ); // dwFlags
- free(pTmpBuffer);
- }
- int cMultiplayer::GetNumMessages()
- {
- DWORD dwTemp;
- dwTemp = m_lstMessages.size();
- return dwTemp;
- }
- PLAYER_MESSAGE* cMultiplayer::GetMessage(int iIndex)
- {
- PLAYERMESSAGE_ELEMENT theIterator;
- EnterCriticalSection(&m_csMessages);
- theIterator = m_lstMessages.begin();
- for(int i=0;i<iIndex;i++)
- theIterator++;
- return (*theIterator);
- }
- void cMultiplayer::RemoveMessage(PLAYER_MESSAGE *pMessage)
- {
- free(pMessage->pReceiveData);
- m_lstMessages.remove(pMessage);
- delete pMessage;
- }
- int cMultiplayer::GetHost()
- {
- return m_dpnidHost;
- }
- void cMultiplayer::FindHost()
- {
- DWORD dwSize = NULL, dwSize2 = NULL;
- DPNID* pDPNIDs = NULL;
- DPN_PLAYER_INFO *pPlayerInfo = NULL;
- m_pDP->EnumPlayersAndGroups(pDPNIDs, &dwSize, DPNENUM_PLAYERS);
- pDPNIDs = (DPNID*)malloc(dwSize * sizeof(DPNID));
- m_pDP->EnumPlayersAndGroups(pDPNIDs, &dwSize, DPNENUM_PLAYERS);
- for(int i=0;i<dwSize;i++)
- {
- dwSize2 = NULL;
- m_pDP->GetPeerInfo(pDPNIDs[i], pPlayerInfo, &dwSize2, 0);
- pPlayerInfo = (DPN_PLAYER_INFO*) malloc(dwSize2);
- m_pDP->GetPeerInfo(pDPNIDs[i], pPlayerInfo, &dwSize2, 0);
- if(pPlayerInfo->dwPlayerFlags & DPNPLAYER_HOST)
- {
- m_dpnidHost = pDPNIDs[i];
- free(pPlayerInfo);
- break;
- }
- free(pPlayerInfo);
- }
- free(pDPNIDs);
- #ifdef _DEBUG
- Log("FIND HOST: %dn", m_dpnidHost);
- #endif
- }
- int cMultiplayer::GetMyId()
- {
- return m_dpnidSelf;
- }
- void cMultiplayer::ReleaseMessage()
- {
- #ifdef _DEBUG
- //Log("LEAVING CRITICAL SECTIONn");
- #endif
- LeaveCriticalSection(&m_csMessages);
- }
- void cMultiplayer::SetHandler(cMessageHandler *pHandler)
- {
- m_pHandler = pHandler;
- }