Print Page | Close Window

IOCP (Not for Win 9x/ME) (Bugs Fixed)

Printed From: Mirage Source
Category: Tutorials
Forum Name: Submitted Tutorials
Forum Discription: Tutorial submissions for MSE are posted here, waiting for approval
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=120
Printed Date: 20 December 2006 at 6:03pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: IOCP (Not for Win 9x/ME) (Bugs Fixed)
Posted By: Sync
Subject: IOCP (Not for Win 9x/ME) (Bugs Fixed)
Date Posted: 11 February 2006 at 2:20pm
Originally posted by Verrigan

Difficulty (Copy & Paste): Medium 3/5
Difficulty (Understanding): Hard 5/5

As always, even though most don't pay attention to my warnings, you should read this tutorial in its entirety before adding it to your game. There's your warning.. I'm sure some of you will just start copying & pasting.. Don't expect to learn much from it if you do. :P (Believe me, I and many others will be able to tell if you did not read this tutorial fully before asking questions.) So take the warning, and read! If it appears that you failed to read the tutorial when you post your support request, you may get responses like "Try reading the tutorial before posting questions"... So the ball is in your court, so the saying goes. Take the opportunity to try to learn how it works before begging for assistance. :)

First, let me just say that (as the Subject says) this modification will make it so you can not run your server on Windows 9x or Windows ME. IOCP (Input/Output Completion Ports) only works on NT-Based operating systems. (NT/2K/XP/2003+) That said, I have a question for you. Win 9x/ME are not built to handle a lot of connections anyways, so why would you want to run an online game server on a Win 9x-Based machine to begin with? If you are programming on a Win 9x-Based machine, you will not be able to test the server on this machine after these modifications.

These changes are only on the server side, so will not affect the client in any way. You do not have to make ANY changes to the client to put IOCP in the server. :) (Yay)

Let's get down to business. This tutorial has been developed for Mirage Source Engine - Build 1. I will not support adding these changes to any other version of Mirage Source, including your own modified versions of Mirage Source Engine - Build 1. In reality, the copy/pasting difficulty of this tutorial is more like 1.5/5, but I set it to 3/5 because I am familiar with these forums and its usual users, and I know you will want to put this in your modified MSE, or older versions of Mirage Source. Again, I am telling you, I will NOT support putting this in your code. However, if you are adding this to MSE - Build 1, and are having difficulty, or find any bugs, feel free to post here. :)

Now, as always, you should [shadow=red,left][size=14pt]ALWAYS BACKUP YOUR SOURCE[/shadow] before adding any tutorial to your game. You are going to be making a lot of modifications, and you might get lost and/or forget something.. So, make sure you backup first!

I have made this tutorial in such a way that you will not have to make many modifications to the way the data is handled on the server. You will only have to make modifications to how sockets are handled, and the data received/sent through them.

This method of using IOCP requires a separate .DLL file. It is a COM object, written in C++ by Jetbyte. You can find the original .DLL file, its source, and documentation for it at http://www.jetbyte.com/portfolio-showarticle.asp?articleId=41&catId=1&subcatId=3 - http://www.jetbyte.com/portfolio-showarticle.asp?articleId=4 1&catId=1&subcatId=3 . Unfortunately, the .DLL/source available on Jetbyte's website will only allow you to write 1024 bytes to a socket, (Thanks, Misunderstood, for helping me figure this out on my test chat-server :)) and will not correctly set the requested IP address. So I modified the source to allow for up to 1048576 bytes to be written to a socket, which will likely never be reached, and the ability to correctly set the local IP address. :) You can download my modified .DLL from http://www.verrigan.net/iocp/COMSocketServer.dll - http://www.verrigan.net/iocp/COMSocketServer.dll .

Warning: With Jetbyte's original .DLL, you will not be able to set the local IP address to your network card's IP address. It will cause a fatal error, and the server will not work. (In it's original form, your local IP address for the server can only be "0.0.0.0") So, I suggest you use my .DLL which also allows for the sending of the larger packets. :) Also, I do not guarantee that your server will work properly with Jetbyte's .DLL, so if you have problems with it, either modify the source for it, and compile it yourself, or just download my modified version.

Whichever one you use, you will need to register the .DLL. There are a couple of ways you can do this. From the command prompt, you can type: regsvr32 <path to the file>. (i.e. If you put the .DLL in C:\MyGame, you would do: regsvr32 C:\MyGame\COMSocketServer.dll) Another way to do this is to go to your server's references, and browse to the .DLL file. When you add the file to your server's references, which you need to do anyway, it will automatically register the .DLL for you. The reason I told you both ways is because if you run the server on a different computer, you will need to register the .DLL on that other computer. :)

