Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IOCP (Not for Win 9x/ME) (Bugs Fixed)
#1
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. Tongue (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. Smile

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. Smile (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. Smile

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-showar ... rticleId=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 Smile) 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. Smile You can download my modified .DLL from 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. Smile 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 . (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. Smile

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. Tongue

[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. Smile
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. Smile 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. Smile

[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. Smile

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  (" & TotalOnlinePlayers & ")"
To:
Code:
frmServer.Caption = "Mirage Source Server  (" & 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)
Reply
#2
tried it and its coming up with an error in an area when...
Quote: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...
Reply
#3
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
Reply
#4
you installed the support files on your server computer?
Reply
#5
Deleted post

This tutorial is bugged. I read and did everything it said. Some parts are written incorrectly but it's just 1 bug after another.
Reply
#6
lol, Your totally wrong. This is easy as 123 abc. Its not bugged at all.
Reply
#7
GameBoy, go try on a Vanilla copy.
Reply
#8
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.
Reply
#9
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 Wink
Reply
#10
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:
http://www.splamm.com/elysium/forums/vi ... php?t=1658
Reply
#11
Advocate Wrote: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.
Reply
#12
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. ^_^
Reply
#13
Has anybody added this to a fresh copy of MSE 1 without any modifications to the source or the tutorial, and had it working correctly?
Reply
#14
I had some errors with it too, but my MS is quite modified.
Reply
#15
Depends, are you using my poke source? It already has IOCP within it.
Reply
#16
Hey, I installed this and I get RTE438, something doesn't exist and I replaced (index) to .item(index) in the yellow marked text and it worked the only problem is that I can't login on the client and that I stay on the text Connected. Does somebody know how to fix this?
Reply
#17
for 1, use Sonire's classes, that'll fix most of the problems. Secondly, I used the tutorial on Elysium Diamond, and it works in Mirage Source too.
Reply
#18
It fixed the problem Big Grin. Thank you and also thanks to Sonire.
Reply
#19
Well, anyone want to post a dl for the .dll? It's not hosted by Verrigan anymore, it seems.
Reply
#20
Try Verrigans again, I'm under the impression that there was a period or something in the url that made it bad.

If not, I'll upload it when I get off work.
Reply
#21
His isn't working, I already tried to remove the period.

Anyone wanna spare me this file?
Reply
#22
http://www.codeproject.com/internet/com ... ct=1639255

That site has both the dll and the source code for the dll.

You have to sign up to download it though.
Reply
#23
I actually found the DLL on another thread on these forums.

The Search Button works wonders. Thank you all for your help, though.
Reply
#24
can anyone reupload files?
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)