The online racing simulator
InSim in Flash
(20 posts, started )
InSim in Flash
It is possible to connect to InSim in Flash? Yes it is, but there is no library for Flash. Maybe someone have it? Can you make it/public it?
Yes, but there isn't any library/example published. I started fun with Flash yesterday and I did an Flash-version of Live Tracker for long races (reading data from PHP). Now I wonna make it flash-only to provide data faster.

EDIT:
Maybe I'll do it with PHP Client/Server (PHP connected to InSim, converts data to XML and then I'll request this XML by Flash but it's a little bit slower than directly Flash-InSim connection and needs more work).
Just create a socket and receive packets normally. A two second search of Google revealed a bunch of info about networking in Flash, including tutorials and official documentation.

You only need to know two things to write InSim code:
  • How to use a TCP socket (there is insane amounts of info about this on Google)
  • How to decode and encode packets of data (again you are not the first person ever to try and do this)
Just do a few searches and figure it out. Writing networking code isn't difficult.
I know. I did PHP system like PHPInSimMod alone and it was working. Now Dygear made PRISM from PHPInSimMod so I didn't public my own php library/framework for InSim.

Now I started to learn Flash, I did half-live tracker for long races in flash using bothy tracker code written in PHP. Like I said I wonna make studio in Flash for team to manage any data in 16h/24h races. I wonna release first version at 1.08.2011r. but I don't have so much time to code every packet in Flash now so I'm asking people like Victor to release InSim flash library.
I don't have an insim library as such. In the Remote I just have one big object called InSimSocket, which does all networking and packet handling. I didn't really have the need for a full blown lib like InSim.NET.

If you're really stuck on how to use sockets in flash I don't mind pasting that part of the class though. But like DT said, there are plenty of tutorials and examples available on how to use sockets in flash.
The InSim handling part is just like in every other language, so you should be able to get that up and running pretty quickly.
Quote from misiek08 :I don't have so much time to code every packet in Flash now so I'm asking people like Victor to release InSim flash library.

A good tip that I used when creating my InSim stuff was to write a script that parses InSim.txt and converts all the C structs into whatever language you are using, such as Python and C# in my case (although I wrote the script in Python). The script can be a bit fiddly to write, but as the InSim text file is already syntactically correct C code, it's not that difficult to parse. Of course the output may not be completely correct, but it should do 99% of the job for you, then you can add the rest manually. Plus of course once you have written such a script you can change it to produce output for other languages too. I agree that manually coding every packet is not a good idea, as not only is it the most boring code to write, you will also introduce lots of mistakes. A simple script however can do 99% of the job for you.
I did my own system PHP<->Flash, where PHP is server (I know PHP so it isn't problem for me). I can now send few packets I need. So I see, I have to parse all packets again in Flash....
Seriously, everyone's giving advice and you just seem to blow it in the wind.
I'm out of this thread. Good luck.
I see tips from another ppl but if someone have done something, we shouldn't have to make it again.
yes, because if we don't share anything, then you don't release anything, right? (to others in this thread : he threatened me with that)

Good attitude.
@Victor, Yeah, he does that ...

@misiek08, Strong arming someone who has what you need is never a good idea. Get a better attitude and try again, I can't see anyone helping you on any subject anymore if you keep acting like this.

Rule #1 of getting help online: Don't be a dick.
So if I need help I have to public many things to others? Okay.
Summer time started and I have time to work on my PRISM plugins that are started or drawed only on paper.

Now I need example in AS3 how to send msg's and reo's to server. In a 1-2 weeks I'll release an relay plugin for PRISM for ppl that need more than 8 apps.

I have problems only with fields like char[]. I can send bytes, words.

Here's how I send ISI (it don't work, but I have an relay plugin that register self with ISI and acts as proxy between flash and LFS):
socket.writeByte(44); // Size
socket.writeByte(1); // Type
socket.writeByte(0); // ReqI
socket.writeByte(0); // Zero
socket.writeUnsignedInt(0); // UDPPort
socket.writeUnsignedInt(8); // Flags
socket.writeByte(0); // Sp0
socket.writeByte('!'.charCodeAt(0)); // Prefix
socket.writeUnsignedInt(0); // Interval
socket.writeUTFBytes('asd'); // Admin
socket.writeUTFBytes('Test Flash App'); // IName

I'm very sorry @Victor. I sent you PM.

@all: I understood Victor's " I don't mind " like "I'm not going to" and it looked like Victor isn't going to help me. I can speak English and I understand it but sometimes I translate something wrong.
buy a gun, aim it at your feet, and FIRE! :P

/rinse and repeat
I'm going to do it I don't know why I understood it worng. Maybe "don't" was my precursor...
Thanks for the pm's.

Firstly about your bit of ISI packet sending code - you must do a (socket.)flush() after writing the bytes to it. Only then the bytes will actually be transferred. But maybe you just left that part out.

Anyway, here comes a downsized InSimSocket.as. I have cut many packet handling methods because they apply to the remote alone, so they're not useful for you. But the core is still there.
As you can see the class extends onto the Socket class, so this class is a Socket in itself, but with all these extra network maintenance and insim related methods.

package {
import flash.events.*;
import flash.net.*;
import flash.utils.*;
import flash.system.Security;

public class InsimSocket extends Socket {
private var IRHost:String;
private var IRPort:int;
private var Connected:Boolean = false;
private var ReConnCount:uint = 0;

private var ByteBuffer:ByteArray = new ByteArray (); // Byte buffer object to receive network traffic in

private var AliveTimer:uint = 0; // Hold a reference for the ACK setTimeout
private var LastIn:int = 0; // Time of last packet
private var ConnReqI:int = -1;
private var RstReqI:int = -1;

private var tmpBytes:ByteArray = new ByteArray(); // 'Static' byte array object, used whenever we need a temporary byte array

public function InsimSocket (_IRHost:String, _IRPort:int):void {
IRHost = _IRHost;
IRPort = _IRPort;

super (IRHost, IRPort);
configureListeners ();

tmpBytes.endian = "littleEndian";
}

private function configureListeners ():void {
addEventListener (Event.CONNECT, connectHandler);
addEventListener (Event.CLOSE, closeHandler);
addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityHandler);
addEventListener (IOErrorEvent.IO_ERROR, errorHandler);
addEventListener (ProgressEvent.SOCKET_DATA, dataHandler);
}

private function connectHandler (e:Event):void {
Main.Msgs.addMsg ("InSim Relay connection created");

Connected = true;
ReConnCount = 0;

// Initial acknowledgement packet timer
AliveTimer = setTimeout (sendACK, 35000);
}

private function closeHandler (e:Event):void {
Main.Msgs.addMsg ("InSim Relay connection closed by server.");
if (ReConnCount++ < 3)
Main.Msgs.addMsg ("Will try to reconnect to the relay in 5 seconds.");

Connected = false;

setTimeout (reconnect, 5000);
}

private function reconnect ():void {
if (ReConnCount < 4) {
Main.Msgs.addMsg ("Reconnection attempt #"+ReConnCount);
connect (IRHost, IRPort);
} else {
Main.Msgs.addMsg ("Tried 3 reconnects, but they failed. Giving up.");
}
}

private function securityHandler (e:SecurityErrorEvent):void {
Main.Msgs.addMsg ("InSim Relay connection failed : "+e+" - "+e.text);
}

private function errorHandler (e:IOErrorEvent):void {
Main.Msgs.addMsg ("IO Error : "+e.text);

// Error ocurred when trying to make a connection - try to reconnect
if (Connected == false) {
setTimeout (reconnect, 3000);
}
}

private function dataHandler (e:ProgressEvent):void {
LastIn = getTimer ();

readBytes (ByteBuffer, ByteBuffer.length);
var Packet:ByteArray;
while (true) {
Packet = findPacket ();
if (Packet.length == 0)
break;

Packet.endian = "littleEndian";

// We've found a packet! Now let's see what to do with it
var Type:int = Packet.readUnsignedByte ();
//trace (Type);

switch (Type) {
case 2 : // VER
handleVER (Packet);
break;
case 3 : // TINY
handleTINY (Packet);
break;
case 4 : // SMALL
handleSMALL (Packet);
break;

case 5 : // STA
handleSTA (Packet);
break;

case 10 : // ISM
handleISM (Packet);
break;

case 11 : // MSO
handleMSO (Packet);
break;
case 12 : // III
break;
case 13 : // MST
break;
case 14 : // MTC
break;

case 16 : // VTN
break;
case 17 : // RST
handleRST (Packet);
break;

case 18 : // NCN
handleNCN (Packet);
break;
case 19 : // CNL
handleCNL (Packet);
break;
case 20 : // CPR
handleCPR (Packet);
break;

case 21 : // NPL
handleNPL (Packet);
break;
case 22 : // PLP
handlePLP (Packet);
break;
case 23 : // PLL
handlePLL (Packet);
break;

case 24 : // LAP
handleLAP (Packet);
break;
case 25 : // SPX
handleSPX (Packet);
break;

case 26 : // PIT
handlePIT (Packet);
break;
case 27 : // PSF
handlePSF (Packet);
break;
case 28 : // PLA
break;

case 29 : // CCH
break;

case 30 : // PEN
handlePEN (Packet);
break;
case 31 : // TOC
handleTOC (Packet);
break;
case 32 : // FLG
handleFLG (Packet);
break;
case 33 : // PFL
handlePFL (Packet);
break;

case 34 : // FIN
handleFIN (Packet);
break;
case 35 : // RES
handleRES (Packet);
break;
case 36 : // REO
break;

case 38 : // MCI
handleMCI (Packet);
break;

case 39 : // MSX
break;

case 41 : // CRS
break;

case 42 : // BFN
break;

case 43 : // AXI
break;
case 44 : // AXO
break;

case 45 : // BTN
break;
case 46 : // BTC
break;
case 47 : // BTT
break;

case 48 : // RIP
break;
case 49 : // SSH
break;

case 50 : // CON
handleCON(Packet);
break;

case 251 : // IRP_ARP (in)
handleARP (Packet);
break;
case 252 : // IRP_HLR (out)
break;
case 253 : // IRP_HOS (in)
Main.HostLister.parseHostPacket (Packet);
break;
case 254 : // IRP_SEL (out)
break;
case 255 : // IRP_ERR (in)
handleERR (Packet);
break;
default :
//Helpers.dbMsg ("Unknown packet found of type "+Type);
trace ("Unknown packet found of type "+Type);
break;
}
}
Packet = null;
}

private function findPacket ():ByteArray {
var Packet:ByteArray = new ByteArray ();
var SizeByte:int = 0;

if (ByteBuffer.length == 0)
return Packet;

// Read sizebyte
ByteBuffer.position = 0;
SizeByte = ByteBuffer.readUnsignedByte ();

// Check if we have a full packet
if (ByteBuffer.length >= SizeByte) {
ByteBuffer.readBytes (Packet, 0, SizeByte-1);

if (ByteBuffer.length == SizeByte) {
ByteBuffer = new ByteArray ();
} else {
var NewBuffer:ByteArray = new ByteArray ();
ByteBuffer.readBytes (NewBuffer, 0);
ByteBuffer = NewBuffer;
}
}

return Packet;
}

private function sendACK ():void {
if (!connected)
return;

writeByte (4);
writeByte (3);
writeByte (0);
writeByte (0);
flush ();
AliveTimer = setTimeout (sendACK, 25000);
}

private function requestRVR ():void {
writeByte (4);
writeByte (3);
writeByte (1);
writeByte (255);
flush ();
}

private function requestVER ():void {
writeByte (4);
writeByte (3);
writeByte (1);
writeByte (1);
flush ();
}

// ... etc for requestXXX() methods

private function handleVER (packet:ByteArray):void {
var reqi:uint = packet.readUnsignedByte ();
//trace ('RECEIVED : '+reqi);
if (reqi != ConnReqI)
return;
ConnReqI = -1;

// We have made a new connection to a host. Need to reset all host stuff.
if (Main.HI != null) {
Main.HI.destroy ();
Main.HI = null;
}
Main.HI = new HostInfoObject ();

packet.readUnsignedByte ();
Main.HI.HostVersion = packet.readMultiByte (8, 'cp1252');
Main.HI.Product = packet.readMultiByte (6, 'cp1252');
Main.HI.InsimVersion = packet.readUnsignedShort ();

Main.closeHostLister ();
Main.Options.resize ();
if (Main.ShowOptions)
Main.Options.visible = true;

requestARP ();
requestISM ();
requestSTA ();
requestRST ();
}

// ... etc for handleXXX() methods
}
}

Can you show yet only code for sending InSim App Name and Admin Pass in ISI packet? Like you see, I don't know how to send char[] or something like that.
yes that is a bitch

// This just shows how to write a String into a binary stream. It pads the adminpassword with Null to 16 bytes.
// This should be inside the Socket class : writeMultiByte and writeByte are methods of the Socket class.
var len:int = 0;

var AdminPass:String = _AdminPass.substr (0, 15);
writeMultiByte (AdminPass, 'cp1252');
len = AdminPass.length;
for (a=len; a<16; a++)
writeByte (0);

Thank you very very much. I thought that, but I thought there is faster way. Not this time

InSim in Flash
(20 posts, started )
FGED GREDG RDFGDR GSFDG