Now let's get on with the tutorial! (Yay)

Needed Files:
  • COMSocketServer.dll (See paragraphs above for downloads)


Files You Will Add
  • clsServer.cls

  • clsSocket.cls

  • colSockets.cls


Files to Modify
  • frmServer.frm

  • modConstants.bas

  • modGameLogic.bas

  • modGeneral.bas

  • modServerTCP


First let's start with the files you will need to add. They are all class modules, so just add 3 blank class modules to your server project, and give them the names you see above. (without the ".cls" - i.e. For the clsServer.cls class module, the name would be: clsServer) These files will be fairly easy, as they are all Copy & Paste. Please forgive me for the poor commenting. :P

[size=14pt]clsServer.cls - This will be the server object. The actual socket server will be initialized and stored in this object. So will our sockets collection. :)
Option Explicit
Private WithEvents m_Server As JBSOCKETSERVERLib.Server   'The Server
Public Sockets          ;     As col Sockets          ;        'The Socket Collection
Private Sub Class_Initialize()
  Set m_Server = JBSOCKETSERVERLib.CreateSocketServer(GAME_PORT, GAME_IP)
  Set Sockets = New colSockets
End Sub
Private Sub Class_Terminate()
  Set Sockets = Nothing
  Set m_Server = Nothing
End Sub
Private Sub m_Server_OnConnectionClosed(ByVal Socket As JBSOCKETSERVERLib.ISocket)
  Call CloseSocket(CLng(Socket.UserData))
End Sub
Private Sub m_Server_OnConnectionEstablished(ByVal Socket As JBSOCKETSERVERLib.ISocket)
  Call AcceptConnection(Socket)
End Sub
Private Sub m_Server_OnDataReceived(ByVal Socket As JBSOCKETSERVERLib.ISocket, ByVal Data As JBSOCKETSERVERLib.IData)
  Call IncomingData(Socket, Data)
End Sub
Public Sub StartListening()
  m_Server.StartListening
End Sub
Public Sub StopListening()
  m_Server.StopListening
End Sub
Public Property Get LocalAddress() As String
  LocalAddress = m_Server.LocalAddress.Address
End Property
Public Property Get LocalPort() As Long
  LocalPort = m_Server.LocalAddress.Port
End Property


[size=14pt]clsSocket.cls - Our custom Socket object to store connection information.
Option Explicit
'local variable(s) to hold property value(s)
Private mvarSocket As JBSOCKETSERVERLib.ISocket 'The Socket Object.
'Custom stuff for handling the socket.
Public Sub CloseSocket()
  mvarSocket.Close
  Set mvarSocket = Nothing
End Sub
Public Sub RequestRead()
  mvarSocket.RequestRead
End Sub
Public Sub Shutdown(how As ShutdownMethod)
  If mvarSocket Is Nothing Then Exit Sub
  Call mvarSocket.Shutdown(how)
End Sub
Public Sub WriteBytes(dbytes() As Byte, Optional thenShutdown As Boolean)
  Call mvarSocket.Write(dbytes, thenShutdown)
End Sub
Public Sub WriteString(Data As String, Optional sendAsUNICODE As Boolean, Optional thenShutdown As Boolean)
  Call mvarSocket.WriteString(Data, sendAsUNICODE, thenShutdown)
End Sub
Public Property Get RemoteAddress() As String
  RemoteAddress = mvarSocket.RemoteAddress.Address
End Property
Public Property Get RemotePort() As Long
  RemotePort = mvarSocket.RemoteAddress.Port
End Property
Public Property Let UserData(ByVal vData As Variant)
  mvarSocket.UserData = vData
End Property
Public Property Get UserData() As Variant
  UserData = mvarSocket.UserData
End Property
Private Sub Class_Terminate()
  Set mvarSocket = Nothing
End Sub
Public Property Set Socket(ByVal vData As JBSOCKETSERVERLib.ISocket)
  Set mvarSocket = vData
End Property
Public Property Get Socket() As JBSOCKETSERVERLib.ISocket
  Set Socket = mvarSocket
End Property


[size=14pt]colSockets.cls - Our custom collection of our clsSocket objects.

