Jypeli 10
The simple game programming library
HTTP.cs
Siirry tämän tiedoston dokumentaatioon.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.IO;
5using System.Net;
6
7namespace Jypeli
8{
9 public partial class FileManager : Updatable
10 {
11 public class AsyncOperation
12 {
13 public HttpWebRequest Request;
14 public IAsyncResult Result;
15 public TimeSpan Lifetime;
16 public Action<StorageFile> Callback;
17
18 public bool IsCompleted
19 {
20 get { return Result == null ? true : Result.IsCompleted; }
21 }
22
23 public AsyncOperation( HttpWebRequest req, Action<StorageFile> callback )
24 {
25 this.Request = req;
26 this.Callback = callback;
27 this.Lifetime = TimeSpan.FromSeconds( 15 );
28 }
29
30 public AsyncOperation( HttpWebRequest req, Action<StorageFile> callback, TimeSpan timeout )
31 {
32 this.Request = req;
33 this.Callback = callback;
34 this.Lifetime = timeout;
35 }
36 }
37
38 private class AsyncTrigger
39 {
40 public List<AsyncOperation> ops;
41 public Action onCompleted;
42 public int triggered = 0;
43 public int failed = 0;
44
45 public AsyncTrigger( List<AsyncOperation> operations, Action endAction )
46 {
47 this.ops = operations;
48 this.onCompleted = endAction;
49 }
50
51 public AsyncTrigger( List<AsyncOperation> operations, TimeSpan timeout, Action endAction )
52 {
53 this.ops = operations;
54 this.onCompleted = endAction;
55 foreach ( var op in ops ) op.Lifetime = timeout;
56 }
57 }
58
60
61 public bool IsUpdated
62 {
63 get { return triggers.Count > 0; }
64 }
65
66 public void Update( Time time )
67 {
68 triggers.Update( time );
69
70 foreach ( var trig in triggers )
71 {
72 foreach ( var op in trig.ops.FindAll( o => !o.IsCompleted ) )
73 {
74 op.Lifetime -= time.SinceLastUpdate;
75
76 if ( op.Lifetime.TotalSeconds <= 0 )
77 {
78 //Game.Instance.MessageDisplay.Add( "!!ABORT" );
79 op.Request.Abort();
80 trig.failed++;
81 }
82 }
83
84 if ( trig.triggered + trig.failed >= trig.ops.Count )
85 {
86 triggers.Remove( trig );
87 trig.onCompleted();
88 }
89 }
90 }
91
107 public AsyncOperation DoWith( string fileName, Action<StorageFile> callback )
108 {
109 using ( var f = Open( fileName, false ) )
110 {
111 callback( f );
112 }
113
114 return new AsyncOperation( null, callback );
115 }
116
132 public AsyncOperation DoWithURL( string url, Action<StorageFile> callback )
133 {
134 HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
135 AsyncOperation op = new AsyncOperation( request, callback );
136 IAsyncResult result = request.BeginGetResponse( DoWithCallback, op );
137 op.Result = result;
138 return op;
139 }
140
158 public AsyncOperation DoWithURL( string url, TimeSpan timeout, Action<StorageFile> callback )
159 {
160 HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
161 AsyncOperation op = new AsyncOperation( request, callback, timeout );
162 IAsyncResult result = request.BeginGetResponse( DoWithCallback, op );
163 op.Result = result;
164 return op;
165 }
166
167 private void DoWithCallback(IAsyncResult ar)
168 {
169 try
170 {
171 AsyncOperation op = (AsyncOperation)ar.AsyncState;
172 WebResponse response = op.Request.EndGetResponse( ar );
173 Stream resStream = response.GetResponseStream();
174 MemoryStream memStream = new MemoryStream();
175 resStream.CopyStreamTo( memStream );
176 resStream.Dispose();
177
178#if WINDOWS_STOREAPP
179 response.Dispose();
180#else
181 response.Close();
182#endif
183
184 memStream.Seek( 0, SeekOrigin.Begin );
185 op.Callback( new StorageFile( op.Request.RequestUri.AbsoluteUri, memStream ) );
186 memStream.Dispose();
187
188 foreach ( var trig in triggers.FindAll( t => t.ops.Any( o => o.Result == ar ) ) )
189 trig.triggered++;
190
191 }
192 catch ( WebException )
193 {
194 // Return if aborted
195 return;
196 }
197 }
198
204 public void TriggerOnComplete( Action callback, params AsyncOperation[] actions )
205 {
206 triggers.Add( new AsyncTrigger( actions.ToList().FindAll( o => !o.IsCompleted ), callback ) );
207
208#if JYPELI
209 triggers.Update( Game.Time );
210#else
211 triggers.Update( Time.Zero );
212#endif
213 }
214
221 public void TriggerOnComplete( Action callback, TimeSpan timeout, params AsyncOperation[] actions )
222 {
223 triggers.Add( new AsyncTrigger( actions.ToList().FindAll( o => !o.IsCompleted ), timeout, callback ) );
224#if JYPELI
225 triggers.Update( Game.Time );
226#else
227 triggers.Update( Time.Zero );
228#endif
229 }
230 }
231}
AsyncOperation(HttpWebRequest req, Action< StorageFile > callback, TimeSpan timeout)
Definition: HTTP.cs:30
HttpWebRequest Request
Definition: HTTP.cs:13
Action< StorageFile > Callback
Definition: HTTP.cs:16
AsyncOperation(HttpWebRequest req, Action< StorageFile > callback)
Definition: HTTP.cs:23
List< AsyncOperation > ops
Definition: HTTP.cs:40
AsyncTrigger(List< AsyncOperation > operations, Action endAction)
Definition: HTTP.cs:45
AsyncTrigger(List< AsyncOperation > operations, TimeSpan timeout, Action endAction)
Definition: HTTP.cs:51
void DoWithCallback(IAsyncResult ar)
Definition: HTTP.cs:167
void TriggerOnComplete(Action callback, params AsyncOperation[] actions)
Laukaisee aliohjelman kun annetut operaatiot on suoritettu.
Definition: HTTP.cs:204
void Update(Time time)
Päivitysfunktio
Definition: HTTP.cs:66
AsyncOperation DoWithURL(string url, Action< StorageFile > callback)
Avaa tiedoston netistä (lukua varten) ja tekee sillä jotain.
Definition: HTTP.cs:132
void TriggerOnComplete(Action callback, TimeSpan timeout, params AsyncOperation[] actions)
Laukaisee aliohjelman kun annetut operaatiot on suoritettu.
Definition: HTTP.cs:221
SynchronousList< AsyncTrigger > triggers
Definition: HTTP.cs:59
virtual StorageFile Open(string fileName, bool write)
Avaa tiedoston.
Definition: Files.cs:36
bool IsUpdated
Definition: HTTP.cs:62
AsyncOperation DoWithURL(string url, TimeSpan timeout, Action< StorageFile > callback)
Avaa tiedoston netistä (lukua varten) ja tekee sillä jotain.
Definition: HTTP.cs:158
AsyncOperation DoWith(string fileName, Action< StorageFile > callback)
Avaa tiedoston (lukua varten) ja tekee sillä jotain.
Definition: HTTP.cs:107
static Time Time
Peliaika. Sisältää tiedon siitä, kuinka kauan peliä on pelattu (Time.SinceStartOfGame) ja kuinka kaua...
Definition: Time.cs:25
Synkroninen lista, eli lista joka päivittyy vasta kun sen Update-metodia kutsutaan....
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:14
static readonly Time Zero
Nolla-aika
Definition: Time.cs:18