예제 #1
0
        private void EndConnect(IAsyncResult result)
        {
            try
            {
                _rtpSocket.EndConnect(result);
            }
            catch (Exception)
            {
                return;
            }
            if (IsRunning)
            {
                //连接成功。
                //创建Socket网络流
                _stream = new NetworkStream(_rtpSocket);

                //开始接收数据
                byte[]         buffer         = new byte[RTP_RECEIVE_BUFFER_SIZE];
                TcpClientState internalClient = new TcpClientState(_stream, buffer);
                _stream.BeginRead(
                    internalClient.Buffer,
                    0,
                    internalClient.Buffer.Length,
                    HandleTcpDatagramReceived,
                    internalClient);
            }
        }
예제 #2
0
        private void HandleTcpDatagramReceived(IAsyncResult ar)
        {
            if (IsRunning)
            {
                TcpClientState internalClient = (TcpClientState)ar.AsyncState;
                _stream = internalClient.NetworkStream;

                int numberOfReadBytes = 0;
                try
                {
                    numberOfReadBytes = _stream.EndRead(ar);
                }
                catch
                {
                    numberOfReadBytes = 0;
                }

                // received byte and trigger event notification
                byte[] buffer = new byte[numberOfReadBytes];
                Buffer.BlockCopy(internalClient.Buffer, 0, buffer, 0, numberOfReadBytes);

                try
                {
                    int len       = 2;
                    int packetLen = GetPacketLen(buffer, 0, len);
                    int bufLen    = buffer.Length;
                    int srcOffset = len;
                    while (bufLen > packetLen)
                    {
                        byte[] newBuffer = new byte[packetLen];
                        Buffer.BlockCopy(buffer, srcOffset, newBuffer, 0, packetLen);

                        TcpDatagramToEnqueue(newBuffer);

                        srcOffset += packetLen;
                        if (srcOffset + len > buffer.Length)
                        {
                            break;
                        }
                        bufLen   -= packetLen + len;
                        packetLen = GetPacketLen(buffer, srcOffset, len);

                        srcOffset += len;
                    }
                    // continue listening for tcp datagram packets
                    _stream.BeginRead(
                        internalClient.Buffer,
                        0,
                        internalClient.Buffer.Length,
                        HandleTcpDatagramReceived,
                        internalClient);
                }
                catch (Exception)
                {
                }
            }
        }
예제 #3
0
        private void HandleTcpClientAccepted(IAsyncResult ar)
        {
            if (IsRunning)
            {
                TcpListener tcpListener = (TcpListener)ar.AsyncState;

                TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
                tcpClient.ReceiveBufferSize = RTP_RECEIVE_BUFFER_SIZE;
                byte[] buffer = new byte[tcpClient.ReceiveBufferSize];

                TcpClientState internalClient = new TcpClientState(tcpClient, buffer);
                _stream = internalClient.NetworkStream;
                _stream.BeginRead(
                    internalClient.Buffer,
                    0,
                    internalClient.Buffer.Length,
                    HandleTcpDatagramReceived,
                    internalClient);

                tcpListener.BeginAcceptTcpClient(new AsyncCallback(HandleTcpClientAccepted), ar.AsyncState);
            }
        }