Jypeli  9
The simple game programming library
HTTP.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.IO;
5 using System.Net;
6 
7 namespace Jypeli
8 {
9  public abstract 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 }
Jypeli.StorageFile
Tiedosto.
Definition: StorageFile.cs:17
Jypeli.FileManager.AsyncTrigger.AsyncTrigger
AsyncTrigger(List< AsyncOperation > operations, TimeSpan timeout, Action endAction)
Definition: HTTP.cs:51
Jypeli.FileManager.AsyncTrigger.onCompleted
Action onCompleted
Definition: HTTP.cs:41
Jypeli.Game.Time
static Time Time
Peliaika. Sisältää tiedon siitä, kuinka kauan peliä on pelattu (Time.SinceStartOfGame) ja kuinka kaua...
Definition: Time.cs:25
Jypeli
Definition: Automobile.cs:5
Jypeli.FileManager.AsyncOperation.Result
IAsyncResult Result
Definition: HTTP.cs:14
Jypeli.FileManager.AsyncOperation
Definition: HTTP.cs:12
Jypeli.FileManager.AsyncTrigger.ops
List< AsyncOperation > ops
Definition: HTTP.cs:40
Jypeli.FileManager.TriggerOnComplete
void TriggerOnComplete(Action callback, params AsyncOperation[] actions)
Laukaisee aliohjelman kun annetut operaatiot on suoritettu.
Definition: HTTP.cs:204
Jypeli.FileManager.AsyncOperation.Request
HttpWebRequest Request
Definition: HTTP.cs:13
Jypeli.FileManager.Open
abstract StorageFile Open(string fileName, bool write)
Jypeli.FileManager.TriggerOnComplete
void TriggerOnComplete(Action callback, TimeSpan timeout, params AsyncOperation[] actions)
Laukaisee aliohjelman kun annetut operaatiot on suoritettu.
Definition: HTTP.cs:221
Jypeli.FileManager.DoWithURL
AsyncOperation DoWithURL(string url, TimeSpan timeout, Action< StorageFile > callback)
Avaa tiedoston netistä (lukua varten) ja tekee sillä jotain.
Definition: HTTP.cs:158
Jypeli.FileManager.AsyncOperation.Lifetime
TimeSpan Lifetime
Definition: HTTP.cs:15
Jypeli.FileManager.AsyncTrigger.failed
int failed
Definition: HTTP.cs:43
Jypeli.FileManager.AsyncOperation.Callback
Action< StorageFile > Callback
Definition: HTTP.cs:16
Jypeli.Time
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:14
Jypeli.FileManager.AsyncTrigger.triggered
int triggered
Definition: HTTP.cs:42
Jypeli.Time.Zero
static readonly Time Zero
Definition: Time.cs:15
Jypeli.FileManager.triggers
SynchronousList< AsyncTrigger > triggers
Definition: HTTP.cs:59
Jypeli.FileManager.AsyncTrigger.AsyncTrigger
AsyncTrigger(List< AsyncOperation > operations, Action endAction)
Definition: HTTP.cs:45
Jypeli.FileManager.Update
void Update(Time time)
Definition: HTTP.cs:66
Jypeli.FileManager.DoWithURL
AsyncOperation DoWithURL(string url, Action< StorageFile > callback)
Avaa tiedoston netistä (lukua varten) ja tekee sillä jotain.
Definition: HTTP.cs:132
Jypeli.FileManager.DoWithCallback
void DoWithCallback(IAsyncResult ar)
Definition: HTTP.cs:167
System
Definition: CFFauxAttributes.cs:29
Jypeli.FileManager.AsyncTrigger
Definition: HTTP.cs:39
Jypeli.FileManager.DoWith
AsyncOperation DoWith(string fileName, Action< StorageFile > callback)
Avaa tiedoston (lukua varten) ja tekee sillä jotain.
Definition: HTTP.cs:107
Jypeli.FileManager.IsUpdated
bool IsUpdated
Definition: HTTP.cs:62
Jypeli.SynchronousList
Synkroninen lista, eli lista joka päivittyy vasta kun sen Update-metodia kutsutaan....
Definition: SynchronousList.cs:15
Jypeli.Game
Definition: Content.cs:46
Jypeli.FileManager.AsyncOperation.IsCompleted
bool? IsCompleted
Definition: HTTP.cs:19
Jypeli.FileManager.AsyncOperation.AsyncOperation
AsyncOperation(HttpWebRequest req, Action< StorageFile > callback)
Definition: HTTP.cs:23
Jypeli.FileManager.AsyncOperation.AsyncOperation
AsyncOperation(HttpWebRequest req, Action< StorageFile > callback, TimeSpan timeout)
Definition: HTTP.cs:30