/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #include "binding_helpers.h" #include "http_curl/http_curl.h" #include "script_squirrel/legacy/squirrel_binding.h" #include "script/script_variant.h" using namespace GS; using namespace GS::Script; #if __PLATFORM_EMSCRIPTEN__ //------------------------------------------------------------------------------ SQInteger HttpPost(HSQUIRRELVM vm) { __SQ_RETURN } SQInteger HttpUpdate(HSQUIRRELVM vm) { __SQ_RETURN } //------------------------------------------------------------------------------ #else //------------------------------------------------------------------------------ class SquirrelHTTP : public HTTP::Curl { HSQUIRRELVM vm; public: void OnRequestComplete(int ticket_id, const Array &data) { SquirrelVM *sq_vm = GetVMObject(vm); if (sq_vm->SetupFunctionCall("OnHttpRequestComplete")) { sq_vm->PushArgument(ticket_id); if (data.GetSize() > 0) { String str(data.c_ptr(), data.GetCount()); sq_vm->PushArgument(str.c_str()); } else sq_vm->PushNullArgument(); sq_vm->DoFunctionCall(); } } void OnRequestError(int ticket_id) { SquirrelVM *sq_vm = GetVMObject(vm); if (sq_vm->SetupFunctionCall("OnHttpRequestError")) { sq_vm->PushArgument(ticket_id); sq_vm->DoFunctionCall(); } } SquirrelHTTP(HSQUIRRELVM v) : vm(v) {} }; //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ SQInteger HttpPost(HSQUIRRELVM vm) { __SQ_GETSTART(2) __SQ_GETSTRING(url) __SQ_GETSTRING(post) SquirrelVM *sq_vm = GetVMObject(vm); int id = sq_vm->http->Post(url, post); __SQ_GETEND __SQ_RETURNINT(id) } SQInteger HttpUpdate(HSQUIRRELVM vm) { SquirrelVM *sq_vm = GetVMObject(vm); sq_vm->http->Update(); __SQ_RETURN } //------------------------------------------------------------------------------ //#include #include SQInteger GetIp(HSQUIRRELVM vm) { String ip_adress; char ac[80]; if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) { __SQ_RETURNSTRING(""); } struct hostent *phe = gethostbyname(ac); if (phe == 0) { __SQ_RETURNSTRING(""); } for (int i = 0; phe->h_addr_list[i] != 0; ++i) { struct in_addr addr; memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr)); ip_adress += String(inet_ntoa(addr)) + String(" "); } __SQ_RETURNSTRING(ip_adress.c_str()) } //********************************************************* #endif void RegisterHTTPBinding(HSQUIRRELVM vm) { #if __PLATFORM_EMSCRIPTEN__ == 0 SquirrelVM *sq_vm = GetVMObject(vm); sq_vm->http = new SquirrelHTTP(vm); #endif /*# Topic: HTTP Desc: Send HTTP POST request to a remote URL and receive remote response asynchronously. #*/ /*# Section: HTTP helper Desc: HTTP helper functions. #*/ /*# Func: HttpUpdate Proto: void: Desc: Update the HTTP subsystem. You must call this function to receive queued events. See: HttpPost #*/ sq_register(vm, HttpUpdate, "HttpUpdate", _SC(".")); /*# Func: HttpPost Proto: int:String url, String post Desc: POST an HTTP request to a remote URL and returns the request identifier.
This function returns immediately, the request result will be sent to the global HTTP script callbacks. Example: // POST a request with two parameters to a remote server. local id = HttpPost("http://www.someurl.com", "param_a=1&param_b=2") print("HTTP request posted, id: " + id) // The two global HTTP request callbacks. function OnHttpRequestComplete(ticket_id, data) { print("HTTP request " + ticket_id + " complete.") print("Data received: " + data) } function OnHttpRequestError(ticket_id) { print("HTTP request " + ticket_id + " errored.") } #*/ sq_register(vm, HttpPost, "HttpPost", _SC(".ss")); /*# Func: GetIp Proto: string:void Desc: Get local ip. #*/ sq_register(vm, GetIp, "GetIp", _SC(".")); }