Advertisement
infiSTAR23

URL_LOAD.dll from infiSTAR

May 25th, 2015
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. Well this is my C# URL_LOAD which is not even close to the "Not multitasking one" by KKID which was a C++ project..
  2.  
  3.  
  4. using RGiesecke.DllExport;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Net;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Linq;
  12.  
  13. namespace ARMAExtCS
  14. {
  15. public class DllEntry // This can be named anything you like
  16. {
  17. private static Dictionary<int, Task<string>> _responses = new Dictionary<int, Task<string>>();
  18. private static int _jobpointer = 0;
  19.  
  20. // This 2 line are IMPORTANT and if changed will stop everything working
  21. // To send a string back to ARMA append to the output StringBuilder, ARMA outputSize limit applies!
  22. [DllExport("_RVExtension@12", CallingConvention = System.Runtime.InteropServices.CallingConvention.Winapi)]
  23. public static void RVExtension(StringBuilder output, int outputSize, [MarshalAs(UnmanagedType.LPStr)] string variable_input)
  24. {
  25. //Logger.Debug(String.Format("new request: {0}", variable_input), typeof(DllEntry));
  26. variable_input = variable_input.Replace("\\n", "\n");
  27. outputSize--;
  28. string[] commandpacket = variable_input.Split('\n');
  29. if (commandpacket.Length != 3)
  30. {
  31. output.Append("ERROR");
  32. Logger.Warn("commandpacket broken", typeof(DllEntry));
  33. return;
  34. }
  35. string response = "ERROR";
  36. switch (commandpacket[0].ToLower())
  37. {
  38. case "getasync":
  39. response = GetAsync(commandpacket[1]);
  40. break;
  41. case "response":
  42. response = GetResponse(commandpacket[1]);
  43. break;
  44. case "get":
  45. response = GetSync(commandpacket[1]);
  46. break;
  47. default:
  48. response = "ERROR";
  49. Logger.Warn("unknown command", typeof(DllEntry));
  50. break;
  51. }
  52. if (response.Length >= outputSize)
  53. {
  54. response = response.Substring(0, outputSize);
  55. Logger.Warn("output larget than max output, trimming..", typeof(DllEntry));
  56. }
  57. output.Append(response);
  58. //Logger.Debug(String.Format("response: {0}", response), typeof(DllEntry));
  59. }
  60.  
  61. private static string GetResponse(string jobid)
  62. {
  63. int id = -1;
  64. try
  65. {
  66. id = Convert.ToInt16(jobid);
  67. }
  68. catch
  69. {
  70. // to prevent errors
  71. }
  72. if (id == -1)
  73. {
  74. return "ERROR";
  75. }
  76. if (!_responses.ContainsKey(id))
  77. {
  78. return "ERROR";
  79. }
  80. Task<string> variabletask = _responses[id];
  81. if (!variabletask.IsCompleted)
  82. {
  83. return "WAIT";
  84. }
  85. _responses.Remove(id);
  86. return variabletask.Result;
  87. }
  88.  
  89. private static string GetAsync(string url)
  90. {
  91. int jobid = _jobpointer;
  92. _jobpointer++;
  93. // new Task(() =>
  94. // {
  95. Task<string> variabletask = new Task<string>(delegate { return Download(url); });
  96. variabletask.Start();
  97. _responses.Add(jobid, variabletask);
  98. // }).Start();
  99. return jobid.ToString();
  100. }
  101.  
  102. private static string GetSync(string url)
  103. {
  104. return Download(url);
  105. }
  106.  
  107. private static string Download(string url)
  108. {
  109. string s = "";
  110. using (WebClient client = new WebClient())
  111. {
  112. try
  113. {
  114. s = client.DownloadString(url);
  115. }
  116. catch (Exception ex)
  117. {
  118. s = "URLERROR";
  119. Logger.Error(String.Format("URLERROR: {0}", ex.Message), typeof(DllEntry));
  120. }
  121. }
  122. return s;
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement