|
@@ -1,8 +1,17 @@
|
|
using System.IO.Compression;
|
|
using System.IO.Compression;
|
|
using System.Net;
|
|
using System.Net;
|
|
|
|
+using System.Net.Security;
|
|
using InABox.Core;
|
|
using InABox.Core;
|
|
using InABox.WebSocket.Shared;
|
|
using InABox.WebSocket.Shared;
|
|
using RestSharp;
|
|
using RestSharp;
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.IO;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Net.Http;
|
|
|
|
+using System.Threading;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+
|
|
|
|
|
|
namespace InABox.Clients
|
|
namespace InABox.Clients
|
|
{
|
|
{
|
|
@@ -12,6 +21,7 @@ namespace InABox.Clients
|
|
private string _server;
|
|
private string _server;
|
|
private bool _compression;
|
|
private bool _compression;
|
|
private BinarySerializationSettings _binarysettings;
|
|
private BinarySerializationSettings _binarysettings;
|
|
|
|
+ private static RestClient cli = null;
|
|
|
|
|
|
public RestClient(string server, bool simpleencryption, bool compression, BinarySerializationSettings binarySerializationSettings)
|
|
public RestClient(string server, bool simpleencryption, bool compression, BinarySerializationSettings binarySerializationSettings)
|
|
{
|
|
{
|
|
@@ -24,11 +34,28 @@ namespace InABox.Clients
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- public RestClient(string server, bool simpleencryption, bool compression) : this(server, simpleencryption, compression, BinarySerializationSettings.Latest)
|
|
|
|
|
|
+ public static class StaticRestClients
|
|
|
|
+ {
|
|
|
|
+ public static Dictionary<string, RestClient> Clients = new Dictionary<string, RestClient>();
|
|
|
|
+
|
|
|
|
+ public static void SetClient(string url)
|
|
|
|
+ {
|
|
|
|
+ var uri = new Uri(url);
|
|
|
|
+ if (Clients.ContainsKey(url))
|
|
|
|
+ cli = Clients[url];
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ cli = new RestClient(uri);
|
|
|
|
+ Clients.Add(url, cli);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public RestClient(string server, bool simpleencryption, bool compression) : this(server, simpleencryption, false, BinarySerializationSettings.Latest)
|
|
{
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
- public RestClient(string server, bool simpleencryption) : this(server, simpleencryption, true)
|
|
|
|
|
|
+ public RestClient(string server, bool simpleencryption) : this(server, simpleencryption, false)
|
|
{
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
@@ -170,8 +197,9 @@ namespace InABox.Clients
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- var uri = new Uri(url);
|
|
|
|
- var cli = new RestClient(uri);
|
|
|
|
|
|
+ if (cli == null)
|
|
|
|
+ StaticRestClients.SetClient(url);
|
|
|
|
+
|
|
var cmd = string.Format(
|
|
var cmd = string.Format(
|
|
"{0}{1}?format={2}&responseFormat={3}&serializationVersion={4}",
|
|
"{0}{1}?format={2}&responseFormat={3}&serializationVersion={4}",
|
|
Action,
|
|
Action,
|
|
@@ -180,35 +208,35 @@ namespace InABox.Clients
|
|
responseFormat,
|
|
responseFormat,
|
|
_binarysettings.Version
|
|
_binarysettings.Version
|
|
);
|
|
);
|
|
- var req = new RestRequest(cmd, Method.POST)
|
|
|
|
|
|
+ var req = new RestRequest(cmd, Method.Post)
|
|
{
|
|
{
|
|
Timeout = Timeout.Milliseconds,
|
|
Timeout = Timeout.Milliseconds,
|
|
};
|
|
};
|
|
//Log(" * {0}{1}() Creating Uri, Client and RestRequest took {2}ms", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds);
|
|
//Log(" * {0}{1}() Creating Uri, Client and RestRequest took {2}ms", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds);
|
|
//sw.Restart();
|
|
//sw.Restart();
|
|
|
|
|
|
- req.AdvancedResponseWriter = (stream, response) =>
|
|
|
|
- {
|
|
|
|
- //Log(" * {0}{1}() Response from Server took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
|
|
|
|
- //length = response.ContentLength;
|
|
|
|
- //sw.Restart();
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- if (responseFormat == SerializationFormat.Binary && typeof(TResponse).HasInterface<ISerializeBinary>())
|
|
|
|
- {
|
|
|
|
- result = (TResponse)Serialization.ReadBinary(typeof(TResponse), stream, _binarysettings);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- result = Serialization.Deserialize<TResponse>(stream, true);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- catch (Exception e)
|
|
|
|
- {
|
|
|
|
- Logger.Send(LogType.Information, "", $"Error deserializing response: {e.Message}");
|
|
|
|
- }
|
|
|
|
- //Log(" * {0}{1}() Deserializing Stream took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
|
|
|
|
- };
|
|
|
|
|
|
+ //req.AdvancedResponseWriter = (stream, response) =>
|
|
|
|
+ //{
|
|
|
|
+ // //Log(" * {0}{1}() Response from Server took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
|
|
|
|
+ // //length = response.ContentLength;
|
|
|
|
+ // //sw.Restart();
|
|
|
|
+ // try
|
|
|
|
+ // {
|
|
|
|
+ // if (responseFormat == SerializationFormat.Binary && typeof(TResponse).HasInterface<ISerializeBinary>())
|
|
|
|
+ // {
|
|
|
|
+ // result = (TResponse)Serialization.ReadBinary(typeof(TResponse), stream, _binarysettings);
|
|
|
|
+ // }
|
|
|
|
+ // else
|
|
|
|
+ // {
|
|
|
|
+ // result = Serialization.Deserialize<TResponse>(stream, true);
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+ // catch (Exception e)
|
|
|
|
+ // {
|
|
|
|
+ // Logger.Send(LogType.Information, "", $"Error deserializing response: {e.Message}");
|
|
|
|
+ // }
|
|
|
|
+ // //Log(" * {0}{1}() Deserializing Stream took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
|
|
|
|
+ //};
|
|
|
|
|
|
if(requestFormat == SerializationFormat.Binary && request is ISerializeBinary binary)
|
|
if(requestFormat == SerializationFormat.Binary && request is ISerializeBinary binary)
|
|
{
|
|
{
|
|
@@ -221,7 +249,7 @@ namespace InABox.Clients
|
|
{
|
|
{
|
|
var json = Serialization.Serialize(request);
|
|
var json = Serialization.Serialize(request);
|
|
|
|
|
|
- req.AddOrUpdateParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
|
|
|
|
|
|
+ req.AddOrUpdateParameter("application/json", json, ParameterType.RequestBody);
|
|
req.RequestFormat = DataFormat.Json;
|
|
req.RequestFormat = DataFormat.Json;
|
|
}
|
|
}
|
|
try
|
|
try
|
|
@@ -293,7 +321,6 @@ namespace InABox.Clients
|
|
}
|
|
}
|
|
|
|
|
|
req = null;
|
|
req = null;
|
|
- cli = null;
|
|
|
|
//double elapsed = (DateTime.Now - now).TotalMilliseconds;
|
|
//double elapsed = (DateTime.Now - now).TotalMilliseconds;
|
|
//Log(" * {0}{1}() completed in {2:F0}ms", Action, typeof(TEntity).Name, elapsed);
|
|
//Log(" * {0}{1}() completed in {2:F0}ms", Action, typeof(TEntity).Name, elapsed);
|
|
return result;
|
|
return result;
|
|
@@ -541,7 +568,7 @@ namespace InABox.Clients
|
|
protected override bool DoPing()
|
|
protected override bool DoPing()
|
|
{
|
|
{
|
|
var cli = new RestClient(new Uri(RestClientCache.URL(_server)));
|
|
var cli = new RestClient(new Uri(RestClientCache.URL(_server)));
|
|
- var req = new RestRequest("/info", Method.GET) { Timeout = 20000 };
|
|
|
|
|
|
+ var req = new RestRequest("/info", Method.Get) { Timeout = 20000 };
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
@@ -566,7 +593,7 @@ namespace InABox.Clients
|
|
|
|
|
|
var uri = new Uri(url);
|
|
var uri = new Uri(url);
|
|
var cli = new RestClient(uri);
|
|
var cli = new RestClient(uri);
|
|
- var req = new RestRequest("/classes", Method.GET) { Timeout = 20000 };
|
|
|
|
|
|
+ var req = new RestRequest("/classes", Method.Get) { Timeout = 20000 };
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|