@@ -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 ) ;
0 commit comments