Removed grapevine, moved to sockets (need for bi-directional communication)

This commit is contained in:
Crom (Thibaut CHARLES) 2015-04-16 23:58:33 +02:00
parent af631a9b24
commit 4292f4b4de
2 changed files with 46 additions and 13 deletions

View File

@ -22,9 +22,10 @@ using NWTalent = NWScript.NWScriptEngineStructure3;
using NWItemProperty = NWScript.NWScriptEngineStructure4; using NWItemProperty = NWScript.NWScriptEngineStructure4;
using System.Threading; using System.Threading;
using Grapevine.Server;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets;
namespace CLRScript namespace CLRScript
{ {
public partial class nwn2_ai_onmoduleload : CLRScriptBase, ICLRScriptImplementation, IGeneratedScriptProgram public partial class nwn2_ai_onmoduleload : CLRScriptBase, ICLRScriptImplementation, IGeneratedScriptProgram
@ -50,8 +51,9 @@ namespace CLRScript
public Int32 ScriptMain([In] object[] ScriptParameters, [In] Int32 DefaultReturnCode) public Int32 ScriptMain([In] object[] ScriptParameters, [In] Int32 DefaultReturnCode)
{ {
server = new RESTServer("127.0.0.1", "8080", "http", "index.html", "C:\\nowhere", 1); sockBufferIn = new Queue<string>();
server.Start(); sockClients = new Dictionary<string,Socket>();
SocketConfigure();
AssignCommand(OBJECT_SELF, delegate() { ActionSpeakString("Start!", TALKVOLUME_TALK); }); AssignCommand(OBJECT_SELF, delegate() { ActionSpeakString("Start!", TALKVOLUME_TALK); });
DelayCommand(0.5f, Heartbeat); DelayCommand(0.5f, Heartbeat);
@ -63,18 +65,52 @@ namespace CLRScript
{ {
ActionSpeakString("hb", TALKVOLUME_TALK); ActionSpeakString("hb", TALKVOLUME_TALK);
DelayCommand(0.5f, Heartbeat); DelayCommand(0.5f, Heartbeat);
//Process queued requests
} }
void SocketConfigure()
RESTServer server;
public sealed class MyResource : RESTResource
{ {
[RESTRoute] sockServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public void HandleAllGetRequests(HttpListenerContext context) IPEndPoint iep = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 8080);
{ sockServer.Bind(iep);
SendTextResponse(context, "GET is a success!"); 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<string> sockBufferIn;
//PNJ tag => Client socket
Dictionary<string, Socket> sockClients;
} }
} }

View File

@ -32,9 +32,6 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Grapevine">
<HintPath>..\grapevine\Grapevine\bin\Debug\Grapevine.dll</HintPath>
</Reference>
<Reference Include="NWNScriptJITIntrinsics, Version=1.0.0.1, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="NWNScriptJITIntrinsics, Version=1.0.0.1, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\CLRScriptSDK\NWNScriptJITIntrinsics.dll</HintPath> <HintPath>..\CLRScriptSDK\NWNScriptJITIntrinsics.dll</HintPath>
</Reference> </Reference>