Author Topic: Question: TCPObject Listen  (Read 798 times)

What does a TCPObject Listen do?

i.e.
new tcpObject(Test);
Test.listen(*codehere*);

It starts accepting connections on the port specified in the first argument.

You'll need to create a ::onConnectRequest method that creates TCPObject instances for clients. Here's an example:

Code: [Select]
function ServerTCP::onConnectRequest(%this,%ip,%socket)
{
   if(isObject(%this.connection[%ip]))
   {
      echo(%this.getName() @ ": Got duplicate connection from" SPC %ip);
      %this.connection[%ip].disconnect();
      %this.connection[%ip].delete();
   }
   echo(%this.getName() @ ": Creating connection to" SPC %ip);
   %this.connection[%ip] = new TCPobject("",%socket) { class = ConnectionTCP; parent = %this; };
}
function ConnectionTCP::onLine(%this,%line)
{
   // do stuff here
}

Obviously this is just an example but you should be able to figure out what to do with it from there.
« Last Edit: September 22, 2012, 03:24:38 PM by Port »

If you wanted a TCPObject to download a text file every other second, how would you do this? Pretty much just looking for a change in the data. I know you can just use a schedule, but is there a better way to do it?

If you wanted a TCPObject to download a text file every other second, how would you do this? Pretty much just looking for a change in the data. I know you can just use a schedule, but is there a better way to do it?

This has nothing to do with the listen method. And there is no better way unless you have direct access to the behavior of the remote host.

It starts accepting connections on the port specified in the first argument.

However, you have to specify TCPObject::onConnectRequest.

You can see a proper definition in my Script_TelChat add-on which I'll be releasing later today.

It would require a tcp server on the host that has the text file.
However, you have to specify TCPObject::onConnectRequest.

You can see a proper definition in my Script_TelChat add-on which I'll be releasing later today.
His edit flew right over your head.

However, you have to specify TCPObject::onConnectRequest.

You can see a proper definition in my Script_TelChat add-on which I'll be releasing later today.

You didn't read anything else than the first line in my post, did you?

How do use tcp objects to download file from website?



Doesn't find something that explains how

Code: [Select]
//*********************************************************
//* Update Downloader
//*********************************************************
//- RTB_Client_Updater::downloadUpdate (downloads a new version)
function RTB_Client_Updater::downloadUpdate(%this,%version)
{
   if(!isWriteableFilename("Add-Ons/System_ReturnToBlockland.zip"))
   {
      MessageBoxOK("Oh Dear!","Your System_ReturnToBlockland.zip is read-only and cannot be overwritten.\n\nPlease download the latest RTB manually from our website, or set System_ReturnToBlockland.zip to not be read-only.");
      return;
   }
   
   if(isObject(RTB_Client_Updater_TCP))
      RTB_Client_Updater_TCP.setName("");
   
   %tcp = new TCPObject(RTB_Client_Updater_TCP)
   {
      version = %version;
   };
   RTBGroup.add(%tcp);

   %tcp.connect("api.returntoblockland.com:80");
   
   RTBCU_UpdateButton.setActive(0);
   RTBCU_ProgressText.setText("Locating Update...");
}

//- RTB_Client_Updater_TCP::onConnected (Connection success callback)
function RTB_Client_Updater_TCP::onConnected(%this)
{
   %content = "c=DLUPDATE&n="@$Pref::Player::NetName@"&arg1="@%this.version@"&"@$RTB::Connection::Session;
   %contentLen = strLen(%content);
   
   %this.send("POST /apiRouter.php?d=APIUM HTTP/1.1\r\nHost: api.returntoblockland.com\r\nUser-Agent: Torque/1.0\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: "@%contentLen@"\r\n\r\n"@%content@"\r\n");
}

//- RTB_Client_Updater_TCP::onLine (Callback for line response)
function RTB_Client_Updater_TCP::onLine(%this,%line)
{
   if(strPos(%line,"404 Not Found") >= 0)
   {
      MessageBoxOK("Error!","An error occured with the updater service and the update could not be located.");
      canvas.popDialog(RTB_Updater);
      return;
   }
   
   if(strPos(%line,"DL-Result:") $= 0)
   {
      %this.dlResult = getWord(%line,1);
      if(getWord(%line,1) $= 0)
      {
         MessageBoxOK("Error!","An error occured with the updater service and the update could not be located.");
         Canvas.popDialog(RTB_Updater);
         return;
      }
   }
   
   if(strPos(%line,"Content-Length:") $= 0)
      %this.contentSize = getWord(%line,1);
     
   if(%line $= "")
   {
      if(%this.dlResult !$= 1)
      {
         MessageBoxOK("Error!","An error occured with the updater service and the update could not be located.");
         Canvas.popDialog(RTB_Updater);
         return;
      }
      %this.setBinarySize(%this.contentSize);
   }
}

//- RTB_Client_Updater_TCP::onBinChunk (On binary chunk received)
function RTB_Client_Updater_TCP::onBinChunk(%this,%chunk)
{
   if(%this.timeStarted $= "")
      %this.timeStarted = getSimTime();
     
   if(%chunk >= %this.contentSize)
   {
      if(isWriteableFilename("Add-Ons/System_ReturnToBlockland.zip"))
      {
         %this.saveBufferToFile("Add-Ons/System_ReturnToBlockland.zip.new");
         %this.disconnect();
         
         RTBCU_Progress.setValue(1);
         RTBCU_ProgressText.setText("Download Complete");
         RTBCU_Speed.setText("N/A");
         RTBCU_Done.setText(byteRound(%this.contentSize));
         
         if(fileDelete("Add-Ons/System_ReturnToBlockland.zip"))
         {
            fileCopy("Add-Ons/System_ReturnToBlockland.zip.new","Add-Ons/System_ReturnToBlockland.zip");
            fileDelete("Add-Ons/System_ReturnToBlockland.zip.new");
           
            MessageBoxOK("Huzzah!","You have successfully downloaded RTB v"@%this.version@".\n\nBlockland must now close to complete the install.","quit();");
         }
         else
         {
            MessageBoxOK("Whoops!","Unable to delete System_ReturnToBlockland.zip to replace it with the new version.\n\nPlease go to your Add-Ons folder and replace System_ReturnToBlockland.zip with System_ReturnToBlockland.zip.new to complete the update.");
         }
      }
      else
      {
         MessageBoxOK("Oh Dear!","Unable to save RTB v"@%this.version@". Your System_ReturnToBlockland.zip is read-only and cannot be overwritten.\n\nPlease download the latest RTB manually from the website.");
      }
   }
   else
   {
      RTBCU_Progress.setValue(%chunk/%this.contentSize);
      RTBCU_ProgressText.setText(mFloor((%chunk/%this.contentSize)*100)@"%");
      RTBCU_Speed.setText(mFloatLength(%chunk/(getSimTime()-%this.timeStarted),2)@"kb/s");
      RTBCU_Done.setText(byteRound(%chunk));
   }
}

//- RTB_Client_Updater_TCP::onDisconnect (disconnected callback)
function RTB_Client_Updater_TCP::onDisconnect(%this)
{
   %this.delete();
}

From System_ReturnToBlockland/modules/client/updater.cs.

Doesn't find something that explains how
I've seen 5 topics about it over the past year
every single one explains how.

You didn't read anything else than the first line in my post, did you?
edits aren't notified up in 'you may wish to review your post'

forget me, right?

Yeah there's a good idea, let's chew out lugnut for a mistake!