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

  • Lunar Engine v0.2.6 is out now! Download and check it out here. The map editor is also now fixed!

William

Veteran
Veteran
Member
May 29, 2006
2,228
0
0
www.key2heaven.net
Gold
0
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 <!-- m --><a class="postlink" href="http://www.jetbyte.com/portfolio-showarticle.asp?articleId=4">http://www.jetbyte.com/portfolio-showar ... rticleId=4</a><!-- m --> 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 <!-- m --><a class="postlink" href="http://www.verrigan.net/iocp/COMSocketServer.dll">http://www.verrigan.net/iocp/COMSocketServer.dll</a><!-- m -->.

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. :)
Code:
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.
Code:
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)
Code:
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:
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:
Code:
' Winsock globals
Public Const GAME_PORT = 7000
Add the following:
Code:
Public Const GAME_IP = "0.0.0.0" 'You can leave this, or use your IP.
Change:
Code:
Public Const MAX_PLAYERS = 70
To:
Code:
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:
Code:
    GetPlayerIP = frmServer.Socket(Index).RemoteHostIP
To:
Code:
    GetPlayerIP = GameServer.Sockets(Index).RemoteAddress
[size=14pt]modGeneral.bas - Handles general stuff, like server initialization, and termination. :)

In Sub InitServer():
Change:
Code:
    frmServer.Socket(0).RemoteHost = frmServer.Socket(0).LocalIP
    frmServer.Socket(0).LocalPort = GAME_PORT
To:
Code:
    Set GameServer = New clsServer
Change:
Code:
        Load frmServer.Socket(i)
To:
Code:
        Call GameServer.Sockets.Add(CStr(i))
Change:
Code:
    frmServer.Socket(0).Listen
To:
Code:
    GameServer.StartListening
Change:
Code:
    For i = 1 To MAX_PLAYERS
        Unload frmServer.Socket(i)
    Next i
To:
Code:
    For i = 1 To MAX_PLAYERS
        Call GameServer.Sockets.Remove(CStr(i))
    Next i
    Set GameServer = Nothing
In Sub ServerLogic(), delete the following:
Code:
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:
Code:
Public GameServer As clsServer
In Sub UpdateCaption(), change:
Code:
    frmServer.Caption = "Mirage Source Server <IP " & frmServer.Socket(0).LocalIP & " Port " & STR(frmServer.Socket(0).LocalPort) & "> (" & TotalOnlinePlayers & ")"
To:
Code:
    frmServer.Caption = "Mirage Source Server <IP " & GameServer.LocalAddress & " Port " & STR(GameServer.LocalPort) & "> (" & TotalOnlinePlayers & ")"
In Function IsConnected(), change:
Code:
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:
Code:
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:
Code:
        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:
Code:
        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:
Code:
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:
Code:
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:
Code:
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:
Code:
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)
 

Aaron

Member
Member
Dec 20, 2006
45
0
0
aaronbartlett.webs.com
Gold
0
tried it and its coming up with an error in an area when...
In Function IsConnected(), change:Code:
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:Code:
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
Did everything as said and checked it over 3 times to make sure i made no mistakes in it and i entered it into a fresh MSE so im not sure...
 

Tony

Member
Member
Nov 29, 2006
780
0
0
Gold
0
Download the COMSocketServer.dll . Put it into your server folder and go to references and browse into your server folder for it. Do this twice as it won't work the first time. If you did this then just tell me.

If you did this step then did you add in class builder and make the class module into a col.?

:: Pando
 

one

Member
Member
Aug 27, 2006
49
0
0
Gold
0
okay, ive to say ive modified my server and client heavily.
i added iocp and it worked fine on my local computer
now i uploaded it to my server and it gives me a out of memory error at
Public Sub WriteBytes(dbytes() As Byte, Optional thenShutdown As Boolean)
Call mvarSocket.Write(dbytes, thenShutdown)
End Sub
when i use the original comsocketserver.dll, not verrigans modified one, it wont give me the error, but i cant attack or spell magic (maybe packets too big?)
 

Tony

Member
Member
Nov 29, 2006
780
0
0
Gold
0
lol, Your totally wrong. This is easy as 123 abc. Its not bugged at all.
 
M

Matt

Guest
Guest
<div class="bbWrapper">Zel isn't a noob programmer, just so you guys know. So talking to him in that way, doesn't really help.<br /> <br /> Not directed at you Kite. Since what you said was actually constructive.</div>
 

Rian

Legend
Legend
May 29, 2006
822
9
18
Gold
0
My Classes

Try using my classes. Not sure if that's your problem or not, but it's worth a shot. If it still doesn't work, at least you'll know that the classes aren't the problem ;)
 

Xlithan

Member
Member
Sep 20, 2006
319
0
0
www.player-realms.co.nr
Gold
0
Thank you Advocate. I made 1 or 2 corrections first, but I still kept getting errors.

I've added this before, but with Elysium Diamond. My copy of Mirage Source is pretty much clean, I've only updated parts of the code which have nothing to do with the connections bits.

I'll try again though later and see if it was just something I'd missed. I know there was a lot of changes I had to make to the tutorial.

I found that this version seems to work better though:
<!-- m --><a class="postlink" href="http://www.splamm.com/elysium/forums/viewtopic.php?t=1658">http://www.splamm.com/elysium/forums/vi ... php?t=1658</a><!-- m -->
 

Tony

Member
Member
Nov 29, 2006
780
0
0
Gold
0
Advocate said:
Zel isn't a noob programmer, just so you guys know. So talking to him in that way, doesn't really help.

Not directed at you Kite. Since what you said was actually constructive.
I never said that he was a noob prorammer, just correcting him that this tutorial is NOT bugged. kthxbye.
 
M

Matt

Guest
Guest
<div class="bbWrapper">I understand that, but the method you went about, made him sound like a noob. That's all. Try to speak more mature next time. ^_^</div>
 

Tony

Member
Member
Nov 29, 2006
780
0
0
Gold
0
Depends, are you using my poke source? It already has IOCP within it.
 

Asrrin29

Member
Member
Jun 7, 2006
464
0
0
www.athrandironline.com
Gold
0
Verrigan's download of COMSocketServer.dll seems to be down, does anyone have a copy I can download? I had it, but it seems the copy I downloaded is corrupted. :(

EDIT: Nvmd, after some help from Grim and Presise, I figured it all out. need to make sure to use the dll as a refernece and not a component, and also need to use Sonire's .cls files to make it work.