Пример #1
0
 private void RaiseDatagramReceived(TcpClient tcpClient, byte[] receivedBytes)
 {
     if (DatagramReceivedEvent != null)
     {
         Delegate[] delArray = DatagramReceivedEvent.GetInvocationList();
         foreach (Delegate del in delArray)
         {
             try
             {
                 EventHandler <TcpDatagramReceivedEventArgs <byte[]> > method = (EventHandler <TcpDatagramReceivedEventArgs <byte[]> >)del;
                 method.BeginInvoke(this, new TcpDatagramReceivedEventArgs <byte[]>(tcpClient, receivedBytes), OnReceivedDatagramProcessComplete, null);
             }
             catch (Exception e)
             {
                 throw e;
             }
         }
     }
 }
Пример #2
0
        private void OnDataReadCompleted(IAsyncResult AsyncResult)
        {
            NetworkStreamState networkStreamState = (NetworkStreamState)AsyncResult.AsyncState;
            int numberOfReadBytes = 0;

            try
            {
                numberOfReadBytes = networkStreamState.networkStream.EndRead(AsyncResult);
            }
            catch
            {
                numberOfReadBytes = 0;
            }
            if (numberOfReadBytes == 0)
            {
                //networkStreamState.networkStream.Close(0);
                return;
            }
            //networkStreamState.networkStream.Close(0);
            byte[] receivedBytes = new byte[numberOfReadBytes];
            Buffer.BlockCopy(networkStreamState.buffer, 0, receivedBytes, 0, numberOfReadBytes);
            if (DatagramReceivedEvent != null)
            {
                Delegate[] delArray = DatagramReceivedEvent.GetInvocationList();
                foreach (Delegate del in delArray)
                {
                    try
                    {
                        EventHandler <TcpDatagramReceivedEventArgs <byte[]> > method = (EventHandler <TcpDatagramReceivedEventArgs <byte[]> >)del;
                        method.BeginInvoke(this, new TcpDatagramReceivedEventArgs <byte[]>(_localClient, receivedBytes), OnReceivedDatagramProcessComplete, null);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
            networkStreamState.networkStream.BeginRead(networkStreamState.buffer, 0, networkStreamState.buffer.Length, OnDataReadCompleted, networkStreamState);
        }