home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / directplay / conferencer / moddplay.bas < prev    next >
Encoding:
BASIC Source File  |  2000-10-02  |  9.6 KB  |  274 lines

  1. Attribute VB_Name = "modDplay"
  2. Option Explicit
  3. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  4. '
  5. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  6. '
  7. '  File:       modDPlay.bas
  8. '
  9. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  10.  
  11. 'Here are all of the messages we can transfer in this app
  12. Public Enum vbMsgType
  13.     MsgChat 'We are talking in the chat channel
  14.     MsgWhisper 'We are whispering to someone in the chat channel
  15.     MsgAskToJoin 'We want to ask if we can join this session
  16.     MsgAcceptJoin 'Accept the call
  17.     MsgRejectJoin 'Reject the call
  18.     MsgCancelCall 'Cancel the call
  19.     MsgShowChat 'Show the chat window
  20.     MsgSendFileRequest 'Request a file transfer
  21.     MsgSendFileAccept 'Accept the file transfer
  22.     MsgSendFileDeny 'Deny the file transfer
  23.     MsgSendFileInfo 'File information (size)
  24.     MsgSendFilePart 'Send a chunk of the file
  25.     MsgAckFilePart 'Acknowledge the file part
  26.     MsgSendDrawPixel 'Send a drawn pixel
  27.     MsgSendDrawLine 'Send a drawn line
  28.     MsgShowWhiteBoard 'Show the whiteboard window
  29.     MsgClearWhiteBoard 'Clear the contents of the whiteboard
  30. End Enum
  31.  
  32. 'Win32 declares
  33. Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  34. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  35. Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
  36.  
  37. Public Type NOTIFYICONDATA
  38.     cbSize As Long
  39.     hwnd As Long
  40.     uID As Long
  41.     uFlags As Long
  42.     uCallbackMessage As Long
  43.     hIcon As Long
  44.     sTip As String * 64
  45. End Type
  46.     
  47. Public Const NIM_ADD = &H0
  48. Public Const NIM_MODIFY = &H1
  49. Public Const NIM_DELETE = &H2
  50. Public Const NIF_MESSAGE = &H1
  51. Public Const NIF_ICON = &H2
  52. Public Const NIF_TIP = &H4
  53. Public Const NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
  54. Public Const WM_MOUSEMOVE = &H200
  55. Public Const WM_LBUTTONDBLCLK = &H203
  56. Public Const WM_RBUTTONUP = &H205
  57.  
  58. 'Constants
  59. Public Const AppGuid = "{9073823A-A565-4865-87EC-19B93B014D27}"
  60. Public Const glDefaultPort As Long = 9987
  61.  
  62. 'DirectX variables
  63. Public dx As DirectX8
  64. Public dpp As DirectPlay8Peer
  65. Public dvClient As DirectPlayVoiceClient8
  66. Public dvServer As DirectPlayVoiceServer8
  67.  
  68. 'Window variables for this app
  69. Public ChatWindow As frmChat
  70. Public WhiteBoardWindow As frmWhiteBoard
  71. Public NetWorkForm As frmNetwork
  72.  
  73. 'Misc app variables
  74. Public sysIcon As NOTIFYICONDATA
  75. Public gsUserName As String
  76. Public glAsyncEnum As Long
  77. Public glMyPlayerID As Long
  78. Public gfHost As Boolean
  79.  
  80. Public Sub Main()
  81.     If App.PrevInstance Then
  82.         'We can only run one instance of this sample per machine since we
  83.         'specify a port to run this application on.  Only one application can
  84.         'be listening (hosting) on a particular port at any given time.
  85.         MsgBox "Only one instance of vbConferencer may be run at a time.", vbOKOnly Or vbInformation, "Only one"
  86.         Exit Sub
  87.     End If
  88.     'Set our username up
  89.     gsUserName = GetSetting("VBDirectPlay", "Defaults", "UserName", vbNullString)
  90.     If gsUserName = vbNullString Then
  91.         'If there is not a default username, then pick the currently
  92.         'logged on username
  93.         gsUserName = Space$(255)
  94.         GetUserName gsUserName, 255
  95.         gsUserName = Left$(gsUserName, InStr(gsUserName, Chr$(0)) - 1)
  96.     End If
  97.     Screen.MousePointer = vbHourglass
  98.     'Show the splash screen
  99.     frmSplash.Show
  100.     'Start the host
  101.     Set NetWorkForm = New frmNetwork
  102.     Load NetWorkForm
  103.     'We don't need it anymore
  104.     Unload frmSplash
  105.     Screen.MousePointer = vbNormal
  106.     NetWorkForm.Show
  107. End Sub
  108.  
  109. Public Sub InitDPlay()
  110.     Set dx = New DirectX8
  111.     Set dpp = dx.DirectPlayPeerCreate
  112. End Sub
  113.  
  114. Public Sub Cleanup()
  115.     On Error Resume Next
  116.     Set ChatWindow = Nothing
  117.     Set WhiteBoardWindow = Nothing
  118.     If Not dpp Is Nothing Then dpp.UnRegisterMessageHandler 'Stop taking messages
  119.     If Not (dvClient Is Nothing) Then dvClient.UnRegisterMessageHandler
  120.     If Not (dvServer Is Nothing) Then dvServer.UnRegisterMessageHandler
  121.     'Lets wait right here for a second letting things finish cleaning up
  122.     Sleep 50
  123.     DoEvents
  124.     If Not (dvClient Is Nothing) Then dvClient.Disconnect DVFLAGS_SYNC
  125.     If Not (dvServer Is Nothing) Then dvServer.StopSession 0
  126.     'Close our peer connection
  127.     If Not dpp Is Nothing Then dpp.Close
  128.     'Destroy the objects
  129.     Set dvClient = Nothing
  130.     Set dvServer = Nothing
  131.     'Lose references to peer and dx objects
  132.     Set dpp = Nothing
  133.     Set dx = Nothing
  134. End Sub
  135.  
  136. Public Sub StartHosting(MsgForm As Form)
  137.     Dim dpa As DirectPlay8Address
  138.     Dim oPlayer As DPN_PLAYER_INFO
  139.     Dim oAppDesc As DPN_APPLICATION_DESC
  140.     
  141.     'Make sure we're ready to host
  142.     Cleanup
  143.     InitDPlay
  144.     NetWorkForm.cmdHangup.Enabled = False
  145.     NetWorkForm.cmdCall.Enabled = True
  146.     gfHost = True
  147.     'Register the Message Handler
  148.     dpp.RegisterMessageHandler MsgForm
  149.     'Set the peer info
  150.     oPlayer.lInfoFlags = DPNINFO_NAME
  151.     oPlayer.Name = gsUserName
  152.     dpp.SetPeerInfo oPlayer, DPNOP_SYNC
  153.     'Create an address
  154.     Set dpa = dx.DirectPlayAddressCreate
  155.     'We will only be connecting via TCP/IP
  156.     dpa.SetSP DP8SP_TCPIP
  157.     dpa.AddComponentLong DPN_KEY_PORT, glDefaultPort
  158.     
  159.     'First set up our application description
  160.     With oAppDesc
  161.         .guidApplication = AppGuid
  162.         .lMaxPlayers = 10 'We don't want to overcrowd our 'room'
  163.         .lFlags = DPNSESSION_NODPNSVR
  164.     End With
  165.     'Start our host
  166.     dpp.Host oAppDesc, dpa
  167.     Set dpa = Nothing
  168.         
  169.     'After we've created the session and let's start
  170.     'the DplayVoice server
  171.     Dim oSession As DVSESSIONDESC
  172.  
  173.     'Create our DPlayVoice Server
  174.     Set dvServer = dx.DirectPlayVoiceServerCreate
  175.  
  176.     'Set up the Session
  177.     oSession.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  178.     oSession.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  179.     oSession.lSessionType = DVSESSIONTYPE_PEER
  180.     oSession.guidCT = vbNullString
  181.  
  182.     'Init and start the session
  183.     dvServer.Initialize dpp, 0
  184.     dvServer.StartSession oSession, 0
  185.     ConnectVoice
  186.     Set dpa = Nothing
  187. End Sub
  188.  
  189. Public Sub Connect(MsgForm As Form, ByVal sHost As String)
  190.     Dim dpa As DirectPlay8Address
  191.     Dim dpl As DirectPlay8Address
  192.     Dim oPlayer As DPN_PLAYER_INFO
  193.     Dim oAppDesc As DPN_APPLICATION_DESC
  194.     
  195.     'Try to connect to the host
  196.     'Make sure we're ready to connect
  197.     Cleanup
  198.     InitDPlay
  199.     NetWorkForm.cmdCall.Enabled = False
  200.     gfHost = False
  201.     'Register the Message Handler
  202.     dpp.RegisterMessageHandler MsgForm
  203.     'Set the peer info
  204.     oPlayer.lInfoFlags = DPNINFO_NAME
  205.     oPlayer.Name = gsUserName
  206.     dpp.SetPeerInfo oPlayer, DPNOP_SYNC
  207.     'Now try to enum hosts
  208.     
  209.     'Create an address
  210.     Set dpa = dx.DirectPlayAddressCreate
  211.     'We will only be connecting via TCP/IP
  212.     dpa.SetSP DP8SP_TCPIP
  213.     dpa.AddComponentString DPN_KEY_HOSTNAME, sHost 'We only want to enumerate connections on this host
  214.     dpa.AddComponentLong DPN_KEY_PORT, glDefaultPort
  215.     
  216.     Set dpl = dx.DirectPlayAddressCreate
  217.     'We will only be connecting via TCP/IP
  218.     dpl.SetSP DP8SP_TCPIP
  219.     
  220.     'First set up our application description
  221.     With oAppDesc
  222.         .guidApplication = AppGuid
  223.     End With
  224.     'Try to connect to this host
  225.     dpp.Connect oAppDesc, dpa, dpl, 0, ByVal 0&, 0
  226.     Set dpa = Nothing
  227.     Set dpl = Nothing
  228. End Sub
  229.  
  230. Public Sub ConnectVoice()
  231.     Dim oSound As DVSOUNDDEVICECONFIG
  232.     Dim oClient As DVCLIENTCONFIG
  233.     'Now create a client as well (so we can both talk and listen)
  234.     Set dvClient = dx.DirectPlayVoiceClientCreate
  235.     'Now let's create a client event..
  236.     dvClient.Initialize dpp, 0
  237.     'Set up our client and sound structs
  238.     oClient.lFlags = DVCLIENTCONFIG_AUTOVOICEACTIVATED Or DVCLIENTCONFIG_AUTORECORDVOLUME
  239.     oClient.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  240.     oClient.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  241.     oClient.lNotifyPeriod = 0
  242.     oClient.lThreshold = DVTHRESHOLD_UNUSED
  243.     oClient.lPlaybackVolume = DVPLAYBACKVOLUME_DEFAULT
  244.     oSound.hwndAppWindow = NetWorkForm.hwnd
  245.     
  246.     On Error Resume Next
  247.     'Connect the client
  248.     dvClient.Connect oSound, oClient, 0
  249.     If Err.Number = DVERR_RUN_SETUP Then    'The audio tests have not been run on this
  250.                                             'machine.  Run them now.
  251.         'we need to run setup first
  252.         Dim dvSetup As DirectPlayVoiceTest8
  253.         
  254.         Set dvSetup = dx.DirectPlayVoiceTestCreate
  255.         dvSetup.CheckAudioSetup vbNullString, vbNullString, NetWorkForm.hwnd, 0 'Check the default devices since that's what we'll be using
  256.         Set dvSetup = Nothing
  257.         dvClient.Connect oSound, oClient, 0
  258.     ElseIf Err.Number <> 0 And Err.Number <> DVERR_PENDING Then
  259.         MsgBox "Could not start DirectPlayVoice.  This sample will not have any voice capablities." & vbCrLf & "Error:" & CStr(Err.Number), vbOKOnly Or vbInformation, "No Voice"
  260.         Exit Sub
  261.     End If
  262.     On Error GoTo 0
  263.     Dim lTargets(0) As Long
  264.     
  265.     lTargets(0) = DVID_ALLPLAYERS
  266.     On Error Resume Next
  267.     'Connect the client
  268.     dvClient.SetTransmitTargets lTargets, 0
  269.     If Err.Number <> 0 And Err.Number <> DVERR_PENDING Then
  270.         MsgBox "Could not start DirectPlayVoice.  This sample will not have any voice capablities." & vbCrLf & "Error:" & CStr(Err.Number), vbOKOnly Or vbInformation, "No Voice"
  271.         Exit Sub
  272.     End If
  273. End Sub
  274.