Skip to content

Commit ef499c6

Browse files
committed
Core: refactor FileSessionStore class
* Move it into its own file. * Fix typo in var name "Exsist" * Move Session.ToBytes() and Session.FromBytes() methods to it.
1 parent ef89274 commit ef499c6

File tree

5 files changed

+125
-130
lines changed

5 files changed

+125
-130
lines changed

src/TgSharp.Core/FileSessionStore.cs

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.IO;
3+
4+
using TgSharp.TL;
5+
using TgSharp.Core.MTProto;
6+
using TgSharp.Core.MTProto.Crypto;
7+
8+
namespace TgSharp.Core
9+
{
10+
public class FileSessionStore : ISessionStore
11+
{
12+
private readonly DirectoryInfo basePath;
13+
14+
public FileSessionStore (DirectoryInfo basePath = null)
15+
{
16+
if (basePath != null && !basePath.Exists) {
17+
throw new ArgumentException ("basePath doesn't exist", nameof (basePath));
18+
}
19+
this.basePath = basePath;
20+
}
21+
22+
public void Save (Session session)
23+
{
24+
string sessionFileName = $"{session.SessionUserId}.dat";
25+
var sessionPath = basePath == null ? sessionFileName :
26+
Path.Combine (basePath.FullName, sessionFileName);
27+
28+
using (var stream = new FileStream (sessionPath, FileMode.OpenOrCreate)) {
29+
var result = ToBytes (session);
30+
stream.Write (result, 0, result.Length);
31+
}
32+
}
33+
34+
public Session Load (string sessionUserId)
35+
{
36+
string sessionFileName = $"{sessionUserId}.dat";
37+
var sessionPath = basePath == null ? sessionFileName :
38+
Path.Combine (basePath.FullName, sessionFileName);
39+
40+
if (!File.Exists (sessionPath))
41+
return null;
42+
43+
using (var stream = new FileStream (sessionPath, FileMode.Open)) {
44+
var buffer = new byte [2048];
45+
stream.Read (buffer, 0, 2048);
46+
47+
return FromBytes (buffer, this, sessionUserId);
48+
}
49+
}
50+
51+
public static Session FromBytes (byte [] buffer, ISessionStore store, string sessionUserId)
52+
{
53+
using (var stream = new MemoryStream (buffer))
54+
using (var reader = new BinaryReader (stream)) {
55+
var id = reader.ReadUInt64 ();
56+
var sequence = reader.ReadInt32 ();
57+
58+
// we do this in CI when running tests so that the they can always use a
59+
// higher sequence than previous run
60+
#if CI
61+
sequence = Session.CurrentTime();
62+
#endif
63+
64+
var salt = reader.ReadUInt64 ();
65+
var lastMessageId = reader.ReadInt64 ();
66+
var timeOffset = reader.ReadInt32 ();
67+
var serverAddress = Serializers.String.Read (reader);
68+
var port = reader.ReadInt32 ();
69+
70+
var doesAuthExist = reader.ReadInt32 () == 1;
71+
int sessionExpires = 0;
72+
TLUser TLUser = null;
73+
if (doesAuthExist) {
74+
sessionExpires = reader.ReadInt32 ();
75+
TLUser = (TLUser)ObjectUtils.DeserializeObject (reader);
76+
}
77+
78+
var authData = Serializers.Bytes.Read (reader);
79+
var defaultDataCenter = new DataCenter (serverAddress, port);
80+
81+
return new Session () {
82+
AuthKey = new AuthKey (authData),
83+
Id = id,
84+
Salt = salt,
85+
Sequence = sequence,
86+
LastMessageId = lastMessageId,
87+
TimeOffset = timeOffset,
88+
SessionExpires = sessionExpires,
89+
TLUser = TLUser,
90+
SessionUserId = sessionUserId,
91+
DataCenter = defaultDataCenter,
92+
};
93+
}
94+
}
95+
96+
internal byte [] ToBytes (Session session)
97+
{
98+
using (var stream = new MemoryStream ())
99+
using (var writer = new BinaryWriter (stream)) {
100+
writer.Write (session.Id);
101+
writer.Write (session.Sequence);
102+
writer.Write (session.Salt);
103+
writer.Write (session.LastMessageId);
104+
writer.Write (session.TimeOffset);
105+
Serializers.String.Write (writer, session.DataCenter.Address);
106+
writer.Write (session.DataCenter.Port);
107+
108+
if (session.TLUser != null) {
109+
writer.Write (1);
110+
writer.Write (session.SessionExpires);
111+
ObjectUtils.SerializeObject (session.TLUser, writer);
112+
} else {
113+
writer.Write (0);
114+
}
115+
116+
Serializers.Bytes.Write (writer, session.AuthKey.Data);
117+
118+
return stream.ToArray ();
119+
}
120+
}
121+
}
122+
}

src/TgSharp.Core/MTProto/Serializers.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System.Collections.Generic;
1+
22
using System.IO;
33
using System.Text;
44

55
namespace TgSharp.Core.MTProto
66
{
77
public class Serializers
88
{
9-
109
public static class Bytes
1110
{
1211
public static byte[] Read(BinaryReader binaryReader)

src/TgSharp.Core/Session.cs

+1-124
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,6 @@ public interface ISessionStore
1313
Session Load(string sessionUserId);
1414
}
1515

16-
public class FileSessionStore : ISessionStore
17-
{
18-
private readonly DirectoryInfo basePath;
19-
20-
public FileSessionStore(DirectoryInfo basePath = null)
21-
{
22-
if (basePath != null && !basePath.Exists)
23-
{
24-
throw new ArgumentException("basePath doesn't exist", nameof(basePath));
25-
}
26-
this.basePath = basePath;
27-
}
28-
29-
public void Save(Session session)
30-
{
31-
string sessionFileName = $"{session.SessionUserId}.dat";
32-
var sessionPath = basePath == null ? sessionFileName :
33-
Path.Combine(basePath.FullName, sessionFileName);
34-
35-
using (var stream = new FileStream(sessionPath, FileMode.OpenOrCreate))
36-
{
37-
var result = session.ToBytes();
38-
stream.Write(result, 0, result.Length);
39-
}
40-
}
41-
42-
public Session Load(string sessionUserId)
43-
{
44-
string sessionFileName = $"{sessionUserId}.dat";
45-
var sessionPath = basePath == null ? sessionFileName :
46-
Path.Combine(basePath.FullName, sessionFileName);
47-
48-
if (!File.Exists(sessionPath))
49-
return null;
50-
51-
using (var stream = new FileStream(sessionPath, FileMode.Open))
52-
{
53-
var buffer = new byte[2048];
54-
stream.Read(buffer, 0, 2048);
55-
56-
return Session.FromBytes(buffer, this, sessionUserId);
57-
}
58-
}
59-
}
60-
6116
public class FakeSessionStore : ISessionStore
6217
{
6318
public void Save(Session session)
@@ -104,7 +59,7 @@ public class Session
10459
= CurrentTime ();
10560

10661
// this is similar to the unixTime but rooted on the worst year of humanity instead of 1970
107-
private static int CurrentTime ()
62+
internal static int CurrentTime ()
10863
{
10964
return (int)DateTime.UtcNow.Subtract (new DateTime (2020, 1, 1)).TotalSeconds;
11065
}
@@ -126,84 +81,6 @@ public Session()
12681
random = new Random();
12782
}
12883

129-
public byte[] ToBytes()
130-
{
131-
using (var stream = new MemoryStream())
132-
using (var writer = new BinaryWriter(stream))
133-
{
134-
writer.Write(Id);
135-
writer.Write(Sequence);
136-
writer.Write(Salt);
137-
writer.Write(LastMessageId);
138-
writer.Write(TimeOffset);
139-
Serializers.String.Write(writer, DataCenter.Address);
140-
writer.Write(DataCenter.Port);
141-
142-
if (TLUser != null)
143-
{
144-
writer.Write(1);
145-
writer.Write(SessionExpires);
146-
ObjectUtils.SerializeObject(TLUser, writer);
147-
}
148-
else
149-
{
150-
writer.Write(0);
151-
}
152-
153-
Serializers.Bytes.Write(writer, AuthKey.Data);
154-
155-
return stream.ToArray();
156-
}
157-
}
158-
159-
public static Session FromBytes(byte[] buffer, ISessionStore store, string sessionUserId)
160-
{
161-
using (var stream = new MemoryStream(buffer))
162-
using (var reader = new BinaryReader(stream))
163-
{
164-
var id = reader.ReadUInt64();
165-
var sequence = reader.ReadInt32();
166-
167-
// we do this in CI when running tests so that the they can always use a
168-
// higher sequence than previous run
169-
#if CI
170-
sequence = CurrentTime();
171-
#endif
172-
173-
var salt = reader.ReadUInt64();
174-
var lastMessageId = reader.ReadInt64();
175-
var timeOffset = reader.ReadInt32();
176-
var serverAddress = Serializers.String.Read(reader);
177-
var port = reader.ReadInt32();
178-
179-
var isAuthExsist = reader.ReadInt32() == 1;
180-
int sessionExpires = 0;
181-
TLUser TLUser = null;
182-
if (isAuthExsist)
183-
{
184-
sessionExpires = reader.ReadInt32();
185-
TLUser = (TLUser)ObjectUtils.DeserializeObject(reader);
186-
}
187-
188-
var authData = Serializers.Bytes.Read(reader);
189-
var defaultDataCenter = new DataCenter (serverAddress, port);
190-
191-
return new Session()
192-
{
193-
AuthKey = new AuthKey(authData),
194-
Id = id,
195-
Salt = salt,
196-
Sequence = sequence,
197-
LastMessageId = lastMessageId,
198-
TimeOffset = timeOffset,
199-
SessionExpires = sessionExpires,
200-
TLUser = TLUser,
201-
SessionUserId = sessionUserId,
202-
DataCenter = defaultDataCenter,
203-
};
204-
}
205-
}
206-
20784
public long GetNewMessageId()
20885
{
20986
long time = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);

src/TgSharp.Core/TgSharp.Core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<Compile Include="TelegramClient.cs" />
8080
<Compile Include="Utils\Helpers.cs" />
8181
<Compile Include="DataCenter.cs" />
82+
<Compile Include="FileSessionStore.cs" />
8283
</ItemGroup>
8384
<ItemGroup>
8485
<None Include="packages.config" />

src/TgSharp.TL/ObjectDeserializer.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
73

84
namespace TgSharp.TL
95
{

0 commit comments

Comments
 (0)