Esempio n. 1
0
        /// <summary>
        /// Receive buffer from connected client (Non-blocking).
        /// </summary>
        public void Receive(Action onReceived)
        {
            receiveState.bufferSize       = 0;
            receiveState.numBytesReceived = 0;
            receiveState.callback         = onReceived;
#if TCPSOCKET_DEBUG
            Debug.Log("Receiving...");
#endif
            // receive
#if UNITY_EDITOR || UNITY_STANDALONE
            client.BeginReceive(receiveState.buffer, 0, 4, SocketFlags.None, OnReceived, null);
#elif NETFX_CORE
            HoloLensSocket.Receive(this, client, receiveState, onReceived);
#endif
        }
        /// <summary>
        /// Connect to client through specified ip and port.
        /// Port can also be "loopback".
        /// </summary>
        public override void Connect(string ip, int port, Action onConnected)
        {
#if TCPCLIENT_DEBUG
            Debug.Log("Connecting to server on: " + ip + ":" + port.ToString());
#endif
#if UNITY_EDITOR || UNITY_STANDALONE
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var endPoint = new IPEndPoint(
                ip == "loopback" ? IPAddress.Loopback : IPAddress.Parse(ip),
                port
                );

            connectState.callback = onConnected;
            client.BeginConnect(endPoint, OnConnected, null);
#elif NETFX_CORE
            client = HoloLensSocket.Connect(ip, port, () => { } +onConnected);
#endif
        }
Esempio n. 3
0
        /// <summary>
        /// Send buffer to connected client (Non-blocking).
        /// </summary>
        public void Send(int size, Action onSent)
        {
            // prepend size
            var head = BitConverter.GetBytes(size);

            Buffer.BlockCopy(head, 0, sendState.buffer, 0, 4);

            // resete send state
            sendState.bufferSize   = 4 + size;
            sendState.numBytesSent = 0;
            sendState.callback     = onSent;
#if TCPSOCKET_DEBUG
            Debug.Log("Sending " + sendState.bufferSize.ToString() + " bytes...");
#endif
            // send
#if UNITY_EDITOR || UNITY_STANDALONE
            client.BeginSend(sendState.buffer, 0, sendState.bufferSize, SocketFlags.None, OnSent, null);
#elif NETFX_CORE
            HoloLensSocket.Send(client, sendState, onSent);
#endif
        }
        /// <summary>
        /// Connect to client through specified ip and port.
        /// Port can also be "any" and "loopback".
        /// </summary>
        public override void Connect(string ip, int port, Action onConnected)
        {
#if TCPSERVER_DEBUG
            Debug.Log("Waiting for client on: " + ip + ":" + port.ToString());
#endif
#if UNITY_EDITOR || UNITY_STANDALONE
            var ipAddress = default(IPAddress);
            if (ip == "any")
            {
                ipAddress = IPAddress.Any;
            }
            else if (ip == "loopback")
            {
                ipAddress = IPAddress.Loopback;
            }
            else
            {
                ipAddress = IPAddress.Parse(ip);
            }

            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(new IPEndPoint(ipAddress, port));
            server.Listen(1);

            connectState.callback = onConnected;
            server.BeginAccept(OnConnected, null);
#elif NETFX_CORE
            HoloLensSocket.Connect(port, (StreamSocket client) =>
            {
                this.client = client;
                if (onConnected != null)
                {
                    onConnected();
                }
            });
#endif
        }