You will need to load the Class Builder Utility for this, so you can change this class module into a collection. You will also have to set the "Item" property as default. (Look at the class builder utility. You will see it.) (See Add-Ins/Add-In Manager to load the utility)
Option Explicit
'local variable to hold collection
Private mCol As Collection
Public Function Add(Optional sKey As String) As clsSocket
  'create a new object
  Dim objNewMember As clsSocket
  Set objNewMember = New clsSocket

  'set the properties passed into the method
  If Len(sKey) = 0 Then
    mCol.Add objNewMember
  Else
    mCol.Add objNewMember, sKey
  End If
 
  'return the object created
  Set Add = objNewMember
  Set objNewMember = Nothing
End Function
Public Property Get Item(vntIndexKey As Variant) As clsSocket
  Set Item = mCol(vntIndexKey)
End Property
Public Property Get Count() As Long
  Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Variant)
  Call mCol(vntIndexKey).Shutdown(ShutdownBoth)
  mCol.Remove vntIndexKey
End Sub
Public Property Get NewEnum() As IUnknown
  Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
  'creates the collection when this class is created
  Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
  'destroys collection when this class is terminated
  Set mCol = Nothing
End Sub


Easy Peasy, One Two Threesy.. Now we get to the "difficult" part. :) Making the necessary modifications to use those class objects, and get IOCP into our game.

The first thing you need to do is delete the Winsock control from frmServer. Then remove Microsoft Winsock Control from your components list. Then make the following modifications. :)

[size=14pt]frmServer.frm - Used to contain the winsock control, and handle all winsock requests. (amongst other things..)

Delete or comment the following code:

Private Sub Socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Call AcceptConnection(Index, requestID)
End Sub

Private Sub Socket_Accept(Index As Integer, SocketId As Integer)
    Call AcceptConnection(Index, SocketId)
End Sub

Private Sub Socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    If IsConnected(Index) Then
        Call IncomingData(Index, bytesTotal)
    End If
End Sub

Private Sub Socket_Close(Index As Integer)
    Call CloseSocket(Index)
End Sub


[size=14pt]modConstants.bas - Stores all the global constant variables for the server.

After the following:
' Winsock globals
Public Const GAME_PORT = 7000

Add the following:
Public Const GAME_IP = "0.0.0.0" 'You can leave this, or use your IP.


Change:
Public Const MAX_PLAYERS = 70

To:
Public Const MAX_PLAYERS = 500 'For starters... More optimization needed for a lot more.


[size=14pt]modGameLogic.bas - Handles all the logical stuff for the server.

In Function GetPlayerIP(), change:
    GetPlayerIP = frmServer.Socket(Index).RemoteHostIP

To:
    GetPlayerIP = GameServer.Sockets(Index).RemoteAddress


[size=14pt]modGeneral.bas - Handles general stuff, like server initialization, and termination. :)

In Sub InitServer():
Change:
    frmServer.Socket(0).RemoteHost = frmServer.Socket(0).LocalIP
    frmServer.Socket(0).LocalPort = GAME_PORT

To:
    Set GameServer = New clsServer


Change:
        Load frmServer.Socket(i)

To:
        Call GameServer.Sockets.Add(CStr(i))


Change:
    frmServer.Socket(0).Listen

To:
    GameServer.StartListening


Change:
    For i = 1 To MAX_PLAYERS
        Unload frmServer.Socket(i)
    Next i

To:
    For i = 1 To MAX_PLAYERS
        Call GameServer.Sockets.Remove(CStr(i))
    Next i
    Set GameServer = Nothing


In Sub ServerLogic(), delete the following:
Dim i As Long

    ' Check for disconnections
    For i = 1 To MAX_PLAYERS
        If frmServer.Socket(i).State > 7 Then
             Call CloseSocket(i)
        End If
    Next i


[size=14pt]modServerTCP.bas - Handles the TCP/IP stuff for the server.

At the top of the module, under any Option statements, add the following:
Public GameServer As clsServer


In Sub UpdateCaption(), change:
    frmServer.Caption = "Mirage Source Server <IP " & frmServer.Socket(0).LocalIP & " Port " & STR(frmServer.Socket(0).LocalPort) & "> (" & TotalOnlinePlayers & ")"

To:
    frmServer.Caption = "Mirage Source Server <IP " & GameServer.LocalAddress & " Port " & STR(GameServer.LocalPort) & "> (" & TotalOnlinePlayers & ")"


In Function IsConnected(), change:
Function IsConnected(ByVal Index As Long) As Boolean
    If frmServer.Socket(Index).State = sckConnected Then
        IsConnected = True
    Else
        IsConnected = False
    End If
End Function

