diff --git a/DTLib.csproj b/DTLib.csproj
index 572c5c2..7693215 100644
--- a/DTLib.csproj
+++ b/DTLib.csproj
@@ -35,7 +35,6 @@
-
diff --git a/Dtsod.cs b/Dtsod.cs
index f2c8865..bcdbfdd 100644
--- a/Dtsod.cs
+++ b/Dtsod.cs
@@ -18,7 +18,7 @@ namespace DTLib
public Dtsod(string text)
{
Text = text;
- foreach (KeyValuePair pair in ParseNew(text))
+ foreach (KeyValuePair pair in Parse(text))
Add(pair.Key, pair.Value);
}
@@ -83,7 +83,7 @@ namespace DTLib
Default
}
- Dictionary ParseNew(string text)
+ Dictionary Parse(string text)
{
Dictionary 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 ParseOld(string text)
- {
- Dictionary 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 "),
- };
- // 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();
- 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());
- 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;
- }
}
}
diff --git a/DtsodParser2.cs b/DtsodParser2.cs
deleted file mode 100644
index b2ade7e..0000000
--- a/DtsodParser2.cs
+++ /dev/null
@@ -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 ParseNew(string text)
- {
- Dictionary 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());
- 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 ReadList()
- {
- List 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 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 ")
- },
- _ => 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");
- }
- }
- }
-}
diff --git a/Network.cs b/Network.cs
index 7f53342..c74389a 100644
--- a/Network.cs
+++ b/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/");
@@ -338,7 +211,7 @@ namespace DTLib
var hasher = new Hasher();
foreach (string fileOnServer in manifest.Keys)
{
- string fileOnClient = dirOnClient+fileOnServer;
+ string fileOnClient = dirOnClient + fileOnServer;
if (debug) Log("b", "file <", "c", fileOnClient, "b", ">... ");
if (!File.Exists(fileOnClient))
{
diff --git a/PublicLog.cs b/PublicLog.cs
index aa08e32..c541629 100644
--- a/PublicLog.cs
+++ b/PublicLog.cs
@@ -9,6 +9,5 @@
// вот к этому объекту подключайте методы для вывода логов
public static LogDelegate Log;
public static LogDelegate LogNoTime;
- public static LogDelegate FSP_DownloadSpeed;
}
}