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?
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
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
}
}
// 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);