changes in StringConverter
This commit is contained in:
parent
f6d045ae2d
commit
20f2c7c7e7
@ -23,8 +23,8 @@ public class FSP
|
||||
lock (MainSocket)
|
||||
{
|
||||
Debug("b", $"requesting file download: {filePath_server}");
|
||||
MainSocket.SendPackage("requesting file download".ToBytes());
|
||||
MainSocket.SendPackage(filePath_server.ToBytes());
|
||||
MainSocket.SendPackage("requesting file download".ToBytes(StringConverter.UTF8));
|
||||
MainSocket.SendPackage(filePath_server.ToBytes(StringConverter.UTF8));
|
||||
}
|
||||
DownloadFile(filePath_client);
|
||||
}
|
||||
@ -44,8 +44,8 @@ public class FSP
|
||||
lock (MainSocket)
|
||||
{
|
||||
Debug("b", $"requesting file download: {filePath_server}");
|
||||
MainSocket.SendPackage("requesting file download".ToBytes());
|
||||
MainSocket.SendPackage(filePath_server.ToBytes());
|
||||
MainSocket.SendPackage("requesting file download".ToBytes(StringConverter.UTF8));
|
||||
MainSocket.SendPackage(filePath_server.ToBytes(StringConverter.UTF8));
|
||||
}
|
||||
return DownloadFileToMemory();
|
||||
}
|
||||
@ -65,8 +65,8 @@ public class FSP
|
||||
lock (MainSocket)
|
||||
{
|
||||
BytesDownloaded = 0;
|
||||
Filesize = MainSocket.GetPackage().BytesToString().ToUInt();
|
||||
MainSocket.SendPackage("ready".ToBytes());
|
||||
Filesize = MainSocket.GetPackage().BytesToString(StringConverter.UTF8).ToUInt();
|
||||
MainSocket.SendPackage("ready".ToBytes(StringConverter.UTF8));
|
||||
int packagesCount = 0;
|
||||
byte[] buffer = new byte[5120];
|
||||
int fullPackagesCount = (Filesize / buffer.Length).Truncate();
|
||||
@ -89,7 +89,7 @@ public class FSP
|
||||
// получение остатка
|
||||
if ((Filesize - fileStream.Position) > 0)
|
||||
{
|
||||
MainSocket.SendPackage("remain request".ToBytes());
|
||||
MainSocket.SendPackage("remain request".ToBytes(StringConverter.UTF8));
|
||||
buffer = MainSocket.GetPackage();
|
||||
BytesDownloaded += (uint)buffer.Length;
|
||||
fileStream.Write(buffer, 0, buffer.Length);
|
||||
@ -109,7 +109,7 @@ public class FSP
|
||||
Filesize = File.GetSize(filePath).ToUInt();
|
||||
lock (MainSocket)
|
||||
{
|
||||
MainSocket.SendPackage(Filesize.ToString().ToBytes());
|
||||
MainSocket.SendPackage(Filesize.ToString().ToBytes(StringConverter.UTF8));
|
||||
MainSocket.GetAnswer("ready");
|
||||
byte[] buffer = new byte[5120];
|
||||
int packagesCount = 0;
|
||||
@ -142,7 +142,7 @@ public class FSP
|
||||
if (!dirOnServer.EndsWith(Путь.Разд))
|
||||
dirOnServer += Путь.Разд;
|
||||
Debug("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">");
|
||||
var manifest = new DtsodV23(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString());
|
||||
var manifest = new DtsodV23(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString(StringConverter.UTF8));
|
||||
Debug("g", $"found {manifest.Values.Count} files in manifest");
|
||||
var hasher = new Hasher();
|
||||
foreach (string fileOnServer in manifest.Keys)
|
||||
|
||||
@ -37,19 +37,19 @@ public static class Package
|
||||
if (data.Length == 0)
|
||||
throw new Exception($"SendPackage() error: package has zero size");
|
||||
var list = new List<byte>();
|
||||
byte[] packageSize = data.Length.ToBytes();
|
||||
byte[] packageSize = data.Length.IntToBytes();
|
||||
if (packageSize.Length == 1)
|
||||
list.Add(0);
|
||||
list.AddRange(packageSize);
|
||||
list.AddRange(data);
|
||||
socket.Send(list.ToArray());
|
||||
}
|
||||
public static void SendPackage(this Socket socket, string data) => SendPackage(socket, data.ToBytes());
|
||||
public static void SendPackage(this Socket socket, string data) => SendPackage(socket, data.ToBytes(StringConverter.UTF8));
|
||||
|
||||
// получает пакет и выбрасывает исключение, если пакет не соответствует образцу
|
||||
public static void GetAnswer(this Socket socket, string answer)
|
||||
{
|
||||
string rec = socket.GetPackage().BytesToString();
|
||||
string rec = socket.GetPackage().BytesToString(StringConverter.UTF8);
|
||||
if (rec != answer)
|
||||
throw new Exception($"GetAnswer() error: invalid answer: <{rec}>");
|
||||
}
|
||||
@ -59,5 +59,5 @@ public static class Package
|
||||
socket.SendPackage(request);
|
||||
return socket.GetPackage();
|
||||
}
|
||||
public static byte[] RequestPackage(this Socket socket, string request) => socket.RequestPackage(request.ToBytes());
|
||||
public static byte[] RequestPackage(this Socket socket, string request) => socket.RequestPackage(request.ToBytes(StringConverter.UTF8));
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ public static class BaseConverter
|
||||
return output;
|
||||
}
|
||||
|
||||
public static byte[] ToBytes(this int num)
|
||||
public static byte[] IntToBytes(this int num)
|
||||
{
|
||||
List<byte> output = new();
|
||||
while (num != 0)
|
||||
|
||||
@ -2,11 +2,10 @@
|
||||
|
||||
public static class StringConverter
|
||||
{
|
||||
public static ASCIIEncoding ASCII = new ASCIIEncoding();
|
||||
public static Encoding UTF8 = new UTF8Encoding(false);
|
||||
public static Encoding UTF8BOM = new UTF8Encoding(true);
|
||||
public static byte[] ToBytes(this string str) => UTF8.GetBytes(str);
|
||||
public static string BytesToString(this byte[] bytes) => UTF8.GetString(bytes);
|
||||
public static byte[] ToBytes(this string str, Encoding encoding) => encoding.GetBytes(str);
|
||||
public static string BytesToString(this byte[] bytes, Encoding encoding) => encoding.GetString(bytes);
|
||||
|
||||
// хеш в виде массива байт в строку (хеш изначально не в кодировке UTF8, так что метод выше не работает с ним)
|
||||
public static string HashToString(this byte[] hash)
|
||||
@ -51,6 +50,11 @@ public static class StringConverter
|
||||
builder.Append(parts[i]);
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
// String.Join(string...) does some low-level memory manipulations, that are faster than StringBuilder
|
||||
public static string MergeToString(string part0, params string[] parts)
|
||||
=>string.Join(part0, parts);
|
||||
|
||||
public static string MergeToString<T>(this IEnumerable<T> collection, string separator)
|
||||
{
|
||||
StringBuilder builder = new();
|
||||
|
||||
@ -6,7 +6,7 @@ public static class Unmanaged
|
||||
{
|
||||
public static unsafe IntPtr StringToHGlobalUTF8(this string s)
|
||||
{
|
||||
byte[] buf = s.ToBytes();
|
||||
byte[] buf = s.ToBytes(StringConverter.UTF8);
|
||||
int bl = buf.Length;
|
||||
byte* ptr=(byte*)Marshal.AllocHGlobal(bl + 1);
|
||||
for (int i = 0; i < bl; i++)
|
||||
|
||||
@ -39,7 +39,7 @@ public static class File
|
||||
return output;
|
||||
}
|
||||
|
||||
public static string ReadAllText(string file) => ReadAllBytes(file).BytesToString();
|
||||
public static string ReadAllText(string file) => ReadAllBytes(file).BytesToString(StringConverter.UTF8);
|
||||
|
||||
public static void WriteAllBytes(string file, byte[] content)
|
||||
{
|
||||
@ -48,7 +48,7 @@ public static class File
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
public static void WriteAllText(string file, string content) => WriteAllBytes(file, content.ToBytes());
|
||||
public static void WriteAllText(string file, string content) => WriteAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
||||
|
||||
public static void AppendAllBytes(string file, byte[] content)
|
||||
{
|
||||
@ -57,7 +57,7 @@ public static class File
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
public static void AppendAllText(string file, string content) => AppendAllBytes(file, content.ToBytes());
|
||||
public static void AppendAllText(string file, string content) => AppendAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
||||
|
||||
public static System.IO.FileStream OpenRead(string file) =>
|
||||
Exists(file)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user