Advertisement
asqapro

Unity Sockets

Feb 5th, 2014
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Collections.Generic;
  8.  
  9. public class client_class{ //class for the accept() threads (to avoid blocking)
  10.     public Socket connection = null;
  11.     public const int buff_size = 256;
  12.     public byte[] buff = new byte[buff_size];
  13. }
  14.  
  15. public class Client : MonoBehaviour {
  16.  
  17.     static public double data;
  18.  
  19.     static List<int> decode_data_int(int decode_data_int){
  20.         //ints
  21.         List<int> digits = new List<int>();
  22.         int decode_data_int_copy = decode_data_int;
  23.         bool found = false;
  24.         while(!found){
  25.             int places = 0;
  26.             while(decode_data_int_copy > 10){
  27.                 decode_data_int_copy /= 10;
  28.                 places++;
  29.             }
  30.             if(places == 0){
  31.                 found = true;
  32.             }
  33.             digits.Add(decode_data_int_copy);
  34.             decode_data_int -= (int)(decode_data_int_copy * Math.Pow(10, places));
  35.             decode_data_int_copy = decode_data_int;
  36.         }
  37.         return digits;
  38.     }
  39.  
  40.     static List<int> decode_data_double(byte[] data){
  41.         //doubles
  42.         int places = 0;
  43.         double decode_data_double = Convert.ToDouble(data);
  44.         double decode_data_copy = decode_data_double;
  45.         while(decode_data_copy - (int)decode_data_copy > 0.1){
  46.             decode_data_copy *= 10;
  47.             places++;
  48.         }
  49.         decode_data_copy = decode_data_double;
  50.         decode_data_copy -= (int)decode_data_copy;
  51.         decode_data_copy *= Math.Pow(10, places);
  52.         return decode_data_int((int)decode_data_copy);
  53.     }
  54.  
  55.     IEnumerator pause() {
  56.         yield return new WaitForEndOfFrame();
  57.     }
  58.  
  59.     private static ManualResetEvent connectDone = new ManualResetEvent(false); //event state
  60.     private static ManualResetEvent sendDone = new ManualResetEvent(false); //event state
  61.  
  62.     void client(){
  63.         //IPAddress extern_ip = "l73.21.71"; //gets the local IP
  64.         //IPEndPoint externEndPoint = new IPEndPoint(extern_ip, 0); //gets the local IP
  65.         Socket connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  66.         //client.BeginConnect(externEndPoint, new AsyncCallback(ConnectCallback), client);
  67.  
  68.         client_class client = new client_class();
  69.         client.connection = connection;
  70.  
  71.         connection.BeginReceive(client.buff, 0, client_class.buff_size, 0, new AsyncCallback(ReceiveCallback), client);
  72.         while(true){
  73.             if(data != -1){
  74.                 byte[] byteData = BitConverter.GetBytes(data);
  75.                 connection.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), connection);
  76.             }
  77.             StartCoroutine(pause()); //to ease load of while loop. possibly too long of a pause
  78.         }
  79.     }
  80.  
  81.     private static void ConnectCallback(IAsyncResult ar){
  82.         // Retrieve the socket from the state object.
  83.         Socket connection = (Socket)ar.AsyncState;
  84.        
  85.         // Complete the connection.
  86.         connection.EndConnect(ar);
  87.        
  88.         // Signal that the connection has been made.
  89.         connectDone.Set();
  90.     }
  91.  
  92.     private static void SendCallback(IAsyncResult ar){
  93.         // Retrieve the socket from the state object.
  94.         Socket connection = (Socket)ar.AsyncState;
  95.        
  96.         // Complete sending the data to the remote device.
  97.         connection.EndSend(ar);
  98.  
  99.         data = -1;
  100.        
  101.         // Signal that all bytes have been sent.
  102.         sendDone.Set();
  103.     }
  104.    
  105.     private static void ReceiveCallback(IAsyncResult ar){
  106.         // Retrieve the state object and the client socket
  107.         // from the asynchronous state object.
  108.         client_class client = (client_class)ar.AsyncState;
  109.         Socket connection = client.connection;
  110.         // Read data from the remote device.
  111.         int bytesRead = connection.EndReceive(ar);
  112.         if (bytesRead > 0) {
  113.             //todo: process decoded data using dictionary
  114.             List<int> data_deci = decode_data_double(client.buff);
  115.             List<int> data_int = decode_data_int((int)Convert.ToDouble(client.buff));
  116.             //Get the rest of the data.
  117.             connection.BeginReceive(client.buff, 0, client_class.buff_size, 0, new AsyncCallback(ReceiveCallback), client);
  118.         }
  119.     }
  120.  
  121.     // Use this for initialization
  122.     void Start(){
  123.         client();
  124.     }
  125.    
  126.     // Update is called once per frame
  127.     void Update(){
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement