Networking Routines

by Brandon Miller

What's in this document

Short Description
The network routines for Contra Wars will be inplemented with NetBIOS using general broadcast datagrams.  There will be four main procedures: 
 
 
NetINIT -  This procedure initializes NetBIOS on each client. 
NetPost- This is the main procedure used by the game.  It processes all incoming datagrams and updates the necessary variable data on the client.
Post - This is a far proc to handle the NetBIOS INT5C.  It calls NetPost to handle incoming datagrams when this interrupt is called. 
NetPost - This is the main procedure used by the game.  It processes all incoming datagrams and updates the necessary variable data on the client accordingly.  Only once the data has been correctly recieved can the graphics, sound, and input routines work correctly.  NetPost will be designed to recieved various types of messages: HELLO for initialization, UPDATEPOS to update player position, UPDATESHOT to update gunfire, and GOODBYE for exiting. 
NetRelease - This procedure is called when a player wishes to exit.  It will notify the rest of the clients that a player is leaving so they can update accordingly. 

The NetInit, SendPacket, and NetRelease code will be based on the NetLIB asm code samples found at 
http://www.ece.uiuc.edu/~ece291/class-resources/netlib.zip.  These routines will also require the use of some of the ECE291 library functions (Lib291.lib).  Note
that the macros have been taken from the Macros.inc file found in netlib.zip. 
 


 
 
 
 
Dependant Constants From Other Source Files
Listed below are constants used from other source files.  The procedures in Finet.asm require that these constants exist and are set properly.
 
 
NUM_PLAYERS 
  • the max number of players in the game
MAX_BULLETS 
  • the max number of bullets a player can fire
TRUE  = 1 
FALSE = 0 
  • for boolean logic (make code readable)
SIZEPLAYERSTRUCT
  • the size in bytes of the player structure
SIZEBULLETSTRUCT
  • the size in bytes of the bullet structure

 
 
