Example #1
0
        private void OnDataReceived(UdpSocketReceiveData receivedData)
        {
            ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "OnDataReceived");

            try
            {
                if (this.DataReceived != null)
                {
                    this.DataReceived(receivedData);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
        }
Example #2
0
        private void OnListen()
        {
            ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "OnListen");

            try
            {
                IExecutorService executor = this.Executor;
                int        waitTime       = _parameter.ListenWaitTime;
                IPEndPoint ep             = _socket.LocalEndPoint as IPEndPoint;

                while (!executor.WaitForShutdown(waitTime))
                {
                    try
                    {
                        EndPoint     remoteEndPoint = new IPEndPoint(ep.Address, ep.Port);
                        IAsyncResult readAsync      = _socket.BeginReadFrom(_readBuffer, ref remoteEndPoint);

                        int waitIndex = executor.WaitAny(readAsync.AsyncWaitHandle, -1);
                        if (waitIndex == 0)
                        {
                            break;
                        }

                        int bytesRead = _socket.EndReadFrom(readAsync, ref remoteEndPoint);
                        if (bytesRead <= 0)
                        {
                            _socket.WriteSocketLog("Invalid data received from : " + remoteEndPoint.ToString());
                        }
                        else
                        {
                            byte[] receivedBuffer = new byte[bytesRead];
                            Buffer.BlockCopy(_readBuffer, 0, receivedBuffer, 0, bytesRead);
                            IPEndPoint rep = remoteEndPoint as IPEndPoint;

                            UdpSocketReceiveData receivedData = new UdpSocketReceiveData()
                            {
                                Buffer         = receivedBuffer,
                                BufferLength   = receivedBuffer.Length,
                                RemoteEndpoint = new IPEndPoint(rep.Address, rep.Port),
                            };
                            if (_useInternalQueue)
                            {
                                _queueReceived.Enqueue(receivedData);
                            }
                            else
                            {
                                this.OnDataReceived(receivedData);
                            }
                        }
                        remoteEndPoint = null;
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(PROC, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
        }