Пример #1
0
        public RtmpClientConnection(IRtmpHandler handler, Socket socket)
            : base(handler, RtmpMode.Client, null, null)
		{
#if FXCLIENT
            //TODO
            socket.ReceiveBufferSize = 4096;
            socket.SendBufferSize = 4096;
#else
            socket.ReceiveBufferSize = FluorineConfiguration.Instance.FluorineSettings.RtmpServer.RtmpTransportSettings.ReceiveBufferSize;
            socket.SendBufferSize = FluorineConfiguration.Instance.FluorineSettings.RtmpServer.RtmpTransportSettings.SendBufferSize;
#endif
            _handler = handler;
            _readBuffer = ByteBuffer.Allocate(4096);
            _readBuffer.Flip();
            _rtmpNetworkStream = new RtmpNetworkStream(socket);
            Context.SetMode(RtmpMode.Client);
		}
        // <summary>
        // Name of job that is waiting for a valid handshake.
        // </summary>
        //private string _waitForHandshakeJob;
        
        public RtmpServerConnection(RtmpServer rtmpServer, RtmpNetworkStream stream)
            : base(rtmpServer.RtmpHandler, RtmpMode.Server, null, null)
		{
            _lock = new FastReaderWriterLock();
            _endpoint = rtmpServer.Endpoint;
            _readBuffer = ByteBuffer.Allocate(4096);
            _readBuffer.Flip();

			// We start with an anonymous connection without a scope.
			// These parameters will be set during the call of "connect" later.
            _rtmpServer = rtmpServer;
            _rtmpNetworkStream = stream;
            //_state = RtmpConnectionState.Active;
            SetIsTunneled(false);
            IsTunnelingDetected = false;

            _readBytes = new AtomicLong();
            _writtenBytes = new AtomicLong();

            //Set the legacy collection flag from the endpoint channel settings
            this.Context.UseLegacyCollection = (_endpoint as RtmpEndpoint).IsLegacyCollection;
            this.Context.UseLegacyThrowable = (_endpoint as RtmpEndpoint).IsLegacyThrowable;
		}
        public RtmpClientConnection(IRtmpHandler handler, Socket socket, bool secure, string host)
            : base(handler, RtmpMode.Client, null, null)
		{
			_secure = secure;
			_host = host;
#if FXCLIENT
            //TODO
            socket.ReceiveBufferSize = 4096;
            socket.SendBufferSize = 4096;
#else
            socket.ReceiveBufferSize = FluorineConfiguration.Instance.FluorineSettings.RtmpServer.RtmpTransportSettings.ReceiveBufferSize;
            socket.SendBufferSize = FluorineConfiguration.Instance.FluorineSettings.RtmpServer.RtmpTransportSettings.SendBufferSize;
#endif
            _handler = handler;
            _readBuffer = ByteBuffer.Allocate(4096);
            _readBuffer.Flip();

			// Wrap a secure connection in an SslStream.
			if (_secure)
			{
				//new SslStream(tcpstream, false, new RemoteCertificateValidationCallback(TrustAllCertificatesCallback));
				SslStream stream = new SslStream(new NetworkStream(socket), false, new RemoteCertificateValidationCallback(TrustAllCertificates));
				stream.AuthenticateAsClient(host);
				_rtmpNetworkStream = new RtmpNetworkStream(socket, stream);
			}
			else
				_rtmpNetworkStream = new RtmpNetworkStream(socket);

            Context.SetMode(RtmpMode.Client);
		}
 public static RtmptRequest DecodeBuffer(RtmpConnection connection, ByteBuffer stream)
 {
     RtmpContext context = connection.Context;
     int position = (int)stream.Position;
     try
     {
         BufferStreamReader sr = new BufferStreamReader(stream);
         string request = sr.ReadLine();
         string[] tokens = request.Split(new char[] { ' ' });
         string method = tokens[0];
         string url = tokens[1];
         // Decode all encoded parts of the URL using the built in URI processing class
         int i = 0;
         while ((i = url.IndexOf("%", i)) != -1)
         {
             url = url.Substring(0, i) + Uri.HexUnescape(url, ref i) + url.Substring(i);
         }
         // Lets just make sure we are using HTTP, thats about all I care about
         string protocol = tokens[2];// "HTTP/"
         //Read headers
         Hashtable headers = new Hashtable();
         string line;
         string name = null;
         while ((line = sr.ReadLine()) != null && line != string.Empty)
         {
             // If the value begins with a space or a hard tab then this
             // is an extension of the value of the previous header and
             // should be appended
             if (name != null && Char.IsWhiteSpace(line[0]))
             {
                 headers[name] += line;
                 continue;
             }
             // Headers consist of [NAME]: [VALUE] + possible extension lines
             int firstColon = line.IndexOf(":");
             if (firstColon != -1)
             {
                 name = line.Substring(0, firstColon);
                 string value = line.Substring(firstColon + 1).Trim();
                 headers[name] = value;
             }
             else
             {
                 //400, "Bad header: " + line
                 break;
             }
         }
         RtmptRequest rtmptRequest = new RtmptRequest(connection, url, protocol, method, headers);
         if (stream.Remaining == rtmptRequest.ContentLength)
         {
             stream.Compact();
             rtmptRequest.Data = ByteBuffer.Wrap(stream.ToArray());
             stream.Flip();
             return rtmptRequest;
         }
         else
         {
             // Move the position back to the start
             stream.Position = position;
         }
     }
     catch
     {
         // Move the position back to the start
         stream.Position = position;
         throw;
     }
     return null;
 }