Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Well this is my C# URL_LOAD which is not even close to the "Not multitasking one" by KKID which was a C++ project..
- using RGiesecke.DllExport;
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Linq;
- namespace ARMAExtCS
- {
- public class DllEntry // This can be named anything you like
- {
- private static Dictionary<int, Task<string>> _responses = new Dictionary<int, Task<string>>();
- private static int _jobpointer = 0;
- // This 2 line are IMPORTANT and if changed will stop everything working
- // To send a string back to ARMA append to the output StringBuilder, ARMA outputSize limit applies!
- [DllExport("_RVExtension@12", CallingConvention = System.Runtime.InteropServices.CallingConvention.Winapi)]
- public static void RVExtension(StringBuilder output, int outputSize, [MarshalAs(UnmanagedType.LPStr)] string variable_input)
- {
- //Logger.Debug(String.Format("new request: {0}", variable_input), typeof(DllEntry));
- variable_input = variable_input.Replace("\\n", "\n");
- outputSize--;
- string[] commandpacket = variable_input.Split('\n');
- if (commandpacket.Length != 3)
- {
- output.Append("ERROR");
- Logger.Warn("commandpacket broken", typeof(DllEntry));
- return;
- }
- string response = "ERROR";
- switch (commandpacket[0].ToLower())
- {
- case "getasync":
- response = GetAsync(commandpacket[1]);
- break;
- case "response":
- response = GetResponse(commandpacket[1]);
- break;
- case "get":
- response = GetSync(commandpacket[1]);
- break;
- default:
- response = "ERROR";
- Logger.Warn("unknown command", typeof(DllEntry));
- break;
- }
- if (response.Length >= outputSize)
- {
- response = response.Substring(0, outputSize);
- Logger.Warn("output larget than max output, trimming..", typeof(DllEntry));
- }
- output.Append(response);
- //Logger.Debug(String.Format("response: {0}", response), typeof(DllEntry));
- }
- private static string GetResponse(string jobid)
- {
- int id = -1;
- try
- {
- id = Convert.ToInt16(jobid);
- }
- catch
- {
- // to prevent errors
- }
- if (id == -1)
- {
- return "ERROR";
- }
- if (!_responses.ContainsKey(id))
- {
- return "ERROR";
- }
- Task<string> variabletask = _responses[id];
- if (!variabletask.IsCompleted)
- {
- return "WAIT";
- }
- _responses.Remove(id);
- return variabletask.Result;
- }
- private static string GetAsync(string url)
- {
- int jobid = _jobpointer;
- _jobpointer++;
- // new Task(() =>
- // {
- Task<string> variabletask = new Task<string>(delegate { return Download(url); });
- variabletask.Start();
- _responses.Add(jobid, variabletask);
- // }).Start();
- return jobid.ToString();
- }
- private static string GetSync(string url)
- {
- return Download(url);
- }
- private static string Download(string url)
- {
- string s = "";
- using (WebClient client = new WebClient())
- {
- try
- {
- s = client.DownloadString(url);
- }
- catch (Exception ex)
- {
- s = "URLERROR";
- Logger.Error(String.Format("URLERROR: {0}", ex.Message), typeof(DllEntry));
- }
- }
- return s;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement