Difference between revisions of "InSim examples"

From LFS Manual
Jump to navigationJump to search
(Improved example)
Line 5: Line 5:
 
How to connect, send a packet and receive the response.
 
How to connect, send a packet and receive the response.
  
<big><pre># Import Python's socket module.
+
<big><pre># Dependencies.
 
import socket
 
import socket
 +
import struct
  
# Import Python's struct module, which allows us to pack and unpack strings.
+
# Some constants.
import struct
+
INSIM_VERSION = 4
 +
BUFFER_SIZE = 2048
 +
 
 +
# Packet types.
 +
ISP_ISI = 1
 +
ISP_VER = 2
 +
ISP_TINY = 3
 +
TINY_NONE = 0
  
# Initialise the socket in TCP mode.  
+
# Initailise the socket in TCP mode.
 
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
Line 17: Line 25:
 
sock.connect(('localhost', 29999))
 
sock.connect(('localhost', 29999))
  
# Pack the IS_ISI data into a string.
+
# Pack the ISI packet into a string.
isi = struct.pack('BBBBHHBcH16s16s',  
+
isi = struct.pack('BBBBHHBBH16s16s',  
                   44,           # Size
+
                   44,         # Size
                   1,           # Type
+
                   ISP_ISI,     # Type
                   0,           # ReqI
+
                   1,           # ReqI - causes LFS to send an IS_VER.
                   0,           # Zero
+
                   0,           # Zero
                   0,           # UDPPort
+
                   0,           # UDPPort
                   0,           # Flags
+
                   0,           # Flags
                   0,           # Sp0
+
                   0,           # Sp0
                   ' ',         # Prefix
+
                   0,           # Prefix
                   0,           # Interval
+
                   0,           # Interval
                   'password',   # Admin
+
                   'password', # Admin  
                   'MyProgram',) # IName
+
                   '^3example') # IName
 
+
               
# Send the string to InSim
+
# Send the ISI packet to InSim.
 
sock.send(isi)
 
sock.send(isi)
  
 
# Some constants.
 
ISP_TINY = 3
 
TINY_NONE = 0
 
 
# keep alive the connection (see below)
 
def keepAlive(packet):
 
    # Check the packet type.
 
    if ord(packet[1]) == ISP_TINY:
 
        # Unpack the packet data.
 
        tiny = struct.unpack('BBBB', packet)
 
        # Check the SubT.
 
        if tiny[3] == TINY_NONE:
 
            # Send the keep alive packet back to LFS.
 
            sock.send(packet)
 
 
 
# We use a string as the buffer.
 
 
buffer = ''
 
buffer = ''
  
 
while True:
 
while True:
    # Receive up to 1024 bytes of data.
+
     data = sock.recv(BUFFER_SIZE)
     data = sock.recv(1024)
 
 
 
    # If no data is received the connection has closed.
 
 
     if data:
 
     if data:
        # Append received data onto the buffer.
 
 
         buffer += data
 
         buffer += data
 
+
       
         # Loop through each completed packet in the buffer. The first byte of
+
         # Loop through completed packets.
        # each packet is the packet size, so check that the length of the
 
        # buffer is at least the size of the first packet.  
 
 
         while len(buffer) > 0 and len(buffer) >= ord(buffer[0]):
 
         while len(buffer) > 0 and len(buffer) >= ord(buffer[0]):
             # Copy the packet from the buffer.
+
           
 +
             # Copy the packet from the buffer and delete it.
 
             packet = buffer[:ord(buffer[0])]
 
             packet = buffer[:ord(buffer[0])]
 
            # Remove the packet from the buffer.
 
 
             buffer = buffer[ord(buffer[0]):]
 
             buffer = buffer[ord(buffer[0]):]
 
+
           
             # check keep alive!
+
            # Check packet type.
            keepAlive(packet)
+
             if ord(buffer[1]) == ISP_TINY:
 
+
                # Unpack the TINY packet and check it for keep-alive signal.
            # The packet is now complete! :)
+
                size, type, reqi, subt = struct.unpack('BBBB', packet)
             # doSomethingWithPacket(packet)
+
                if subt == TINY_NONE:
 +
                    sock.send(packet)
 +
             elif ord(buffer[1]) == ISP_VER:
 +
                # Unpack the VER packet and check the InSim version.
 +
                size, type, reqi, _, version, product, insimver = struct.unpack('BBBB8s6sH')
 +
                if insimver != INSIM_VERSION:
 +
                    print 'Invalid InSim version'
 +
                    break
 
     else:  
 
     else:  
 +
        # InSim connection has been lost.
 
         break
 
         break
 
+
       
 
# Release the socket.
 
# Release the socket.
 
sock.close()</pre></big>
 
sock.close()</pre></big>

Revision as of 16:57, 27 June 2009

Python

Example #1

How to connect, send a packet and receive the response.

# Dependencies.
import socket
import struct

# Some constants.
INSIM_VERSION = 4
BUFFER_SIZE = 2048

# Packet types.
ISP_ISI = 1
ISP_VER = 2
ISP_TINY = 3
TINY_NONE = 0

# Initailise the socket in TCP mode.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to LFS.
sock.connect(('localhost', 29999))

# Pack the ISI packet into a string.
isi = struct.pack('BBBBHHBBH16s16s', 
                  44,          # Size
                  ISP_ISI,     # Type
                  1,           # ReqI - causes LFS to send an IS_VER. 
                  0,           # Zero
                  0,           # UDPPort
                  0,           # Flags
                  0,           # Sp0
                  0,           # Prefix
                  0,           # Interval
                  'password',  # Admin 
                  '^3example') # IName
                 
# Send the ISI packet to InSim. 
sock.send(isi)

buffer = ''

while True:
    data = sock.recv(BUFFER_SIZE)
    if data:
        buffer += data
        
        # Loop through completed packets.
        while len(buffer) > 0 and len(buffer) >= ord(buffer[0]):
            
            # Copy the packet from the buffer and delete it.
            packet = buffer[:ord(buffer[0])]
            buffer = buffer[ord(buffer[0]):]
            
            # Check packet type.
            if ord(buffer[1]) == ISP_TINY:
                # Unpack the TINY packet and check it for keep-alive signal.
                size, type, reqi, subt = struct.unpack('BBBB', packet)
                if subt == TINY_NONE:
                    sock.send(packet)
            elif ord(buffer[1]) == ISP_VER:
                # Unpack the VER packet and check the InSim version.
                size, type, reqi, _, version, product, insimver = struct.unpack('BBBB8s6sH')
                if insimver != INSIM_VERSION:
                    print 'Invalid InSim version'
                    break
    else: 
        # InSim connection has been lost.
        break
        
# Release the socket.
sock.close()