NWN2-AI-Server/nwn2ai_onmoduleload/nwn2ai_onmoduleload.cs

124 lines
3.7 KiB
C#

//
// This script serves as a basis for new scripts. New scripts can copy this
// source file to start out.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Reflection.Emit;
using CLRScriptFramework;
using NWScript;
using NWScript.ManagedInterfaceLayer.NWScriptManagedInterface;
using NWEffect = NWScript.NWScriptEngineStructure0;
using NWEvent = NWScript.NWScriptEngineStructure1;
using NWLocation = NWScript.NWScriptEngineStructure2;
using NWTalent = NWScript.NWScriptEngineStructure3;
using NWItemProperty = NWScript.NWScriptEngineStructure4;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace CLRScript
{
public partial class nwn2ai_onmoduleload : CLRScriptBase, ICLRScriptImplementation, IGeneratedScriptProgram
{
public nwn2ai_onmoduleload([In] NWScriptJITIntrinsics Intrinsics, [In] INWScriptProgram Host)
{
InitScript(Intrinsics, Host);
}
private nwn2ai_onmoduleload([In] nwn2ai_onmoduleload Other)
{
InitScript(Other);
LoadScriptGlobals(Other.SaveScriptGlobals());
}
//
// Include the list of types for parameters to the main function here.
// An empty list means no parameters.
//
public static Type[] ScriptParameterTypes = { };
StreamWriter debugFile;
public Int32 ScriptMain([In] object[] ScriptParameters, [In] Int32 DefaultReturnCode)
{
debugFile = File.AppendText("nwnx4\\customlog.txt");
Debug("====================================> Start");
sockBufferIn = new Queue<Tuple<string,Socket>>();
sockClients = new Dictionary<string,Socket>();
SocketConfigure();
AssignCommand(OBJECT_SELF, delegate() { ActionSpeakString("Start!", TALKVOLUME_TALK); });
DelayCommand(0.5f, Heartbeat);
return DefaultReturnCode;
}
public void Heartbeat()
{
ActionSpeakString("hb", TALKVOLUME_TALK);
DelayCommand(0.5f, Heartbeat);
//Process queued requests
}
void SocketConfigure()
{
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);
sockServer.BeginAccept(new AsyncCallback(RequestHandler), null);
}
void RequestHandler(IAsyncResult state)
{
Socket sockClient = sockServer.EndAccept(state);
sockServer.BeginAccept(new AsyncCallback(RequestHandler), null);
sockClient.Blocking = true;
while (sockClient.Connected) {
byte[] rawData = new byte[sockClient.Available];
if (sockClient.Available > 0) {
sockClient.Receive(rawData, rawData.Length, 0);
string data = Encoding.UTF8.GetString(rawData);
Debug("Queued "+data);
sockBufferIn.Enqueue(new Tuple<string,Socket>(data, sockClient));
}
else
Thread.Sleep(100);//TODO: Find another way than sleeping
}
}
Socket sockServer;
Queue<Tuple<string,Socket>> sockBufferIn;//Request & client
Dictionary<string, Socket> sockClients;//PNJ tag => Client socket
private void Debug(string msg){
debugFile.WriteLine(DateTime.Now.ToLongTimeString()+": "+msg);
debugFile.Flush();
}
}
}