From 4292f4b4defd0a71ecbf0ae5c943c65e5dbcb46f Mon Sep 17 00:00:00 2001 From: "Crom (Thibaut CHARLES)" Date: Thu, 16 Apr 2015 23:58:33 +0200 Subject: [PATCH] Removed grapevine, moved to sockets (need for bi-directional communication) --- nwn2_ai_onmoduleload/nwn2_ai_onmoduleload.cs | 56 +++++++++++++++---- .../nwn2_ai_onmoduleload.csproj | 3 - 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/nwn2_ai_onmoduleload/nwn2_ai_onmoduleload.cs b/nwn2_ai_onmoduleload/nwn2_ai_onmoduleload.cs index 2dd5489..4b252ee 100644 --- a/nwn2_ai_onmoduleload/nwn2_ai_onmoduleload.cs +++ b/nwn2_ai_onmoduleload/nwn2_ai_onmoduleload.cs @@ -22,9 +22,10 @@ using NWTalent = NWScript.NWScriptEngineStructure3; using NWItemProperty = NWScript.NWScriptEngineStructure4; using System.Threading; -using Grapevine.Server; using System.IO; using System.Net; +using System.Net.Sockets; + namespace CLRScript { public partial class nwn2_ai_onmoduleload : CLRScriptBase, ICLRScriptImplementation, IGeneratedScriptProgram @@ -50,8 +51,9 @@ namespace CLRScript public Int32 ScriptMain([In] object[] ScriptParameters, [In] Int32 DefaultReturnCode) { - server = new RESTServer("127.0.0.1", "8080", "http", "index.html", "C:\\nowhere", 1); - server.Start(); + sockBufferIn = new Queue(); + sockClients = new Dictionary(); + SocketConfigure(); AssignCommand(OBJECT_SELF, delegate() { ActionSpeakString("Start!", TALKVOLUME_TALK); }); DelayCommand(0.5f, Heartbeat); @@ -63,18 +65,52 @@ namespace CLRScript { ActionSpeakString("hb", TALKVOLUME_TALK); DelayCommand(0.5f, Heartbeat); + + //Process queued requests } - - RESTServer server; - public sealed class MyResource : RESTResource + void SocketConfigure() { - [RESTRoute] - public void HandleAllGetRequests(HttpListenerContext context) - { - SendTextResponse(context, "GET is a success!"); + sockServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + IPEndPoint iep = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 8080); + sockServer.Bind(iep); + sockServer.Listen(10); + + sockThread = new Thread(new ThreadStart(SockerHandler)); + sockThread.Start(); + } + + void SockerHandler() + { + while(true){ + Socket sockClient = sockServer.Accept(); + sockServer.BeginAccept(new AsyncCallback(RequestHandler), sockServer); } } + + void RequestHandler(IAsyncResult state) + { + //Socket listener = (Socket)state.AsyncState; + //Socket handler = listener.EndAccept(state); + Socket sockClient = sockServer.EndAccept(state); + + byte[] rawLength = new byte[4]; + sockClient.Receive(rawLength); + + int length = rawLength[0]<<8 + rawLength[1]; + byte[] rawData = new byte[length]; + sockClient.Receive(rawData); + + string data = Encoding.UTF8.GetString(rawData); + sockBufferIn.Enqueue(data); + } + + Socket sockServer; + Thread sockThread; + Queue sockBufferIn; + + //PNJ tag => Client socket + Dictionary sockClients; } } diff --git a/nwn2_ai_onmoduleload/nwn2_ai_onmoduleload.csproj b/nwn2_ai_onmoduleload/nwn2_ai_onmoduleload.csproj index a9f8a01..b0c91e0 100644 --- a/nwn2_ai_onmoduleload/nwn2_ai_onmoduleload.csproj +++ b/nwn2_ai_onmoduleload/nwn2_ai_onmoduleload.csproj @@ -32,9 +32,6 @@ 4 - - ..\grapevine\Grapevine\bin\Debug\Grapevine.dll - ..\CLRScriptSDK\NWNScriptJITIntrinsics.dll