removed old code
This commit is contained in:
parent
eb24c9f94b
commit
092a6ad347
@ -35,7 +35,6 @@
|
||||
<Compile Include="CompressedArray.cs" />
|
||||
<Compile Include="Dtsod.cs" />
|
||||
<Compile Include="cs9somefix.cs" />
|
||||
<Compile Include="DtsodParser2.cs" />
|
||||
<Compile Include="Filework.cs" />
|
||||
<Compile Include="ColoredConsole.cs" />
|
||||
<Compile Include="PublicLog.cs" />
|
||||
|
||||
136
Dtsod.cs
136
Dtsod.cs
@ -18,7 +18,7 @@ namespace DTLib
|
||||
public Dtsod(string text)
|
||||
{
|
||||
Text = text;
|
||||
foreach (KeyValuePair<string, dynamic> pair in ParseNew(text))
|
||||
foreach (KeyValuePair<string, dynamic> pair in Parse(text))
|
||||
Add(pair.Key, pair.Value);
|
||||
}
|
||||
|
||||
@ -83,7 +83,7 @@ namespace DTLib
|
||||
Default
|
||||
}
|
||||
|
||||
Dictionary<string, dynamic> ParseNew(string text)
|
||||
Dictionary<string, dynamic> Parse(string text)
|
||||
{
|
||||
Dictionary<string, dynamic> parsed = new();
|
||||
int i = 0;
|
||||
@ -212,7 +212,7 @@ namespace DTLib
|
||||
if (debug) LogNoTime("y", text[i].ToString());
|
||||
type = ValueType.Complex;
|
||||
if (debug) LogNoTime("g", valueBuilder.ToString());
|
||||
return ParseNew(valueBuilder.ToString());
|
||||
return Parse(valueBuilder.ToString());
|
||||
}
|
||||
|
||||
dynamic ParseValueToRightType(string stringValue)
|
||||
@ -285,135 +285,5 @@ namespace DTLib
|
||||
throw new Exception("Dtsod.Parse.ReadValue error: end of text");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Dictionary<string, dynamic> ParseOld(string text)
|
||||
{
|
||||
Dictionary<string, dynamic> output = new();
|
||||
StringBuilder nameStrB = new();
|
||||
StringBuilder valStrB = new();
|
||||
dynamic value = null;
|
||||
bool readValue = false;
|
||||
bool readString = false;
|
||||
bool readListElem = false;
|
||||
bool isList = false;
|
||||
|
||||
dynamic StringToElse(string str)
|
||||
{
|
||||
if (readString) return str;
|
||||
// bool
|
||||
switch (str)
|
||||
{
|
||||
// предустановленные значения
|
||||
case "true": return true;
|
||||
case "false": return false;
|
||||
case "null": return null;
|
||||
default:
|
||||
// double
|
||||
if (str.Contains(".")) return SimpleConverter.ToDouble(str);
|
||||
// ushort, uint, ulong
|
||||
else if (str.Length > 2 && str[str.Length - 2] == 'u')
|
||||
return str[str.Length - 1] switch
|
||||
{
|
||||
's' => SimpleConverter.ToUShort(str.Remove(str.Length - 2)),
|
||||
'i' => SimpleConverter.ToUInt(str.Remove(str.Length - 2)),
|
||||
'l' => SimpleConverter.ToULong(str.Remove(str.Length - 2)),
|
||||
_ => throw new Exception($"ParseConfig() error: unknown data type <u{str[str.Length - 1]}>"),
|
||||
};
|
||||
// short, int, long
|
||||
else return str[str.Length - 1] switch
|
||||
{
|
||||
's' => SimpleConverter.ToShort(str.Remove(str.Length - 1)),
|
||||
'l' => SimpleConverter.ToLong(str.Remove(str.Length - 1)),
|
||||
_ => SimpleConverter.ToInt(str),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < text.Length; i++)
|
||||
{
|
||||
if (debug) LogNoTime(text[i].ToString());
|
||||
|
||||
void ReadString()
|
||||
{
|
||||
i++;
|
||||
while (text[i] != '"' || text[i - 1] == '\\')
|
||||
{
|
||||
if (debug) LogNoTime(text[i].ToString());
|
||||
valStrB.Append(text[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
switch (text[i])
|
||||
{
|
||||
case '{':
|
||||
i++;
|
||||
for (; text[i] != '}'; i++)
|
||||
{
|
||||
if (text[i] == '"') ReadString();
|
||||
else valStrB.Append(text[i]);
|
||||
}
|
||||
value = ParseOld(valStrB.ToString());
|
||||
valStrB.Clear();
|
||||
break;
|
||||
case '}':
|
||||
throw new Exception("ParseConfig() error: unexpected '}' at " + i + "char");
|
||||
case '"':
|
||||
readString = true;
|
||||
ReadString();
|
||||
break;
|
||||
case ':':
|
||||
readValue = true;
|
||||
break;
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
break;
|
||||
case '[':
|
||||
isList = true;
|
||||
value = new List<dynamic>();
|
||||
break;
|
||||
case ',':
|
||||
case ']':
|
||||
if (isList) value.Add(StringToElse(valStrB.ToString()));
|
||||
else throw new Exception($"unexpected <{text[i]}> at text[{i}]");
|
||||
valStrB.Clear();
|
||||
break;
|
||||
case ';':
|
||||
// конвертация value в нужный тип данных
|
||||
if (!isList && valStrB.Length > 0) value = StringToElse(valStrB.ToString());
|
||||
if (readListElem)
|
||||
{
|
||||
if (!output.ContainsKey(nameStrB.ToString())) output.Add(nameStrB.ToString(), new List<dynamic>());
|
||||
output[nameStrB.ToString()].Add(value);
|
||||
}
|
||||
else output.Add(nameStrB.ToString(), value);
|
||||
nameStrB.Clear();
|
||||
valStrB.Clear();
|
||||
value = null;
|
||||
readValue = false;
|
||||
readString = false;
|
||||
readListElem = false;
|
||||
isList = false;
|
||||
break;
|
||||
// коммент
|
||||
case '#':
|
||||
for (; i < text.Length && text[i] != '\n'; i++) ;
|
||||
break;
|
||||
// если $ перед названием параметра поставить, значение (value) добавится в лист с таким названием (nameStrB.ToString())
|
||||
case '$':
|
||||
if (nameStrB.ToString().Length != 0) throw new Exception("unexpected usage of '$' at char " + i.ToString());
|
||||
readListElem = true;
|
||||
break;
|
||||
default:
|
||||
if (readValue) valStrB.Append(text[i]);
|
||||
else nameStrB.Append(text[i]);
|
||||
break;
|
||||
};
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
207
DtsodParser2.cs
207
DtsodParser2.cs
@ -1,207 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DTLib
|
||||
{
|
||||
class DtsodParser2
|
||||
{
|
||||
|
||||
enum ValueType
|
||||
{
|
||||
List,
|
||||
Complex,
|
||||
String,
|
||||
Double,
|
||||
Long,
|
||||
Ulong,
|
||||
Short,
|
||||
Ushort,
|
||||
Int,
|
||||
Uint,
|
||||
Null,
|
||||
Boolean,
|
||||
Default
|
||||
}
|
||||
|
||||
Dictionary<string, dynamic> ParseNew(string text)
|
||||
{
|
||||
Dictionary<string, dynamic> parsed = new();
|
||||
int i = 0;
|
||||
for (; i < text.Length; i++) ReadName();
|
||||
return parsed;
|
||||
|
||||
void ReadName()
|
||||
{
|
||||
bool isListElem = false;
|
||||
|
||||
void ReadCommentLine()
|
||||
{
|
||||
for (; i < text.Length && text[i] != '\n'; i++) ;
|
||||
}
|
||||
|
||||
dynamic value = null;
|
||||
StringBuilder defaultNameBuilder = new();
|
||||
|
||||
for (; i < text.Length; i++)
|
||||
{
|
||||
switch (text[i])
|
||||
{
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
break;
|
||||
case ':':
|
||||
value = ReadValue();
|
||||
break;
|
||||
// строка, начинающаяся с # будет считаться комментом
|
||||
case '#':
|
||||
ReadCommentLine();
|
||||
break;
|
||||
case '}':
|
||||
throw new Exception("ParseConfig() error: unexpected '}' at " + i + "char");
|
||||
// если $ перед названием параметра поставить, значение value добавится в лист с названием name
|
||||
case '$':
|
||||
if (defaultNameBuilder.ToString().Length != 0) throw new Exception("unexpected usage of '$' at char " + i.ToString());
|
||||
isListElem = true;
|
||||
break;
|
||||
case ';':
|
||||
string name = defaultNameBuilder.ToString();
|
||||
if (isListElem)
|
||||
{
|
||||
if (!parsed.ContainsKey(name)) parsed.Add(name, new List<dynamic>());
|
||||
parsed[name].Add(value);
|
||||
}
|
||||
else parsed.Add(name, value);
|
||||
return;
|
||||
default:
|
||||
defaultNameBuilder.Append(text[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dynamic ReadValue()
|
||||
{
|
||||
ValueType type = ValueType.Default;
|
||||
|
||||
string ReadString()
|
||||
{
|
||||
i++;
|
||||
StringBuilder valueBuilder = new();
|
||||
for (; text[i] != '"' || text[i - 1] == '\\'; i++)
|
||||
valueBuilder.Append(text[i]);
|
||||
type = ValueType.String;
|
||||
return valueBuilder.ToString();
|
||||
}
|
||||
|
||||
List<string> ReadList()
|
||||
{
|
||||
List<string> output = new();
|
||||
StringBuilder valueBuilder = new();
|
||||
for (; text[i] != ']'; i++)
|
||||
{
|
||||
switch (text[i])
|
||||
{
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
break;
|
||||
case ',':
|
||||
output.Add(valueBuilder.ToString());
|
||||
break;
|
||||
default:
|
||||
valueBuilder.Append(text[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
type = ValueType.List;
|
||||
return output;
|
||||
}
|
||||
|
||||
Dictionary<string, dynamic> ReadComplex()
|
||||
{
|
||||
i++;
|
||||
StringBuilder valueBuilder = new();
|
||||
for (; text[i] != '}'; i++)
|
||||
{
|
||||
if (text[i] == '"') valueBuilder.Append(ReadString());
|
||||
else valueBuilder.Append(text[i]);
|
||||
}
|
||||
type = ValueType.Complex;
|
||||
return ParseNew(valueBuilder.ToString());
|
||||
}
|
||||
|
||||
dynamic value = null;
|
||||
StringBuilder defaultValueBuilder = new();
|
||||
for (; i < text.Length; i++)
|
||||
{
|
||||
switch (text[i])
|
||||
{
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
break;
|
||||
case '"':
|
||||
value = ReadString();
|
||||
break;
|
||||
case ';':
|
||||
if (type == ValueType.Default)
|
||||
{
|
||||
string valueString = defaultValueBuilder.ToString();
|
||||
type = valueString switch
|
||||
{
|
||||
"true" or "false" => ValueType.Boolean,
|
||||
"null" => ValueType.Null,
|
||||
_ when valueString.Contains('.') => ValueType.Null,
|
||||
_ when (valueString.Length > 2 && valueString[valueString.Length - 2] == 'u') => valueString[valueString.Length - 1] switch
|
||||
{
|
||||
's' => ValueType.Ushort,
|
||||
'i' => ValueType.Uint,
|
||||
'l' => ValueType.Ulong,
|
||||
_ => throw new Exception($"Dtsod.Parse.ReadValue() error: wrong type <u{valueString[valueString.Length - 1]}>")
|
||||
},
|
||||
_ => valueString[valueString.Length - 1] switch
|
||||
{
|
||||
's' => ValueType.Short,
|
||||
'l' => ValueType.Long,
|
||||
_ => ValueType.Int
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return type switch
|
||||
{
|
||||
ValueType.String or ValueType.List or ValueType.Complex => value,
|
||||
ValueType.Double => SimpleConverter.ToDouble(value),
|
||||
ValueType.Long => SimpleConverter.ToLong(value.Remove(value.Length - 1)),
|
||||
ValueType.Ulong => SimpleConverter.ToULong(value.Remove(value.Length - 2)),
|
||||
ValueType.Short => SimpleConverter.ToShort(value.Remove(value.Length - 1)),
|
||||
ValueType.Ushort => SimpleConverter.ToUShort(value.Remove(value.Length - 2)),
|
||||
ValueType.Int => SimpleConverter.ToInt(value),
|
||||
ValueType.Uint => SimpleConverter.ToUInt(value),
|
||||
ValueType.Boolean => SimpleConverter.ToBool(value),
|
||||
ValueType.Null => null,
|
||||
_ => throw new Exception($"Dtlib.Parse.ReadValue() error: can't convert value to type <{type}>")
|
||||
};
|
||||
case '[':
|
||||
value = ReadList();
|
||||
break;
|
||||
case '{':
|
||||
value = ReadComplex();
|
||||
break;
|
||||
default:
|
||||
defaultValueBuilder.Append(text[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw new Exception("Dtsod.Parse.ReadValue error: end of text");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
127
Network.cs
127
Network.cs
@ -55,133 +55,6 @@ namespace DTLib
|
||||
socket.Send(list.ToArray());
|
||||
}
|
||||
|
||||
/*
|
||||
// скачивает файл с помощью FSP протокола
|
||||
public static void FSP_Download(this Socket mainSocket, string filePath_server, string filePath_client)
|
||||
{
|
||||
Log("b", $"requesting file download: {filePath_server}\n");
|
||||
mainSocket.SendPackage("requesting file download".ToBytes());
|
||||
mainSocket.SendPackage(filePath_server.ToBytes());
|
||||
FSP_Download(mainSocket, filePath_client);
|
||||
}
|
||||
public static void FSP_Download(this Socket mainSocket, string filePath_client)
|
||||
{
|
||||
File.Create(filePath_client);
|
||||
using var fileStream = File.OpenWrite(filePath_client);
|
||||
var fileSize = mainSocket.GetPackage().ToStr().ToUInt();
|
||||
var hashstr = mainSocket.GetPackage().HashToString();
|
||||
mainSocket.SendPackage("ready".ToBytes());
|
||||
int packagesCount = 0;
|
||||
byte[] buffer = new byte[5120];
|
||||
int fullPackagesCount = SimpleConverter.Truncate(fileSize / buffer.Length);
|
||||
// рассчёт скорости
|
||||
int seconds = 0;
|
||||
var speedCounter = new Timer(true, 1000, () =>
|
||||
{
|
||||
seconds++;
|
||||
Log("c", $"speed= {packagesCount * buffer.Length / (seconds * 1000)} kb/s\n");
|
||||
});
|
||||
// получение файла
|
||||
for (; packagesCount < fullPackagesCount; packagesCount++)
|
||||
{
|
||||
buffer = mainSocket.GetPackage();
|
||||
fileStream.Write(buffer, 0, buffer.Length);
|
||||
fileStream.Flush();
|
||||
}
|
||||
speedCounter.Stop();
|
||||
// получение остатка
|
||||
if ((fileSize - fileStream.Position) > 0)
|
||||
{
|
||||
mainSocket.SendPackage("remain request".ToBytes());
|
||||
buffer = mainSocket.GetPackage();
|
||||
fileStream.Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
fileStream.Flush();
|
||||
fileStream.Close();
|
||||
Log(new string[] { "g", $" downloaded {packagesCount * 5120 + buffer.Length} of {fileSize} bytes\n" });
|
||||
|
||||
}
|
||||
|
||||
public static byte[] FSP_DownloadToMemory(this Socket mainSocket, string filePath_server)
|
||||
{
|
||||
Log("b", $"requesting file download: {filePath_server}\n");
|
||||
mainSocket.SendPackage("requesting file download".ToBytes());
|
||||
mainSocket.SendPackage(filePath_server.ToBytes());
|
||||
using var fileStream = new System.IO.MemoryStream();
|
||||
var fileSize = mainSocket.GetPackage().ToStr().ToUInt();
|
||||
var hashstr = mainSocket.GetPackage().HashToString();
|
||||
mainSocket.SendPackage("ready".ToBytes());
|
||||
int packagesCount = 0;
|
||||
byte[] buffer = new byte[5120];
|
||||
int fullPackagesCount = SimpleConverter.Truncate(fileSize / buffer.Length);
|
||||
// рассчёт скорости
|
||||
int seconds = 0;
|
||||
var speedCounter = new Timer(true, 1000, () =>
|
||||
{
|
||||
seconds++;
|
||||
Log("c", $"speed= {packagesCount * buffer.Length / (seconds * 1000)} kb/s\n");
|
||||
});
|
||||
// получение файла
|
||||
for (; packagesCount < fullPackagesCount; packagesCount++)
|
||||
{
|
||||
buffer = mainSocket.GetPackage();
|
||||
fileStream.Write(buffer, 0, buffer.Length);
|
||||
fileStream.Flush();
|
||||
}
|
||||
speedCounter.Stop();
|
||||
// получение остатка
|
||||
if ((fileSize - fileStream.Position) > 0)
|
||||
{
|
||||
mainSocket.SendPackage("remain request".ToBytes());
|
||||
buffer = mainSocket.GetPackage();
|
||||
fileStream.Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
byte[] output = fileStream.GetBuffer();
|
||||
fileStream.Close();
|
||||
Log(new string[] { "g", $" downloaded {packagesCount * 5120 + buffer.Length} of {fileSize} bytes\n" });
|
||||
return output;
|
||||
}
|
||||
|
||||
// отдаёт файл с помощью FSP протокола
|
||||
public static void FSP_Upload(this Socket mainSocket, string filePath)
|
||||
{
|
||||
Log("b", $"uploading file {filePath}\n");
|
||||
using var fileStream = File.OpenRead(filePath);
|
||||
var fileSize = File.GetSize(filePath);
|
||||
var fileHash = new Hasher().HashFile(filePath);
|
||||
mainSocket.SendPackage(fileSize.ToString().ToBytes());
|
||||
mainSocket.SendPackage(fileHash);
|
||||
if (mainSocket.GetPackage().ToStr() != "ready") throw new Exception("user socket isn't ready");
|
||||
byte[] buffer = new byte[5120];
|
||||
var hashstr = fileHash.HashToString();
|
||||
int packagesCount = 0;
|
||||
int seconds = 0;
|
||||
// рассчёт скорости
|
||||
var speedCounter = new Timer(true, 1000, () =>
|
||||
{
|
||||
seconds++;
|
||||
Log("c", $"speed= {packagesCount * buffer.Length / (seconds * 1000)} kb/s\n");
|
||||
});
|
||||
// отправка файла
|
||||
int fullPackagesCount = SimpleConverter.Truncate(fileSize / buffer.Length);
|
||||
for (; packagesCount < fullPackagesCount; packagesCount++)
|
||||
{
|
||||
fileStream.Read(buffer, 0, buffer.Length);
|
||||
mainSocket.SendPackage(buffer);
|
||||
}
|
||||
speedCounter.Stop();
|
||||
// досылка остатка
|
||||
if ((fileSize - fileStream.Position) > 0)
|
||||
{
|
||||
if (mainSocket.GetPackage().ToStr() != "remain request") throw new Exception("FSP_Upload() error: didn't get remain request");
|
||||
buffer = new byte[(fileSize - fileStream.Position).ToInt()];
|
||||
fileStream.Read(buffer, 0, buffer.Length);
|
||||
mainSocket.SendPackage(buffer);
|
||||
}
|
||||
fileStream.Close();
|
||||
Log(new string[] { "g", $" uploaded {packagesCount * 5120 + buffer.Length} of {fileSize} bytes\n" });
|
||||
}
|
||||
*/
|
||||
// получает с сайта публичный ip
|
||||
public static string GetPublicIP() => new WebClient().DownloadString("https://ipv4bot.whatismyipaddress.com/");
|
||||
|
||||
|
||||
@ -9,6 +9,5 @@
|
||||
// вот к этому объекту подключайте методы для вывода логов
|
||||
public static LogDelegate Log;
|
||||
public static LogDelegate LogNoTime;
|
||||
public static LogDelegate FSP_DownloadSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user