Skip to content

Commit

Permalink
Merge pull request #40 from sigmantium/master
Browse files Browse the repository at this point in the history
Added params to OS::OpenConnection()
  • Loading branch information
turleypol committed Oct 21, 2017
2 parents 8dc1317 + ffcf169 commit bae849a
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 32 deletions.
12 changes: 11 additions & 1 deletion docs/docs.polserver.com/pol099/corechanges.xml
Expand Up @@ -2,9 +2,19 @@
<ESCRIPT>
<header>
<topic>Latest Core Changes</topic>
<datemodified>09-23-2017</datemodified>
<datemodified>10-21-2017</datemodified>
</header>
<version name="POL099">
<entry>
<date>10-20-2017</date>
<author>DevGIB:</author>
<change type="Changed">Added assume_string flag to OS::OpenConnection() to allow you to force the connection script to run as AUXSVC_ASSUME_STRING script option before the script loads in case of fast responding connections.</change>
</entry>
<entry>
<date>10-11-2017</date>
<author>DevGIB:</author>
<change type="Changed">Added params to OS::OpenConnection() to allow you to pass parameters to the script that is run for the connection.</change>
</entry>
<entry>
<date>09-23-2017</date>
<author>Turley:</author>
Expand Down
7 changes: 5 additions & 2 deletions docs/docs.polserver.com/pol099/osem.xml
Expand Up @@ -226,11 +226,14 @@ const SCRIPTOPT_CAN_ACCESS_OFFLINE_MOBILES := 4;</code></explain>
</function>

<function name="OpenConnection">
<prototype>OpenConnection( host, port, scriptdef )</prototype>
<prototype>OpenConnection( host, port, scriptdef, params )</prototype>
<parameter name="host" value="Target host"/>
<parameter name="port" value="Target port"/>
<parameter name="scriptdef" value="Name of the script to be started when the connection is established"/>
<explain>creates an outgoing TCP/IP connection</explain>
<parameter name="params" value="A struct of parameters to be sent to the script"/>
<parameter name="assume_string" value="Integer if set to 1 all communication from connection will be sent/received as raw strings."/>
<explain>Creates an outgoing TCP/IP connection to the host/port, once connection is open the scriptdef is run and</explain>
<explain> any params defined in the struct will be passed to that script. The script type should be in the form of an Auxilry Script.</explain>
<return>1 on success</return>
</function>

Expand Down
4 changes: 4 additions & 0 deletions pol-core/doc/core-changes.txt
@@ -1,4 +1,8 @@
-- POL099 --
10-20-2017 DevGIB:
Changed: Added assume_string flag to OS::OpenConnection() to allow you to force the connection script to run as AUXSVC_ASSUME_STRING script option before the script loads in case of fast responding connections.
10-11-2017 DevGIB:
Changed: Added params to OS::OpenConnection() to allow you to pass parameters to the script that is run for the connection.
09-23-2017 Turley:
Added: array.sort(int sub_index:=0) the optional parameter specifies the index used for sorting in the case of an array of arrays
09-10-2017 Turley:
Expand Down
6 changes: 0 additions & 6 deletions pol-core/pol/globals/network.cpp
Expand Up @@ -49,12 +49,10 @@ NetworkManager::NetworkManager()
ext_handler_table(),
packetsSingleton( new Network::PacketsSingleton() ),
clientTransmit( new Network::ClientTransmit() ),
#ifdef PERGON
auxthreadpool( new threadhelp::DynTaskThreadPool( "AuxPool" ) ), // TODO: seems to work
// activate by default?
// maybe add a cfg entry for
// max number of threads
#endif
banned_ips(),
polsocket()
{
Expand Down Expand Up @@ -115,9 +113,7 @@ void NetworkManager::deinialize()

// unload_aux_service
Clib::delete_all( auxservices );
#ifdef PERGON
auxthreadpool.reset();
#endif
banned_ips.clear();
#ifdef _WIN32
closesocket( polsocket.listen_socket );
Expand Down Expand Up @@ -187,9 +183,7 @@ NetworkManager::Memory NetworkManager::estimateSize() const

usage.misc += packetsSingleton->estimateSize();
usage.misc += sizeof( Network::ClientTransmit );
#ifdef PERGON
usage.misc += sizeof( threadhelp::DynTaskThreadPool );
#endif

usage.misc += 3 * sizeof( Network::IPRule* ) + banned_ips.capacity() * sizeof( Network::IPRule );

Expand Down
2 changes: 0 additions & 2 deletions pol-core/pol/globals/network.h
Expand Up @@ -100,9 +100,7 @@ class NetworkManager : boost::noncopyable

std::unique_ptr<Network::ClientTransmit> clientTransmit;

#ifdef PERGON
std::unique_ptr<threadhelp::DynTaskThreadPool> auxthreadpool;
#endif

std::vector<Network::IPRule> banned_ips;

Expand Down
55 changes: 40 additions & 15 deletions pol-core/pol/module/osmod.cpp
Expand Up @@ -587,9 +587,11 @@ BObjectImp* OSExecutorModule::mf_OpenConnection()
{
const String* host;
const String* scriptname_str;
BObjectImp* imp;
unsigned short port;
int escriptint;
if ( ( host = getStringParam( 0 ) ) != NULL && getParam( 1, port ) &&
( scriptname_str = getStringParam( 2 ) ) != NULL )
( scriptname_str = getStringParam( 2 ) ) != NULL && (imp = getParamImp( 3 )) != NULL && getParam( 4, escriptint ))
{
// FIXME needs to inherit available modules?
Core::ScriptDef sd; // = new ScriptDef();
Expand All @@ -602,21 +604,44 @@ BObjectImp* OSExecutorModule::mf_OpenConnection()
{
return new BError( "Script " + sd.name() + " does not exist." );
}
if (!this_uoexec->suspend())
{
DEBUGLOG << "Script Error in '" << this_uoexec->scriptname() << "' PC=" << this_uoexec->PC
<< ": \n"
<< "\tThe execution of this script can't be blocked!\n";
return new Bscript::BError("Script can't be blocked");
}

weak_ptr<Core::UOExecutor> uoexec_w = this_uoexec->weakptr;
std::string hostname(host->value());
bool assume_string = escriptint != 0;
Core::networkManager.auxthreadpool->push([uoexec_w, sd, hostname, port, imp, assume_string]() {
Clib::Socket s;
bool success_open = s.open(hostname.c_str(), port);
{
Core::PolLock lck;
if (!uoexec_w.exists())
{
INFO_PRINT << "Script has been destroyed\n";
s.close();
return;
}
if (!success_open)
{
uoexec_w.get_weakptr()->ValueStack.back().set(
new BObject(new BError("Error connecting to client")));
}
else
{
uoexec_w.get_weakptr()->ValueStack.back().set(new BObject(new BLong(1)));
}
uoexec_w.get_weakptr()->os_module->revive();
}
std::unique_ptr<Network::AuxClientThread> client(new Network::AuxClientThread(sd, s, imp->copy(),assume_string));
client->run();
});

// Socket* s = new Socket();
// bool success_open = s->open(host->value().c_str(),30);
Clib::Socket s;
bool success_open = s.open( host->value().c_str(), port );

if ( !success_open )
{
// delete s;
return new BError( "Error connecting to client" );
}
Clib::SocketClientThread* clientthread = new Network::AuxClientThread( sd, s );
clientthread->start();

return new BLong( 1 );
return new BLong( 0 );
}
else
{
Expand Down
10 changes: 7 additions & 3 deletions pol-core/pol/network/auxclient.cpp
Expand Up @@ -108,8 +108,8 @@ AuxClientThread::AuxClientThread( AuxService* auxsvc, Clib::SocketListener& list
: SocketClientThread( listener ), _auxservice( auxsvc ), _uoexec( 0 )
{
}
AuxClientThread::AuxClientThread( Core::ScriptDef scriptdef, Clib::Socket& sock )
: SocketClientThread( sock ), _auxservice( 0 ), _scriptdef( scriptdef ), _uoexec( 0 )
AuxClientThread::AuxClientThread( Core::ScriptDef scriptdef, Clib::Socket& sock, Bscript::BObjectImp* params, bool assume_string )
: SocketClientThread( sock ), _auxservice( 0 ), _uoexec(0), _scriptdef(scriptdef), _params(params), _assume_string(assume_string)
{
}

Expand All @@ -124,8 +124,12 @@ bool AuxClientThread::init()
if ( _auxservice )
uoemod = Core::start_script( _auxservice->scriptdef(), _auxconnection.get() );
else
uoemod = Core::start_script( _scriptdef, _auxconnection.get() );
uoemod = Core::start_script( _scriptdef, _auxconnection.get(), _params);
_uoexec = uoemod->uoexec.weakptr;
if (_assume_string == true)
{
uoemod->uoexec.auxsvc_assume_string = _assume_string;
}
return true;
}
else
Expand Down
6 changes: 4 additions & 2 deletions pol-core/pol/network/auxclient.h
Expand Up @@ -85,7 +85,7 @@ class AuxClientThread : public Clib::SocketClientThread
{
public:
AuxClientThread( AuxService* auxsvc, Clib::SocketListener& listener );
AuxClientThread( Core::ScriptDef scriptdef, Clib::Socket& sock );
AuxClientThread( Core::ScriptDef scriptdef, Clib::Socket& sock, Bscript::BObjectImp* params, bool assume_string);
virtual void run() POL_OVERRIDE;
void transmit( const Bscript::BObjectImp* imp );
Bscript::BObjectImp* get_ip();
Expand All @@ -94,10 +94,12 @@ class AuxClientThread : public Clib::SocketClientThread
bool init();
bool ipAllowed( sockaddr MyPeer );

weak_ptr<Core::UOExecutor> _uoexec;
AuxService* _auxservice;
Core::ScriptDef _scriptdef;
Bscript::BObjectImp* _params;
bool _assume_string;
ref_ptr<AuxConnection> _auxconnection;
weak_ptr<Core::UOExecutor> _uoexec;
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion pol-core/support/scripts/os.em
Expand Up @@ -104,5 +104,5 @@ Set_Event_Queue_Size(size);
Is_Critical();

OpenURL( character, url );
OpenConnection( host, port, scriptdef);
OpenConnection( host, port, scriptdef, params := 0, assume_string := 0);
Debugger(); // put script in debug state

0 comments on commit bae849a

Please sign in to comment.