Esempio n. 1
0
        private static bool Receive(Socket client)
        {
            try {
                // Create the state object.
                DirectStateObject state = new DirectStateObject();
                state.workSocket = client;

                Debug.Log("[UDP multicast] Receive");

                // Begin receiving the data from the remote device.
                client.BeginReceive(state.buffer, 0, DirectStateObject.BufferSize, 0,
                                    new AsyncCallback(ReceiveCallback), state);
            } catch (Exception e) {
                Debug.Log(e.ToString());
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        private static void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                //Debug.Log("[UDP multicast] Start ReceiveCallback");
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                DirectStateObject state  = (DirectStateObject)ar.AsyncState;
                Socket            client = state.workSocket;

                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0 && _isIsActiveThread)
                {
                    ReadPacket(state.buffer);

                    //client.Shutdown(SocketShutdown.Both);
                    //client.Close();

                    client.BeginReceive(state.buffer, 0, DirectStateObject.BufferSize, 0,
                                        new AsyncCallback(ReceiveCallback), state);
                }
                else
                {
                    Debug.LogWarning("[UDP] - End ReceiveCallback");

                    if (_isIsActiveThread == false)
                    {
                        Debug.LogWarning("[UDP] - Closing port");
                        _isInitRecieveStatus = false;
                        //client.Shutdown(SocketShutdown.Both);
                        client.Close();
                    }

                    // Signal that all bytes have been received.
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }
        private static bool Receive(Socket client)
        {
            try
            {
                // Create the state object.
                DirectStateObject state = new DirectStateObject();
                state.workSocket = client;

                Debug.Log("[UDP multicast] Receive");

                // Begin receiving the data from the remote device.
                client.BeginReceive( state.buffer, 0, DirectStateObject.BufferSize, 0,
                                    new AsyncCallback(ReceiveCallback), state);

            } catch (Exception e)
            {
                Debug.Log(e.ToString());
                return false;
            }

            return true;
        }