Dependant Variables From Other Source Files
Listed below are variables used from other source files.  The procedures in Finet.asm and macros in Sndnet.inc require that these variables exist.
 
 
Players PlayerType NUM_PLAYERS dup (<>) 
  • holds the list of players in the game with their states
  • used when drawing players and bullets to the double buffer
  • MyOffset dw ?
    • holds offset of your player in the Players array
    MyNum db
    • client player number in the game
     
    BULLET_ACTIVE db ?
    • tells if the current bullet in the bullet array is active

     
     
    Defined Structures
    This is a Network Control Block Structure for use by NetBIOS.
    NCB struc
            ncb_command     db ?    ; command code
            ncb_retcode     db ?    ; error return code
            ncb_lsn         db ?    ; session number
            ncb_num         db ?    ; name number
            ncb_buf_off     dw ?    ; ptr to send/receive data offset
            ncb_buf_seg     dw ?    ; ptr to send/receive data segment
            ncb_buflen      dw ?    ; length of data
            ncb_callname    db 16 dup (?) ; remote name
            ncb_name        db 16 dup (?) ; local name
            ncb_rto         db 0    ; receive timeout
            ncb_sto         db 0    ; send timeout
            ncb_post_off    dw ?    ; async command complete post offset
            ncb_post_seg    dw ?    ; async command complete post segment
            ncb_lana_num    db ?    ; adapter number
            ncb_cmd_done    db ?    ; 0FFh until command completed
            ncb_res         db 14 dup (?) ; reserved
    NCB ENDS
    GenericMsg STRUC
            MsgType         db ?    ; Generic Message Structure
            PlayerNum       db 19
    GenericMsg ENDS
    HelloMsg STRUC
            MsgType         db HELLO
            PlayerNum       db 19
            TeamNum         db ?
            StartPos        dw 2 dup(?)
    HelloMsg ENDS
    UpdatePos STRUC
            MsgType         db UPDATEP
            PlayerNum       db 19
            UPosition       dw 2 dup(?); The current position
            Direction       db ?    ; Direction the player is facing
    UpdatePos ENDS
    UpdateShot STRUC
            MsgType         db UPDATESH
            PlayerNum       db 19
            ShotPos         dw 2 dup(?)
            ShotDir         db ?
    UpdateShot ENDS
    GoodByeMsg STRUC
            MsgType         db GOODBYE
            PlayerNum       db 19
    GoodByeMsg ENDS

     
     
    Declared Varaiables
    Variables declared by the networking rouintes.
     
    grpsnd  NCB     <>
    • Send Network Control Block
    grprcv  NCB     <>
    • Receive Network Control Block
    RXBuffer db 1024 dup('?') 
    • Receive Data Buffer
    TXBuffer db 1024 dup('?')
    • Transmit Data Buffer
    grp_num  db ?
    • Determined by NetBIOS at runtime
    my_num   db     ?
    • Determined by NetBIOS at runtime
    rcvcnt     dw   0
    • The number of packets received
    sndcnt     dw   0
    • The number of packets sent
    rcvbadpost dw   0
    • The number of bad packets received
    grp_name db 'ECEContraWar$$$$'
    • This is the group name for networking
    my_name db 'ECEWarPlayer00'
    • This is the player name for networking

     
     
    Declared Constants
    The following are all constants used by the networking routines.
    The following constants are used by NetPost, and some networking macros:
            HELLO           equ 1
            UPDATEP         equ 2
            UPDATESH        equ 3
            GOODBYE         equ 4
    The following constants are used by NetBIOS:
            RESET                 equ 032h
            CANCEL                equ 035h
            STATUS1               equ 0B3h
            STATUS_WAIT           equ 034h
            TRACE                 equ 0F9h
            UNLINK                equ 070h
            ADD_NAME              equ 0B0h
            ADD_NAME_WAIT         equ 030h
            ADD_GROUP_NAME        equ 0B6h
            ADD_GROUP_NAME_WAIT   equ 036h
            DELETE_NAME           equ 0B1h
            DELETE_NAME_WAIT      equ 031h
            CALL_                 equ 090h
            LISTEN                equ 091h
            HANG_UP               equ 092h
            SEND                  equ 094h
            CHAIN_SEND            equ 097h
            RECEIVE               equ 095h
            RECEIVE_ANY           equ 096h
            SESSION_STATUS        equ 0B4h
            SEND_DATAGRAM         equ 0A0h
            SEND_BCST             equ 0A2h
            RECEIVE_DATA          equ 0A1h
            RECEIVE_BCST_DATA     equ 0A3h
     

     
     
    Macros
    Macros defined and used by networking routines.

     
    INT5C MACRO buffer ; Call NetBIOS Interrupt 
            push    es                 ; save registers
            push    bx
            push    ax
    
            mov     ax, seg buffer
            mov     es, ax
            mov     bx, offset buffer
            int     05Ch               ; NetBIOS interrupt
    
            pop     ax
            pop     bx                 ; restore registers
            pop     es
    ENDM
    HELLOSEND MACRO
    ; Input: None
    ; Output: None
    ; Calls: SendPacket - This sends out the HelloMsg that has been set up
    ;        MOVMW - Macro to move word from memory to memory.
    ;        MOVMB - Macro to move byte from memory to memory.
    ; Purpose: This macro sends sets up the HelloMsg with the appropriate values
    ;       and then calls SendPacket to transmit the packet to everyone else.
    ;       A HelloMsg tells what playernumber the message is from, their team
    ;       number, their direction, and their position.
    
            push    ax
            push    bx
            mov     (HelloMsg PTR TXBuffer).MsgType ,   HELLO
            MOVMB   (HelloMsg PTR TXBuffer).PlayerNum , MyNum
            MOVMB   (HelloMsg PTR TXBuffer).TeamNum, (PlayerType PTR players[bx]).Team
            mov     bx, MyOffset
            MOVMW   (PointType PTR (HelloMsg PTR TXBuffer).StartPos).x, (PointType PTR (PlayerType PTR players[bx]).GamePos).x
            MOVMW   (PointType PTR (HelloMsg PTR TXBuffer).StartPos).y, (PointType PTR (PlayerType PTR players[bx]).GamePos).y
            MOVMB   (UpdatePos PTR TXBuffer).Direction, players[bx].Dir
            mov     ax, Sizeof HelloMsg
            call    SendPacket
            pop     bx
            pop     ax
    ENDM
    UPDATEPOSEND MACRO
    ; Input: None
    ; Output: None
    ; Calls: SendPacket - This sends out the HelloMsg that has been set up
    ;        MOVMW - Macro to move word from memory to memory.
    ;        MOVMB - Macro to move byte from memory to memory.
    ; Purpose: This macro sends sets up the UpdatePos message struct with the 
    ;       appropriate values and then calls SendPacket to transmit the packet
    ;       everyone else.  An UpdatePos message tells what playernumber the
    ;       message is from, their direction, and their position.
    
            push    ax
            push    bx
            mov     (UpdatePos PTR TXBuffer).MsgType, UPDATEP
            MOVMB   (UpdatePos PTR TXBuffer).PlayerNum, MyNum
            mov     bx, MyOffset
            MOVMW   (PointType PTR (UpdatePos PTR TXBuffer).UPosition).x, (PointType PTR (PlayerType PTR players[bx]).GamePos).x
            MOVMW   (PointType PTR (UpdatePos PTR TXBuffer).UPosition).y, (PointType PTR (PlayerType PTR players[bx]).GamePos).y
            MOVMB   (UpdatePos PTR TXBuffer).Direction, players[bx].Dir
            mov     ax, Sizeof UpdatePos
            call    SendPacket
            pop     bx
            pop     ax      
    ENDM
    UPDATESHOTSEND MACRO    BulDir, BulPosX, BulPosY
    ; Input: BulDir - The direction of the bullet
    ;        BulPosX - The X position the bullet is being fired from
    ;        BulPosY - The Y position the bullet is being fired from
    ; Output: None
    ; Calls: SendPacket - This sends out the HelloMsg that has been set up
    ;        MOVMW - Macro to move word from memory to memory.
    ;        MOVMB - Macro to move byte from memory to memory.
    ; Purpose: This macro sends sets up the UpdateShot message struct with the 
    ;       appropriate values and then calls SendPacket to transmit the packet
    ;       everyone else.  An UpdateShot message tells what playernumber the
    ;       message is from, the bullet's direction, and the bullet's start position
    
            push    ax
            mov     (UpdateShot PTR TXBuffer).MsgType, UPDATESH
            MOVMB   (UpdateShot PTR TXBuffer).PlayerNum, MyNum
            MOVMW   (PointType PTR (UpdateShot PTR TXBuffer).ShotPos).x, BulPosX
            MOVMW   (PointType PTR (UpdateShot PTR TXBuffer).ShotPos).y, BulPosY
            MOVMB   (UpdateShot PTR TXBuffer).ShotDir, BulDir
            mov     ax, Sizeof UpdateShot
            call    SendPacket
            pop     ax
    ENDM
    GOODBYESEND MACRO
    ; Input: None
    ; Output: None
    ; Calls: SendPacket - This sends out the HelloMsg that has been set up
    ;        MOVMB - Macro to move byte from memory to memory.
    ; Purpose: This macro sends sets up the GoodByeMsg message struct with the 
    ;       appropriate values and then calls SendPacket to transmit the packet
    ;       everyone else.  An GoodByeMsg message tells what playernumber the
    ;       message is from. 
    
            push    ax
            mov     (GoodByeMsg PTR TXBuffer).MsgType, GOODBYE
            MOVMB   (GoodByeMsg PTR TXBuffer).PlayerNum, MyNum
            mov     ax, Sizeof GoodByeMsg
            call    SendPacket
            pop     ax
    ENDM
    MOVMB MACRO dst, src ; MOV Byte from Memory to Memory
            PUSH    AX
            mov     al,src
            mov     dst,al
            POP     AX
    ENDM
    MOVMW MACRO dst, src ; MOV Word from Memory to Memory
            PUSH    AX
            mov     ax,src
            mov     dst,ax
            POP     AX
    ENDM
    STRCPY MACRO dst, src, NumBytes
            push    ds
            push    es
            push    ax              
            push    cx
            push    di
            push    si
    
            cld
    
            MOV CX,NumBytes
    
            LEA DI, dst
            MOV AX, seg dst
            MOV ES,AX
    
            LEA SI, src
            MOV AX, seg src
            MOV DS,AX
    
            REP MOVSB
    
            pop     si
            pop     di
            pop     cx
            pop     ax
            pop     es
            pop     ds
    ENDM
    STRCPY16 MACRO dst, src ; Optimized Copies 16-byte String 
            push    es
            push    ds
            push    eax              
            push    cx
            push    di
            push    si
    
            cld                     
            MOV AX,seg dst
            MOV ES,AX
            MOV DI,offset dst
    
            MOV AX,seg src
            MOV DS,AX
            MOV SI,offset src
    
            mov     cx, 4          
            rep     movsd           
    
            pop     si
            pop     di
            pop     cx
            pop     eax
            pop     ds
            pop     es
    ENDM
    PRINTMSG MACRO message ; Print a text message
            LOCAL msgdta
            LOCAL msgjmp
            JMP     msgjmp
    msgdta  db message , CR , LF , '$'
    msgjmp: PUSH    DX
            MOV     DX, offset msgdta
            CALL    DSPMSG
            POP     DX
    ENDM
    PRINTSTR MACRO message, varstr ; Print message and a string
            LOCAL msgdta
            LOCAL msgjmp
            JMP   msgjmp
    msgdta  db message , '$'
    msgjmp: PUSH DX
    
            MOV DX,offset msgdta
            call DSPMSG
            MOV DX,offset varstr
            Call DSPMSG
            MOV DX,offset CRLF
            Call DSPMSG
    
            POP DX
    ENDM
    DOSEXIT MACRO RetCode ; exit to dos (like DOSXIT, but w/o message)
            MOV AH,4ch
            MOV AL,RetCode
            INT 21h
    ENDM
    

     
    Procedures
      Procedures for the networking routines.
    NetINIT PROC NEAR
    • Reg In: 
      • None.
    • Reg Out: 
      • AL - contains the player number
    • Variables read: 
      • grp_name - sets up the client to be a member of this group
      • my_name - adds your name to the group
    • Variables updated: 
      • grpsnd - this Network Control Block structure is updated
    • calls: 
      • None.
    • description: 
      • Initializes NetBIOS on the client by adding the client to the network group and updating the name
      • The assigned player number is returned in AL
      • This code is taken directly from the ECE291 netlib.zip.
    NetRelease PROC NEAR
    • Reg In: 
      • None.
    • Reg Out: 
      • None.
    • Variables read: 
      • my_grp - the name of the group that this player is exiting
      • my_name - the name of the player leaving the group
      • sndcnt - the number of packets sent
      • rcvcnt - the number of packets received
      • rcvbadpost - the number of bad packets received
    • Variables updated: 
      • grpsnd - variables in this Network Control Block strucure are updated
    • calls: 
      • None.
    • description: 
      • This procedure calls a NetBIOS interrupt that gracefully allllows a player to exit from a NetBIOS group.
    SendPacket PROC NEAR
    • Reg In: 
      • AX - holds the length of the data
      • CX - holds the current code segment
    • Reg Out: 
      • None.
    • Variables read: 
      • TXBuffer - holds the data to transmit
      • group_name - the group name the packet will be sent to
      • grp_num - the group number the packet is being sent to
    • Variables updated: 
      • grpsnd - variables in this Network Control Block strucure are updated
    • calls: 
      • None.
    • description: 
      • This procedure updates the grpsnd Network Control Block structure and calls the NetBIOS interrupt to send out a datagram with a message with the length found in AX onto the network. 
      • This code is taken directly from the ECE291 netlib.zip.
    Post PROC FAR
    • Reg In: 
      • None (this is an interrupt handler).
    • Reg Out: 
      • None (this is an interrupt handler).
    • Variables read: 
      • grprcv - the Network Control Block structure that recieves packets
    • Variables updated: 
      • rcvcnt - the count of the number of packets received is incremented
      • rcvbadpost - the count of the number of bad packets received is updated if there is an error
      • grpcrv - the Network Control Block structure that receives packets is updated
    • calls: 
      • NetPost - the procedure that handles all recieved message types
    • description: 
      • This procedure is the interrupt handler for the NetBIOS INT5C.  If a good packet is received, NetPost is called to process the datagram.  If the packet is bad, NetPost is not called.  Either way, the NetBIOS INT5C is then called.
      • This code is taken direrctly from the ECE291 netlib.zip.
    NetPost PROC NEAR
    • Reg In: 
      • None.
    • Reg Out: 
      • None.
    • Variables read: 
      • HelloMsg - this message structure is used if the message being read is a message to initialize a player
      • UpdatePos - this message structure is used if the message being read is a message to update the player position
      • UpdateShot - this message structure is used if the message being read is a message to indicate that a shot has been fired
      • GoodByeMsg - this message structure is used if the message being read is a message to update the player is leaving or has been shot
    • Variables updated: 
      • players - the player array of player structs
    • calls: 
      • None.
    • description: 
      • This procedure processes incoming datagrams and updates data in the player array accordingly.  The incoming datagrams have been divided up into four types of messages: HelloMsg, UpdatePos, UpdateShot, and GoodByeMsg.  The GoodByeMsg will set the death flag in the player array to 1 for that player.