To:
Function IsConnected(ByVal Index As Long) As Boolean
    IsConnected = False
    If Index = 0 Then Exit Function
    If GameServer Is Nothing Then Exit Function
    If Not GameServer.Sockets(Index).Socket Is Nothing Then
        IsConnected = True
    End If
End Function


In Function IsMultiIPOnline(), change:
        If IsConnected(i) And Trim(GetPlayerIP(i)) = Trim(IP) Then
             n = n + 1
            
             If (n > 1) Then
                 IsMultiIPOnline = True
                 Exit Function
             End If
        End If

To:
        If IsConnected(i) Then
             If Trim(GetPlayerIP(i)) = Trim(IP) Then
                 n = n + 1
            
                 If (n > 1) Then
                     IsMultiIPOnline = True
                     Exit Function
                 End If
             End If
        End If


Change Sub SendDataTo() to:
Sub SendDataTo(ByVal Index As Long, ByVal Data As String)
Dim i As Long, n As Long, startc As Long
Dim dBytes() As Byte
   
    dBytes = StrConv(Data, vbFromUnicode)
    If IsConnected(Index) Then
        GameServer.Sockets(Index).WriteBytes dBytes
        DoEvents
    End If
End Sub


Change Sub AcceptConnection() to:
Sub AcceptConnection(Socket As JBSOCKETSERVERLib.ISocket)
Dim i As Long

    i = FindOpenPlayerSlot
   
    If i <> 0 Then
        'Whoho, we can connect them
        Socket.UserData = i
        Set GameServer.Sockets(CStr(i)).Socket = Socket
        Call SocketConnected(i)
        Socket.RequestRead
    Else
        Socket.Close
    End If
End Sub


Change Sub IncomingData() to:
Sub IncomingData(Socket As JBSOCKETSERVERLib.ISocket, Data As JBSOCKETSERVERLib.IData)
On Error Resume Next

Dim Buffer As String
Dim dbytes() As Byte
Dim Packet As String
Dim top As String * 3
Dim Start As Integer
Dim Index As Long
Dim DataLength As Long

    dbytes = Data.Read
    Socket.RequestRead
    Buffer = StrConv(dbytes(), vbUnicode)
    DataLength = Len(Buffer)
    Index = CLng(Socket.UserData)
    If Buffer = "top" Then
        top = STR(TotalOnlinePlayers)
        Call SendDataTo(Index, top)
        Call CloseSocket(Index)
    End If
            
    Player(Index).Buffer = Player(Index).Buffer & Buffer
       
    Start = InStr(Player(Index).Buffer, END_CHAR)
    Do While Start > 0
        Packet = Mid(Player(Index).Buffer, 1, Start - 1)
        Player(Index).Buffer = Mid(Player(Index).Buffer, Start + 1, Len(Player(Index).Buffer))
        Player(Index).DataPackets = Player(Index).DataPackets + 1
        Start = InStr(Player(Index).Buffer, END_CHAR)
        If Len(Packet) > 0 Then
             Call HandleData(Index, Packet)
        End If
    Loop
                
    ' Check if elapsed time has passed
    Player(Index).DataBytes = Player(Index).DataBytes + DataLength
    If GetTickCount >= Player(Index).DataTimer + 1000 Then
        Player(Index).DataTimer = GetTickCount
        Player(Index).DataBytes = 0
        Player(Index).DataPackets = 0
        Exit Sub
    End If
       
    ' Check for data flooding
    If Player(Index).DataBytes > 1000 And GetPlayerAccess(Index) <= 0 Then
        Call HackingAttempt(Index, "Data Flooding")
        Exit Sub
    End If
       
    ' Check for packet flooding
    If Player(Index).DataPackets > 25 And GetPlayerAccess(Index) <= 0 Then
        Call HackingAttempt(Index, "Packet Flooding")
        Exit Sub
    End If
End Sub


Change Sub CloseSocket() to:
Sub CloseSocket(ByVal Index As Long)
    ' Make sure player was/is playing the game, and if so, save'm.
    If Index > 0 And IsConnected(Index) Then
        Call LeftGame(Index)
   
        Call TextAdd(frmServer.txtText, "Connection from " & GetPlayerIP(Index) & " has been terminated.", True)
       
        Call GameServer.Sockets(Index).Shutdown(ShutdownBoth)
        Call GameServer.Sockets(Index).CloseSocket

        Call UpdateCaption
        Call ClearPlayer(Index)
    End If
End Sub


Sheesh, that was long.. Please feel free to post any bugs, and discuss away. :) (You're welcome, Dave. :P)




Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info