fork download
  1. /* SCE CONFIDENTIAL
  2.  * PlayStation(R)Suite SDK 0.98.2
  3.  * Copyright (C) 2012 Sony Computer Entertainment Inc.
  4.  * All Rights Reserved.
  5.  */
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. using System.IO;
  10. using System.Net;
  11. using System.Text;
  12. using System.Diagnostics;
  13. using Sce.PlayStation.Core.Graphics;
  14. using Sce.PlayStation.Core.Environment;
  15. using Sce.PlayStation.Core.Input;
  16. using Sce.PlayStation.Core.Imaging;
  17.  
  18. namespace Sample
  19. {
  20.  
  21. /**
  22.  * HttpSample
  23.  */
  24. public static class HttpSample
  25. {
  26. private static GraphicsContext graphics;
  27. private static SampleButton connectButton;
  28. private static SampleSprite httpText = null;
  29.  
  30. static SampleButton tekitouBtn;
  31. static bool buttonState = false;
  32.  
  33. private const int maxReadSize = 1024;
  34. private static int totalReadSize;
  35. private static MemoryStream readStream;
  36. private static byte[] readBuffer;
  37. private static ConnectState connectState = ConnectState.None;
  38. private static string statusCode;
  39. private static long contentLength;
  40.  
  41. public enum ConnectState
  42. {
  43. None,
  44. Ready,
  45. Connect,
  46. Success,
  47. Failed
  48. }
  49.  
  50. static bool loop = true;
  51.  
  52. public static void Main (string[] args)
  53. {
  54. Init ();
  55.  
  56. while (loop) {
  57. SystemEvents.CheckEvents ();
  58. Update ();
  59. Render ();
  60. }
  61.  
  62. Term ();
  63. }
  64.  
  65. public static bool Init ()
  66. {
  67. graphics = new GraphicsContext ();
  68. SampleDraw.Init (graphics);
  69.  
  70. connectButton = new SampleButton (48, 48, 256, 64);
  71. connectButton.SetText ("Connect");
  72.  
  73. tekitouBtn = new SampleButton(400,10,200,64);
  74. tekitouBtn.SetText("tekitou");
  75.  
  76. connectState = ConnectState.None;
  77.  
  78. return true;
  79. }
  80.  
  81. /// Terminate
  82. public static void Term ()
  83. {
  84. if (httpText != null) {
  85. httpText.Dispose ();
  86. httpText = null;
  87. }
  88.  
  89. connectButton.Dispose ();
  90.  
  91. SampleDraw.Term ();
  92. graphics.Dispose ();
  93. }
  94.  
  95. public static bool Update ()
  96. {
  97. List<TouchData> touchDataList = Touch.GetData (0);
  98.  
  99. if(tekitouBtn.TouchDown(touchDataList)){
  100. buttonState = !buttonState;
  101.  
  102. }
  103.  
  104. if(buttonState)
  105. {
  106. tekitouBtn.ButtonColor = 0xaaaaaaaa;
  107. }
  108. else
  109. {
  110. tekitouBtn.ButtonColor =0x88888888;
  111. }
  112.  
  113. switch (connectState) {
  114. case ConnectState.None:
  115. if (connectButton.TouchDown (touchDataList)) {
  116. connectState = ConnectState.Ready;
  117. if (httpText != null) {
  118. httpText.Dispose ();
  119. httpText = null;
  120. }
  121. connectButton.ButtonColor = 0xff7f7f7f;
  122. }
  123. break;
  124. case ConnectState.Ready:
  125. connectHttpTest ("http://h...content-available-to-author-only...h.net/news4vip/subback.html");
  126.  
  127. break;
  128. case ConnectState.Success:
  129. connectButton.ButtonColor = 0xffffffff;
  130.  
  131. if (readStream != null) {
  132. httpText = createTextSprite (readStream.ToArray (), statusCode, contentLength);
  133.  
  134. readStream.Dispose ();
  135. readStream = null;
  136. readBuffer = null;
  137. }
  138.  
  139. connectState = ConnectState.None;
  140. break;
  141. case ConnectState.Failed:
  142. httpText = createTextSprite (null, statusCode, contentLength);
  143. connectState = ConnectState.None;
  144. break;
  145. }
  146.  
  147. return true;
  148. }
  149.  
  150. public static bool Render ()
  151. {
  152. graphics.SetClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  153. graphics.Clear ();
  154.  
  155. if (httpText != null) {
  156. SampleDraw.DrawSprite (httpText);
  157. }
  158.  
  159. connectButton.Draw ();
  160. tekitouBtn.Draw();
  161.  
  162. SampleDraw.DrawText ("Http Sample", 0xffffffff, 0, 0);
  163. graphics.SwapBuffers ();
  164.  
  165. return true;
  166. }
  167.  
  168. /// Http connection test
  169. private static bool connectHttpTest (string url)
  170. {
  171. statusCode = "Unknown";
  172. contentLength = 0;
  173.  
  174. try {
  175. var webRequest = HttpWebRequest.Create (url);
  176. // If you use web proxy, uncomment this and set appropriate address.
  177. //webRequest.Proxy = new System.Net.WebProxy("http://your_proxy.com:10080");
  178. webRequest.BeginGetResponse (new AsyncCallback (requestCallBack), webRequest);
  179. connectState = ConnectState.Connect;
  180.  
  181. } catch (Exception e) {
  182. Console.WriteLine (e);
  183. connectState = ConnectState.Failed;
  184. return false;
  185. }
  186.  
  187. return true;
  188. }
  189.  
  190. private static void requestCallBack (IAsyncResult ar)
  191. {
  192. try {
  193. var webRequest = (HttpWebRequest)ar.AsyncState;
  194. var webResponse = (HttpWebResponse)webRequest.EndGetResponse (ar);
  195.  
  196. statusCode = webResponse.StatusCode.ToString ();
  197. contentLength = webResponse.ContentLength;
  198. readBuffer = new byte[1024];
  199. readStream = new MemoryStream ();
  200. totalReadSize = 0;
  201.  
  202. var stream = webResponse.GetResponseStream ();
  203.  
  204. stream.BeginRead (readBuffer, 0, readBuffer.Length, new AsyncCallback (readCallBack), stream);
  205.  
  206.  
  207. } catch (Exception e) {
  208. Console.WriteLine (e);
  209. connectState = ConnectState.Failed;
  210. }
  211. }
  212.  
  213. private static void readCallBack (IAsyncResult ar)
  214. {
  215. try {
  216. var stream = (Stream)ar.AsyncState;
  217. int readSize = stream.EndRead (ar);
  218.  
  219. if (readSize > 0) {
  220. totalReadSize += readSize;
  221. readStream.Write (readBuffer, 0, readSize);
  222. }
  223.  
  224. if (readSize <= 0) {
  225. stream.Close ();
  226. connectState = ConnectState.Success;
  227. } else {
  228. stream.BeginRead (readBuffer, 0, readBuffer.Length, new AsyncCallback (readCallBack), stream);
  229. CreateGarbage();
  230. //Collect ();
  231. buttonState = !buttonState;
  232.  
  233. }
  234. } catch (Exception e) {
  235. Console.WriteLine (e);
  236. connectState = ConnectState.Failed;
  237. }
  238. }
  239.  
  240. /// Create a sprite from a string buffer
  241. private static SampleSprite createTextSprite (byte[] buffer, string statusCode, long contentLength)
  242. {
  243. int width = SampleDraw.Width;
  244. int height = SampleDraw.Height - 128;
  245. int positionX = 0;
  246. int positionY = 0;
  247. int fontHeight = SampleDraw.CurrentFont.Metrics.Height;
  248.  
  249. var image = new Image (ImageMode.Rgba,
  250. new ImageSize (width, height),
  251. new ImageColor (0, 0, 0, 0));
  252.  
  253. image.DrawText ("ResponseCode : " + statusCode, new ImageColor (0xff, 0xff, 0xff, 0xff), SampleDraw.CurrentFont, new ImagePosition (positionX, positionY));
  254. positionY += fontHeight;
  255. image.DrawText ("ContentLength : " + contentLength, new ImageColor (0xff, 0xff, 0xff, 0xff), SampleDraw.CurrentFont, new ImagePosition (positionX, positionY));
  256. positionY += fontHeight;
  257. positionY += fontHeight;
  258.  
  259. if (buffer != null) {
  260. var stream = new MemoryStream (buffer);
  261. var reader = new StreamReader (stream);
  262.  
  263. while (!reader.EndOfStream && positionY < height) {
  264. string text = reader.ReadLine ();
  265. image.DrawText (text, new ImageColor (0xff, 0xff, 0xff, 0xff), SampleDraw.CurrentFont, new ImagePosition (positionX, positionY));
  266. positionY += fontHeight;
  267. }
  268.  
  269. reader.Close ();
  270. stream.Close ();
  271. }
  272.  
  273. var texture = new Texture2D (width, height, false, PixelFormat.Rgba);
  274. texture.SetPixels (0, image.ToBuffer ());
  275. image.Dispose ();
  276.  
  277. var sprite = new SampleSprite (texture, 0, 128);
  278.  
  279. return sprite;
  280. }
  281.  
  282.  
  283. static void CreateGarbage()
  284. {
  285. for(int i = 0; i < 1000; ++i)
  286. {
  287. var x = new int[10];
  288. }
  289. }
  290.  
  291. static void Collect()
  292. {
  293.  
  294. GC.Collect();
  295. }
  296.  
  297. }
  298.  
  299. } // Sample
  300.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty