Advertisement
Guest User

Loloooo

a guest
Apr 20th, 2014
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="CSharpSocket.cs" company="Exit Games GmbH">
  3. // Protocol & Photon Client Lib - Copyright (C) 2013 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Uses the UDP socket for a peer to send and receive enet/Photon messages.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10.  
  11. #if UNITY_EDITOR || (!UNITY_ANDROID && !UNITY_IPHONE && !UNITY_PS3 && !UNITY_WIIU && !UNITY_WINRT && !UNITY_WP8)
  12.  
  13. namespace ExitGames.Client.Photon
  14. {
  15. using System;
  16. using System.Net;
  17. using System.Net.Sockets;
  18. using System.Security;
  19. using System.Threading;
  20.  
  21. /// <summary> Internal class to encapsulate the network i/o functionality for the realtime libary.</summary>
  22. internal class SocketUdp : IPhotonSocket
  23. {
  24. private Socket sock;
  25.  
  26. private readonly object syncer = new object();
  27.  
  28. public SocketUdp(PeerBase npeer) : base(npeer)
  29. {
  30. if (this.ReportDebugOfLevel(DebugLevel.ALL))
  31. {
  32. this.Listener.DebugReturn(DebugLevel.ALL, "CSharpSocket: UDP, Unity3d.");
  33. }
  34.  
  35. this.Protocol = ConnectionProtocol.Udp;
  36. this.PollReceive = false;
  37. }
  38.  
  39. public override bool Connect()
  40. {
  41. lock (this.syncer)
  42. {
  43. bool baseOk = base.Connect();
  44. if (!baseOk)
  45. {
  46. return false;
  47. }
  48.  
  49. this.State = PhotonSocketState.Connecting;
  50.  
  51. Thread dns = new Thread(this.DnsAndConnect);
  52. dns.Name = "photon dns thread";
  53. dns.IsBackground = true;
  54. dns.Start();
  55.  
  56. return true;
  57. }
  58. }
  59.  
  60. public override bool Disconnect()
  61. {
  62. if (this.ReportDebugOfLevel(DebugLevel.INFO))
  63. {
  64. this.EnqueueDebugReturn(DebugLevel.INFO, "CSharpSocket.Disconnect()");
  65. }
  66.  
  67. this.State = PhotonSocketState.Disconnecting;
  68.  
  69. lock (this.syncer)
  70. {
  71. if (this.sock != null)
  72. {
  73. try
  74. {
  75. this.sock.Close();
  76. this.sock = null;
  77. }
  78. catch (Exception ex)
  79. {
  80. this.EnqueueDebugReturn(DebugLevel.INFO, "Exception in Disconnect(): " + ex);
  81. }
  82. }
  83. }
  84.  
  85. this.State = PhotonSocketState.Disconnected;
  86. return true;
  87. }
  88.  
  89. /// <summary>used by PhotonPeer*</summary>
  90. public override PhotonSocketError Send(byte[] data, int length)
  91. {
  92. lock (this.syncer)
  93. {
  94. if (!this.sock.Connected)
  95. {
  96. return PhotonSocketError.Skipped;
  97. }
  98.  
  99. try
  100. {
  101. sock.Send(data, 0, length, SocketFlags.None);
  102. }
  103. catch
  104. {
  105. return PhotonSocketError.Exception;
  106. }
  107. }
  108.  
  109. return PhotonSocketError.Success;
  110. }
  111.  
  112. public override PhotonSocketError Receive(out byte[] data)
  113. {
  114. data = null;
  115. return PhotonSocketError.NoData;
  116. }
  117.  
  118. internal void DnsAndConnect()
  119. {
  120. try
  121. {
  122. lock (this.syncer)
  123. {
  124. this.sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  125.  
  126. IPAddress ep = IPhotonSocket.GetIpAddress(this.ServerAddress);
  127. this.sock.Connect(ep, this.ServerPort);
  128.  
  129. this.State = PhotonSocketState.Connected;
  130. }
  131. }
  132. catch (SecurityException se)
  133. {
  134. if (this.ReportDebugOfLevel(DebugLevel.ERROR))
  135. {
  136. this.Listener.DebugReturn(DebugLevel.ERROR, "Connect() failed: " + se.ToString());
  137. }
  138.  
  139. this.HandleException(StatusCode.SecurityExceptionOnConnect);
  140. return;
  141. }
  142. catch (Exception se)
  143. {
  144. if (this.ReportDebugOfLevel(DebugLevel.ERROR))
  145. {
  146. this.Listener.DebugReturn(DebugLevel.ERROR, "Connect() failed: " + se.ToString());
  147. }
  148.  
  149. this.HandleException(StatusCode.ExceptionOnConnect);
  150. return;
  151. }
  152.  
  153. Thread run = new Thread(new ThreadStart(ReceiveLoop));
  154. run.Name = "photon receive thread";
  155. run.IsBackground = true;
  156. run.Start();
  157. }
  158.  
  159. /// <summary>Endless loop, run in Receive Thread.</summary>
  160. public void ReceiveLoop()
  161. {
  162. byte[] inBuffer = new byte[this.MTU];
  163. while (this.State == PhotonSocketState.Connected)
  164. {
  165. try
  166. {
  167. int read = this.sock.Receive(inBuffer);
  168. this.HandleReceivedDatagram(inBuffer, read, true);
  169. }
  170. catch (Exception e)
  171. {
  172. if (this.State != PhotonSocketState.Disconnecting && this.State != PhotonSocketState.Disconnected)
  173. {
  174. if (this.ReportDebugOfLevel(DebugLevel.ERROR))
  175. {
  176. this.EnqueueDebugReturn(DebugLevel.ERROR, "Receive issue. State: " + this.State + " Exception: " + e);
  177. }
  178.  
  179. this.HandleException(StatusCode.ExceptionOnReceive);
  180. }
  181. }
  182. } //while Connected receive
  183.  
  184. // on exit of the receive-loop: disconnect socket
  185. this.Disconnect();
  186. }
  187. } //class
  188.  
  189. }
  190. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement