home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / directplay / dxvbmessenger / server / frmserver.frm (.txt) next >
Encoding:
Visual Basic Form  |  2000-10-02  |  16.5 KB  |  381 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  3. Begin VB.Form frmServer 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "vbMessenger Server"
  6.    ClientHeight    =   4515
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   3645
  10.    Icon            =   "frmServer.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   4515
  15.    ScaleWidth      =   3645
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.Timer tmrLogon 
  18.       Interval        =   50
  19.       Left            =   3660
  20.       Top             =   960
  21.    End
  22.    Begin VB.Timer tmrLogoff 
  23.       Interval        =   50
  24.       Left            =   3660
  25.       Top             =   480
  26.    End
  27.    Begin VB.ListBox lstUsers 
  28.       Height          =   3765
  29.       Left            =   60
  30.       TabIndex        =   1
  31.       Top             =   360
  32.       Width           =   3495
  33.    End
  34.    Begin MSComctlLib.StatusBar sBar 
  35.       Align           =   2  'Align Bottom
  36.       Height          =   375
  37.       Left            =   0
  38.       TabIndex        =   0
  39.       Top             =   4140
  40.       Width           =   3645
  41.       _ExtentX        =   6429
  42.       _ExtentY        =   661
  43.       Style           =   1
  44.       SimpleText      =   " "
  45.       _Version        =   393216
  46.       BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628} 
  47.          NumPanels       =   1
  48.          BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
  49.          EndProperty
  50.       EndProperty
  51.    End
  52.    Begin VB.Label Label1 
  53.       BackStyle       =   0  'Transparent
  54.       Caption         =   "Users currently in this session"
  55.       Height          =   255
  56.       Left            =   60
  57.       TabIndex        =   2
  58.       Top             =   60
  59.       Width           =   3495
  60.    End
  61.    Begin VB.Menu mnuPop 
  62.       Caption         =   "PopUp"
  63.       Visible         =   0   'False
  64.       Begin VB.Menu mnuShow 
  65.          Caption         =   "Show"
  66.       End
  67.       Begin VB.Menu mnuSep 
  68.          Caption         =   "-"
  69.       End
  70.       Begin VB.Menu mnuExit 
  71.          Caption         =   "Exit"
  72.       End
  73.    End
  74. Attribute VB_Name = "frmServer"
  75. Attribute VB_GlobalNameSpace = False
  76. Attribute VB_Creatable = False
  77. Attribute VB_PredeclaredId = True
  78. Attribute VB_Exposed = False
  79. Option Explicit
  80. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  81. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  82. '  File:       frmServer.frm
  83. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  84. Implements DirectPlay8Event
  85. Private mfExit As Boolean
  86. Private mfLogoffTimer As Boolean
  87. Private msLogoffName As String
  88. Private mfLogonTimer As Boolean
  89. Private msLogonName As String
  90. Private Sub StartServer()
  91.     Dim appdesc As DPN_APPLICATION_DESC
  92.     'Now set up the app description
  93.     With appdesc
  94.         .guidApplication = AppGuid
  95.         .lMaxPlayers = 1000 'This seems like a nice round number
  96.         .SessionName = "vbMessengerServer"
  97.         .lFlags = DPNSESSION_CLIENT_SERVER Or DPNSESSION_NODPNSVR 'We must pass the client server flags if we are a server
  98.     End With
  99.     'Now set up our address value
  100.     dpa.SetSP DP8SP_TCPIP
  101.     dpa.AddComponentLong DPN_KEY_PORT, glDefaultPort 'Use a specific port
  102.     'Now start the server
  103.     dps.Host appdesc, dpa
  104.     UpdateText "Server running...  (" & CStr(glNumPlayers) & "/1000 clients connected.)"
  105. End Sub
  106. Private Sub Form_Load()
  107.     dps.RegisterMessageHandler Me
  108.     'Lets put an icon in the system tray
  109.     With sysIcon
  110.         .cbSize = LenB(sysIcon)
  111.         .hwnd = Me.hwnd
  112.         .uFlags = NIF_DOALL
  113.         .uCallbackMessage = WM_MOUSEMOVE
  114.         .hIcon = Me.Icon
  115.         .sTip = "Server running...  (" & CStr(glNumPlayers) & "/1000 clients connected.)" & vbNullChar
  116.     End With
  117.     Shell_NotifyIcon NIM_ADD, sysIcon
  118.     'Open the database
  119.     OpenClientDatabase
  120.     'Start the server
  121.     StartServer
  122. End Sub
  123. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  124.     Dim ShellMsg As Long
  125.     ShellMsg = X / Screen.TwipsPerPixelX
  126.     Select Case ShellMsg
  127.     Case WM_LBUTTONDBLCLK
  128.         mnuShow_Click
  129.     Case WM_RBUTTONUP
  130.         'Show the menu
  131.         PopupMenu mnuPop, , , , mnuShow
  132.     End Select
  133. End Sub
  134. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  135.     If Not mfExit Then
  136.         Cancel = 1
  137.         Me.Hide
  138.     End If
  139. End Sub
  140. Private Sub Form_Unload(Cancel As Integer)
  141.     'Remove the icon from the system tray
  142.     Shell_NotifyIcon NIM_DELETE, sysIcon
  143.     'Close the database
  144.     CloseDownDB
  145.     'Cleanup the dplay objects
  146.     Cleanup
  147. End Sub
  148. Private Sub mnuExit_Click()
  149.     mfExit = True
  150.     Unload Me
  151. End Sub
  152. Private Sub mnuShow_Click()
  153.     Me.Visible = True
  154.     Me.SetFocus
  155. End Sub
  156. Private Sub UpdateText(sNewText As String)
  157.     sBar.SimpleText = sNewText
  158.     'modify our icon text
  159.     sysIcon.sTip = sNewText & vbNullChar
  160.     sysIcon.uFlags = NIF_TIP
  161.     Shell_NotifyIcon NIM_MODIFY, sysIcon
  162. End Sub
  163. Private Sub tmrLogoff_Timer()
  164.     'Log this user off
  165.     If mfLogoffTimer Then
  166.         LogInfo "(tmrLogoff) Entering timer"
  167.         NotifyFriendsImOffline msLogoffName
  168.         LogInfo "(tmrLogoff) outta here:"
  169.     End If
  170.     msLogoffName = vbNullString
  171.     mfLogoffTimer = False
  172. End Sub
  173. Private Sub tmrLogon_Timer()
  174.     If mfLogonTimer Then
  175.         mfLogonTimer = False
  176.         LogInfo "(tmrLogin) Notify Friends:"
  177.         NotifyFriendsImOnline msLogonName 'Tell everyone who has me marked as a friend that I'm online
  178.         LogInfo "(tmrLogin) GetFriends:"
  179.         GetFriendsOfMineOnline msLogonName 'Find out if any of my friends are online and tell me
  180.         LogInfo "(tmrLogin) outta here:"
  181.     End If
  182.     msLogonName = vbNullString
  183. End Sub
  184. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  185.     'VB requires that we must implement *every* member of this interface
  186. End Sub
  187. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  188.     'VB requires that we must implement *every* member of this interface
  189. End Sub
  190. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  191.     'VB requires that we must implement *every* member of this interface
  192. End Sub
  193. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  194.     'VB requires that we must implement *every* member of this interface
  195. End Sub
  196. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  197.     'VB requires that we must implement *every* member of this interface
  198. End Sub
  199. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  200.     'VB requires that we must implement *every* member of this interface
  201. End Sub
  202. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  203.     'VB requires that we must implement *every* member of this interface
  204. End Sub
  205. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  206.     Dim lCount As Long
  207.     On Local Error GoTo ErrOut 'So we don't get an InvalidPlayer error when checking on the host
  208.     'Update the DB to show a logoff
  209.     UpdateDBToShowLogoff lPlayerID
  210.     'Remove this player from our listbox
  211.     For lCount = lstUsers.ListCount - 1 To 0 Step -1
  212.         If lstUsers.ItemData(lCount) = lPlayerID Then
  213.             mfLogoffTimer = True
  214.             msLogoffName = lstUsers.List(lCount)
  215.             glNumPlayers = glNumPlayers - 1
  216.             lstUsers.RemoveItem lCount
  217.             Exit For
  218.         End If
  219.     Next
  220. ErrOut:
  221.     UpdateText "Server running...  (" & CStr(glNumPlayers) & "/1000 clients connected.)"
  222. End Sub
  223. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  224.     'VB requires that we must implement *every* member of this interface
  225. End Sub
  226. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  227.     'VB requires that we must implement *every* member of this interface
  228. End Sub
  229. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  230.     'VB requires that we must implement *every* member of this interface
  231. End Sub
  232. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  233.     'VB requires that we must implement *every* member of this interface
  234. End Sub
  235. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  236.     'VB requires that we must implement *every* member of this interface
  237. End Sub
  238. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  239.     'VB requires that we must implement *every* member of this interface
  240. End Sub
  241. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  242.     'We need to get each message we receive from a client, process it, and respond accordingly
  243.     Dim lMsg As Long, lOffset As Long
  244.     Dim oNewMsg() As Byte, lNewOffSet As Long
  245.     Dim sUsername As String, sPass As String
  246.     Dim lNewMsg As Long, fLoggedin As Boolean
  247.     Dim sChatMsg As String, sFromMsg As String
  248.     With dpnotify
  249.     GetDataFromBuffer .ReceivedData, lMsg, LenB(lMsg), lOffset
  250.     Select Case lMsg 'The server will only receive certain messages.  Handle those.
  251.     Case Msg_AddFriend 'They want to add a friend to their list
  252.         LogInfo "(AddFriend) Message Received"
  253.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  254.         If Not DoesUserExist(sUsername) Then
  255.             'This user does not exist, notify the person that they cannot be added
  256.             LogInfo "(AddFriend) User does not exist"
  257.             lNewMsg = Msg_FriendDoesNotExist
  258.             lNewOffSet = NewBuffer(oNewMsg)
  259.             AddDataToBuffer oNewMsg, lNewMsg, LenB(lNewMsg), lNewOffSet
  260.             dps.SendTo .idSender, oNewMsg, 0, 0
  261.         Else
  262.             'Great, add this user to our friend list
  263.             LogInfo "(AddFriend) User Exists, adding"
  264.             fLoggedin = AddFriend(.idSender, sUsername, True)
  265.             LogInfo "(AddFriend) Added to DB"
  266.             lNewMsg = Msg_FriendAdded
  267.             lNewOffSet = NewBuffer(oNewMsg)
  268.             AddDataToBuffer oNewMsg, lNewMsg, LenB(lNewMsg), lNewOffSet
  269.             AddStringToBuffer oNewMsg, sUsername, lNewOffSet
  270.             dps.SendTo .idSender, oNewMsg, 0, DPNSEND_SYNC
  271.             LogInfo "(AddFriend) Notified of add"
  272.             If fLoggedin Then
  273.                 LogInfo "(AddFriend) They're logged in too"
  274.                 lNewMsg = Msg_FriendLogon
  275.                 lNewOffSet = NewBuffer(oNewMsg)
  276.                 AddDataToBuffer oNewMsg, lNewMsg, LenB(lMsg), lNewOffSet
  277.                 AddStringToBuffer oNewMsg, sUsername, lNewOffSet
  278.                 dps.SendTo .idSender, oNewMsg, 0, 0
  279.             End If
  280.         End If
  281.     Case Msg_BlockFriend 'They want to block a friend from their list
  282.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  283.         If Not DoesUserExist(sUsername) Then
  284.             'This user does not exist, notify the person that they cannot be blocked
  285.             lNewMsg = Msg_BlockUserDoesNotExist
  286.             lNewOffSet = NewBuffer(oNewMsg)
  287.             AddDataToBuffer oNewMsg, lNewMsg, LenB(lNewMsg), lNewOffSet
  288.             dps.SendTo .idSender, oNewMsg, 0, 0
  289.         Else
  290.             'Great, block this user in our friend list
  291.             AddFriend .idSender, sUsername, False
  292.             lNewMsg = Msg_FriendBlocked
  293.             lNewOffSet = NewBuffer(oNewMsg)
  294.             AddDataToBuffer oNewMsg, lNewMsg, LenB(lNewMsg), lNewOffSet
  295.             AddStringToBuffer oNewMsg, sUsername, lNewOffSet
  296.             dps.SendTo .idSender, oNewMsg, 0, 0
  297.         End If
  298.     Case Msg_CreateNewAccount 'They want to create a new account
  299.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  300.         sPass = GetStringFromBuffer(.ReceivedData, lOffset)
  301.         If DoesUserExist(sUsername) Then
  302.             'This user already exists, inform the person so they can try a new name
  303.             lNewMsg = Msg_UserAlreadyExists
  304.             lNewOffSet = NewBuffer(oNewMsg)
  305.             AddDataToBuffer oNewMsg, lNewMsg, LenB(lNewMsg), lNewOffSet
  306.             dps.SendTo .idSender, oNewMsg, 0, 0
  307.         Else
  308.             'Great, this username doesn't exist.  Now lets add this user
  309.             AddUser sUsername, sPass, .idSender
  310.             'We don't need to inform anyone we are logged on, because
  311.             'no one could have us listed as a friend yet
  312.             
  313.             'Notify the user they logged on successfully
  314.             lNewMsg = Msg_LoginSuccess
  315.             lNewOffSet = NewBuffer(oNewMsg)
  316.             AddDataToBuffer oNewMsg, lNewMsg, LenB(lNewMsg), lNewOffSet
  317.             dps.SendTo .idSender, oNewMsg, 0, 0
  318.             
  319.             'Increment our user count
  320.             glNumPlayers = glNumPlayers + 1
  321.             'Add this user to our list of users currently online
  322.             lstUsers.AddItem sUsername
  323.             lstUsers.ItemData(lstUsers.ListCount - 1) = .idSender
  324.             UpdateText "Server running...  (" & CStr(glNumPlayers) & "/1000 clients connected.)"
  325.         End If
  326.     Case Msg_Login 'They have requested a login, check name/password
  327.         LogInfo "(Login) GetUserName/Pass"
  328.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  329.         sPass = GetStringFromBuffer(.ReceivedData, lOffset)
  330.         LogInfo "(Login) Try Login"
  331.         Select Case LogonUser(sUsername, sPass) 'Try to log on the user
  332.         Case LogonSuccess 'Great, they logged on
  333.             LogInfo "(Login) Success, updateDB"
  334.             UpdateDBToShowLogon sUsername, dpnotify.idSender 'Update the DB to show I'm online
  335.             'Notify the user they logged on successfully
  336.             lNewMsg = Msg_LoginSuccess
  337.             lNewOffSet = NewBuffer(oNewMsg)
  338.             AddDataToBuffer oNewMsg, lNewMsg, LenB(lNewMsg), lNewOffSet
  339.             LogInfo "(Logon) Send Success Msg"
  340.             dps.SendTo .idSender, oNewMsg, 0, 0
  341.             mfLogonTimer = True
  342.             msLogonName = sUsername
  343.             'Increment our user count
  344.             glNumPlayers = glNumPlayers + 1
  345.             'Add this user to our list of users currently online
  346.             lstUsers.AddItem sUsername
  347.             lstUsers.ItemData(lstUsers.ListCount - 1) = .idSender
  348.             UpdateText "Server running...  (" & CStr(glNumPlayers) & "/1000 clients connected.)"
  349.             
  350.         Case InvalidPassword 'Let the user know that they didn't type in the right password
  351.             'Notify the user they sent the wrong password
  352.             lNewMsg = Msg_InvalidPassword
  353.             lNewOffSet = NewBuffer(oNewMsg)
  354.             AddDataToBuffer oNewMsg, lNewMsg, LenB(lNewMsg), lNewOffSet
  355.             dps.SendTo .idSender, oNewMsg, 0, 0
  356.         Case AccountDoesNotExist 'Let the user know this account isn't in the DB
  357.             'Notify the user that this account doesn't exist
  358.             lNewMsg = Msg_InvalidUser
  359.             lNewOffSet = NewBuffer(oNewMsg)
  360.             AddDataToBuffer oNewMsg, lNewMsg, LenB(lNewMsg), lNewOffSet
  361.             dps.SendTo .idSender, oNewMsg, 0, 0
  362.         End Select
  363.     Case Msg_SendMessage 'They are trying to send a message to someone
  364.         sUsername = GetStringFromBuffer(.ReceivedData, lOffset)
  365.         sFromMsg = GetStringFromBuffer(.ReceivedData, lOffset)
  366.         sChatMsg = GetStringFromBuffer(.ReceivedData, lOffset)
  367.         LogInfo "(Send Message) User/From/Chat"
  368.         LogInfo "(Send Message) User " & sUsername
  369.         LogInfo "(Send Message) From " & sFromMsg
  370.         LogInfo "(Send Message) Chat " & sChatMsg
  371.         SendMessage sUsername, sFromMsg, sChatMsg
  372.     End Select
  373.     End With
  374. End Sub
  375. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  376.     'VB requires that we must implement *every* member of this interface
  377. End Sub
  378. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  379.     'VB requires that we must implement *every* member of this interface
  380. End Sub
  381.