Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. tex.GetPixels32();
  2.  
  3. void Start()
  4. {
  5. webcamTexture = new WebCamTexture();
  6. Background.texture = webcamTexture;
  7. Background.material.mainTexture = webcamTexture;
  8. webcamTexture.Play();
  9.  
  10. if (!_isStarted)
  11. {
  12. _isStarted = true;
  13.  
  14. NetworkTransport.Init();
  15. m_Config = new ConnectionConfig();
  16. m_CommunicationChannel = m_Config.AddChannel(QosType.ReliableFragmented);
  17. HostTopology topology = new HostTopology(m_Config, 12);
  18. m_GenericHostId = NetworkTransport.AddHost(topology, 0);
  19. byte error;
  20. m_ConnectionId = NetworkTransport.Connect(m_GenericHostId, ip, port, 0, out error);
  21. }
  22. }
  23.  
  24. void Update()
  25. {
  26. if (!_isStarted)
  27. return;
  28.  
  29. NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
  30.  
  31. switch (recData)
  32. {
  33. case NetworkEventType.Nothing: //1
  34. break;
  35. case NetworkEventType.ConnectEvent: //2
  36. Debug.Log("Received connection confirmation");
  37. _readyToSend = true;
  38. break;
  39. case NetworkEventType.DataEvent: //3
  40.  
  41. break;
  42. case NetworkEventType.DisconnectEvent: //4
  43. //one of the established connection has been disconnected
  44. Debug.Log(String.Format("Disconnect from host {0} connection {1}", recHostId, connectionId));
  45.  
  46. break;
  47. }
  48.  
  49. if (_readyToSend)
  50. {
  51. _readyToSend = false; // To send just the first frame
  52.  
  53. byte[] colourArray = SerializeObject(MakeSerializable(GetRenderTexturePixels(webcamTexture))); // Serialize the webcam texture
  54.  
  55. // Sending total size
  56. byte[] sizeToSend = BitConverter.GetBytes(colourArray.Length);
  57. NetworkTransport.Send(m_GenericHostId, m_ConnectionId, m_CommunicationChannel, sizeToSend, sizeToSend.Length, out error);
  58.  
  59. byte[] bytes = new byte[bufferLenght];
  60. int remainingBytes = colourArray.Length;
  61. int index = 0;
  62. int i = 1;
  63.  
  64. while (remainingBytes >= bufferLenght)
  65. {
  66. System.Buffer.BlockCopy(colourArray, index, bytes, 0, bufferLenght);
  67. NetworkTransport.Send(m_GenericHostId, m_ConnectionId, m_CommunicationChannel, bytes, bytes.Length, out error);
  68. remainingBytes -= bufferLenght;
  69. Debug.Log(i++ + "Remaining bytes: " + remainingBytes + " - Error: "+error);
  70. index += bufferLenght;
  71. }
  72.  
  73. if (remainingBytes > 0) // Send the last fragment below bufferLenght bytes
  74. {
  75. System.Buffer.BlockCopy(colourArray, index, bytes, 0, remainingBytes);
  76. NetworkTransport.Send(m_GenericHostId, m_ConnectionId, m_CommunicationChannel, bytes, remainingBytes, out error);
  77. Debug.Log("Error: "+error);
  78. }
  79. }
  80. }
  81.  
  82. void Start()
  83. {
  84. if (!_isStarted)
  85. {
  86. _isStarted = true;
  87.  
  88. NetworkTransport.Init();
  89.  
  90. m_Config = new ConnectionConfig();
  91.  
  92. m_CommunicationChannel = m_Config.AddChannel(QosType.ReliableFragmented);
  93.  
  94. HostTopology topology = new HostTopology(m_Config, 12);
  95.  
  96. m_GenericHostId = NetworkTransport.AddHost(topology, port, null);
  97. }
  98. }
  99.  
  100. void Update()
  101. {
  102. if (!_isStarted)
  103. return;
  104.  
  105. int recHostId;
  106. int connectionId;
  107. int channelId;
  108. byte[] recBuffer = new byte[bufferLenght];
  109. int bufferSize = bufferLenght;
  110. int dataSize;
  111. byte error;
  112.  
  113. NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
  114.  
  115. switch (recData)
  116. {
  117. case NetworkEventType.Nothing: //1
  118. break;
  119.  
  120. case NetworkEventType.ConnectEvent: //2
  121.  
  122. //somebody else connect to me
  123. Log.text += string.Format("Connect from host {0} connection {1}n", recHostId, connectionId);
  124. break;
  125.  
  126. case NetworkEventType.DataEvent: //3
  127.  
  128. if (!sizeReceived)
  129. {
  130. sizeReceived = true;
  131.  
  132. if (dataSize == 2)
  133. {
  134. bytesToReceive = BitConverter.ToInt16(recBuffer, 0);
  135. }
  136. else if (dataSize == 4)
  137. {
  138. bytesToReceive = BitConverter.ToInt32(recBuffer, 0);
  139. }
  140.  
  141. Debug.Log("We will receive: "+bytesToReceive);
  142. }
  143. else
  144. {
  145. Log.text = string.Format("Received event host {0} connection {1} channel {2} message length {3}n", recHostId, connectionId, channelId, dataSize);
  146.  
  147. Log.text += "Received " + bufferSize + " bytesn";
  148. bytesToReceive -= bufferSize;
  149. Log.text += "Remaining " + bytesToReceive + " bytesn";
  150. }
  151. break;
  152.  
  153. case NetworkEventType.DisconnectEvent: //4
  154.  
  155. break;
  156.  
  157. }
  158. }
  159.  
  160. no free events for long message
  161. UnityEngine.Networking.NetworkTransport:Send(Int32, Int32, Int32, Byte[], Int32, Byte&)
  162. CameraStreamer:Update() (at Assets/Scripts/Client/CameraStreamer.cs:112)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement