Jypeli  5
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 
156  public AsyncOperation DoWithURL( string url, TimeSpan timeout, Action<StorageFile> callback )
157  {
158  HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
159  AsyncOperation op = new AsyncOperation( request, callback, timeout );
160  IAsyncResult result = request.BeginGetResponse( DoWithCallback, op );
161  op.Result = result;
162  return op;
163  }
164 
165  private void DoWithCallback(IAsyncResult ar)
166  {
167  try
168  {
169  AsyncOperation op = (AsyncOperation)ar.AsyncState;
170  WebResponse response = op.Request.EndGetResponse( ar );
171  Stream resStream = response.GetResponseStream();
172  MemoryStream memStream = new MemoryStream();
173  resStream.CopyStreamTo( memStream );
174  resStream.Close();
175  response.Close();
176 
177  memStream.Seek( 0, SeekOrigin.Begin );
178  op.Callback( new StorageFile( op.Request.RequestUri.AbsoluteUri, memStream ) );
179  memStream.Close();
180 
181  foreach ( var trig in triggers.FindAll( t => t.ops.Any( o => o.Result == ar ) ) )
182  trig.triggered++;
183 
184  }
185  catch ( WebException we )
186  {
187  // Return if aborted
188  return;
189  }
190  }
191 
197  public void TriggerOnComplete( Action callback, params AsyncOperation[] actions )
198  {
199  triggers.Add( new AsyncTrigger( actions.ToList().FindAll( o => !o.IsCompleted ), callback ) );
200 
201 #if JYPELI
202  triggers.Update( Game.Time );
203 #else
204  triggers.Update( Time.Zero );
205 #endif
206  }
207 
214  public void TriggerOnComplete( Action callback, TimeSpan timeout, params AsyncOperation[] actions )
215  {
216  triggers.Add( new AsyncTrigger( actions.ToList().FindAll( o => !o.IsCompleted ), timeout, callback ) );
217 #if JYPELI
218  triggers.Update( Game.Time );
219 #else
220  triggers.Update( Time.Zero );
221 #endif
222  }
223  }
224 }
HttpWebRequest Request
Definition: HTTP.cs:13
Action< StorageFile > Callback
Definition: HTTP.cs:16
void TriggerOnComplete(Action callback, params AsyncOperation[] actions)
Laukaisee aliohjelman kun annetut operaatiot on suoritettu.
Definition: HTTP.cs:197
void Update(Time time)
Lisää ja poistaa jonossa olevat elementit sekä kutsuu niiden Update-metodia.
abstract StorageFile Open(string fileName, bool write)
AsyncOperation(HttpWebRequest req, Action< StorageFile > callback)
Definition: HTTP.cs:23
void TriggerOnComplete(Action callback, TimeSpan timeout, params AsyncOperation[] actions)
Laukaisee aliohjelman kun annetut operaatiot on suoritettu.
Definition: HTTP.cs:214
AsyncOperation DoWithURL(string url, Action< StorageFile > callback)
Avaa tiedoston netistä (lukua varten) ja tekee sillä jotain.
Definition: HTTP.cs:132
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:13
void Update(Time time)
Definition: HTTP.cs:66
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
AsyncOperation DoWithURL(string url, TimeSpan timeout, Action< StorageFile > callback)
Avaa tiedoston netistä (lukua varten) ja tekee sillä jotain.
Definition: HTTP.cs:156
int Count
Kuinka monta elementtiä listassa nyt on. Ei laske mukaan samalla päivityskierroksella tehtyjä muutoks...
static Time Time
Peliaika. Sisältää tiedon siitä, kuinka kauan peliä on pelattu (Time.SinceStartOfGame) ja kuinka kaua...
Definition: Game.cs:309
List< T > FindAll(Predicate< T > pred)
AsyncOperation(HttpWebRequest req, Action< StorageFile > callback, TimeSpan timeout)
Definition: HTTP.cs:30
AsyncOperation DoWith(string fileName, Action< StorageFile > callback)
Avaa tiedoston (lukua varten) ja tekee sillä jotain.
Definition: HTTP.cs:107
static readonly Time Zero
Definition: Time.cs:15
bool IsUpdated
Definition: HTTP.cs:62