private void OnDisconnectCallback(object state) { _lock.AcquireReaderLock(); try { if (IsClosed || IsClosing) { return; // Already shutting down. } } finally { _lock.ReleaseReaderLock(); } log4net.ThreadContext.Properties["ClientIP"] = this.RemoteEndPoint; if (log.IsDebugEnabled) { log.Debug(__Res.GetString(__Res.Rtmp_SocketDisconnectProcessing, _connectionId)); } try { FluorineRtmpContext.Initialize(this); _rtmpServer.RtmpHandler.ConnectionClosed(this); //BaseRtmpHandler -> Close(); } catch (Exception ex) { if (log.IsErrorEnabled) { log.Error(this.ConnectionId.ToString(), ex); } } }
public override void Close() { _lock.AcquireWriterLock(); try { if (IsClosed || IsClosing) { return; // Already shutting down. } SetIsClosing(true); } finally { _lock.ReleaseWriterLock(); } FluorineRtmpContext.Initialize(this); base.Close(); _rtmpServer.OnConnectionClose(this); _rtmpNetworkStream.Close(); _lock.AcquireWriterLock(); try { SetIsClosed(true); SetIsClosing(false); SetIsDisconnecting(false); } finally { _lock.ReleaseWriterLock(); } }
public override void Execute(ScheduledJobContext context) { if (this._connection.IsConnected) { long readBytes = this._connection.ReadBytes; if (readBytes > this._connection._lastBytesRead) { this._connection._lastBytesRead = readBytes; } else { FluorineRtmpContext.Initialize(this._connection); if ((this._connection._lastPongReceived > 0) && ((this._connection._lastPingSent - this._connection._lastPongReceived) > FluorineConfiguration.Instance.FluorineSettings.RtmpServer.RtmpConnectionSettings.MaxInactivity)) { RtmpServerConnection.log.Debug("Keep alive job name " + this._connection._keepAliveJobName); (this._connection.Scope.GetService(typeof(ISchedulingService)) as ISchedulingService).RemoveScheduledJob(this._connection._keepAliveJobName); this._connection._keepAliveJobName = null; RtmpServerConnection.log.Warn(string.Format("Closing {0} due to too much inactivity ({0}).", this._connection, this._connection._lastPingSent - this._connection._lastPongReceived)); this._connection.OnInactive(); } else { this._connection.Ping(); } } } }
private void HandleOpen(HttpRequest request, HttpResponse response) { Unreferenced.Parameter(request); //Pass a null IPEndPoint, for this connection will create it on demand RtmptConnection connection = new RtmptConnection(this, null, null, null); FluorineRtmpContext.Initialize(connection); /* * HttpSession session = _endpoint.GetMessageBroker().SessionManager.GetHttpSession(HttpContext.Current); * FluorineContext.Current.SetSession(session); * RtmptConnection connection = new RtmptConnection(this, null, session, null, null); * Client client = this.Endpoint.GetMessageBroker().ClientRegistry.GetClient(connection.ConnectionId) as Client; * FluorineContext.Current.SetClient(client); * FluorineContext.Current.SetConnection(connection); * connection.Initialize(client); * //Current object are set notify listeners. * if (session != null && session.IsNew) * session.NotifyCreated(); * if (client != null) * { * client.RegisterSession(session); * client.NotifyCreated(); * } */ _connections[connection.ConnectionId] = connection; // Return connection id to client ReturnMessage(connection.ConnectionId + "\n", response); }
private void HandleIdent(RtmptRequest request) { RtmptConnection connection = new RtmptConnection(this, request.Connection.RemoteEndPoint, null, null); FluorineRtmpContext.Initialize(connection); _connections[connection.ConnectionId] = connection; // Return connection id to client ReturnMessage(Ident, request); }
public override void ConnectionOpened(RtmpConnection connection) { base.ConnectionOpened(connection); if (connection.Context.Mode == RtmpMode.Server) { FluorineRtmpContext.Initialize(connection); connection.StartWaitForHandshake(); } }
private void OnReceivedCallback(object state) { if (log.get_IsDebugEnabled()) { log.Debug(__Res.GetString("Rtmp_SocketReadProcessing", new object[] { base._connectionId })); } if (log.get_IsDebugEnabled()) { log.Debug("Begin handling packet " + this.ToString()); } if (!this.IsTunnelingDetected) { this.IsTunnelingDetected = true; byte num = this._readBuffer.Get(0); this.SetIsTunneled(num != 3); } try { if (!this.IsTunneled) { List <object> list = RtmpProtocolDecoder.DecodeBuffer(base.Context, this._readBuffer); if ((list != null) && (list.Count > 0)) { foreach (object obj2 in list) { if (obj2 is ByteBuffer) { ByteBuffer buf = obj2 as ByteBuffer; this.Send(buf); } else { FluorineRtmpContext.Initialize(this); this._rtmpServer.RtmpHandler.MessageReceived(this, obj2); } } } } else { this.HandleRtmpt(); } } catch (Exception exception) { this.HandleError(exception); } if (log.get_IsDebugEnabled()) { log.Debug("End handling packet " + this.ToString()); } this.BeginReceive(false); }
private void OnReceivedCallback(object state) { if (log.get_IsDebugEnabled()) { log.Debug(__Res.GetString("Rtmp_SocketReadProcessing", new object[] { base._connectionId })); } if (log.get_IsDebugEnabled()) { log.Debug("Begin handling packet " + this.ToString()); } try { List <object> list = RtmpProtocolDecoder.DecodeBuffer(base.Context, this._readBuffer); if (base.Context.State == RtmpState.Handshake) { ByteBuffer src = list[0] as ByteBuffer; src.Skip(1); src.Compact(); src.Limit = 0x600; ByteBuffer buf = ByteBuffer.Allocate(0x600); buf.Put(src); this.Send(buf); base.Context.State = RtmpState.Connected; this._handler.ConnectionOpened(this); } else if ((list != null) && (list.Count > 0)) { foreach (object obj2 in list) { if (obj2 is ByteBuffer) { ByteBuffer buffer3 = obj2 as ByteBuffer; this.Send(buffer3); } else { FluorineRtmpContext.Initialize(this); this._handler.MessageReceived(this, obj2); } } } } catch (Exception exception) { this.HandleError(exception); } if (log.get_IsDebugEnabled()) { log.Debug("End handling packet " + this.ToString()); } this.BeginReceive(false); }
private void HandleClose(RtmptRequest request) { RtmptConnection connection = GetConnection(request); if (connection == null) { HandleBadRequest(__Res.GetString(__Res.Rtmpt_UnknownClient, request.Url), request); return; } FluorineRtmpContext.Initialize(connection); RemoveConnection(connection.ConnectionId); _rtmpHandler.ConnectionClosed(connection); ReturnMessage(0, request); connection.RealClose(); }
private void HandleSend(HttpRequest request, HttpResponse response) { RtmptConnection connection = GetConnection(request); if (connection == null) { HandleBadRequest(__Res.GetString(__Res.Rtmpt_UnknownClient, GetHttpRequestPath(request)), response); return; } FluorineRtmpContext.Initialize(connection); //int length = request.ContentLength; byte[] data = new byte[request.InputStream.Length]; request.InputStream.Read(data, 0, (int)request.InputStream.Length); ByteBuffer buffer = ByteBuffer.Wrap(data); IList messages = connection.Decode(buffer); if (messages == null || messages.Count == 0) { ReturnMessage(connection.PollingDelay, response); return; } // Execute the received RTMP messages foreach (object message in messages) { try { if (message is ByteBuffer) { connection.Write(message as ByteBuffer); } else if (message is byte[]) { connection.Write(message as byte[]); } else { _rtmpHandler.MessageReceived(connection, message); } } catch (Exception ex) { Log.Error(__Res.GetString(__Res.Rtmp_CouldNotProcessMessage), ex); } } // Send results to client ReturnPendingMessages(connection, response); }
private void HandleSend(RtmptRequest request) { RtmptConnection connection = this.GetConnection(request); if (connection == null) { this.HandleBadRequest(__Res.GetString("Rtmpt_UnknownClient", new object[] { request.Url }), request); } else { if (connection.Client != null) { connection.Client.Renew(); } int contentLength = request.ContentLength; ByteBuffer data = request.Data; IList list = connection.Decode(data); if ((list == null) || (list.Count == 0)) { this.ReturnMessage(connection.PollingDelay, request); } else { foreach (object obj2 in list) { try { if (obj2 is ByteBuffer) { ByteBuffer packet = obj2 as ByteBuffer; connection.RawWrite(packet); } else { FluorineRtmpContext.Initialize(connection); this._rtmpHandler.MessageReceived(connection, obj2); } } catch (Exception exception) { log.Error(__Res.GetString("Rtmp_CouldNotProcessMessage"), exception); } } this.ReturnPendingMessages(connection, request); } } }
private void HandleIdle(RtmptRequest request) { RtmptConnection connection = GetConnection(request); if (connection == null) { HandleBadRequest(__Res.GetString(__Res.Rtmpt_UnknownClient, request.Url), request); return; } if (connection.IsClosing) { // Tell client to close the connection ReturnMessage(0, request); connection.RealClose(); return; } FluorineRtmpContext.Initialize(connection); ReturnPendingMessages(connection, request); }
private void OnReceivedCallback(object state) { _lock.AcquireReaderLock(); try { if (IsClosed || IsClosing || IsDisconnecting) { return; // Already shutting down. } } finally { _lock.ReleaseReaderLock(); } log4net.ThreadContext.Properties["ClientIP"] = this.RemoteEndPoint; if (log.IsDebugEnabled) { log.Debug(__Res.GetString(__Res.Rtmp_SocketReadProcessing, _connectionId)); } if (!IsTunnelingDetected) { IsTunnelingDetected = true; byte rtmpDetect = _readBuffer.Get(0); bool encrypted = (rtmpDetect == 0x06); //rtmpe? if (!encrypted) { SetIsTunneled(rtmpDetect != 0x3); } if (!IsTunneled) { //For tunneled connections we do not really need a session for this connection _session = _endpoint.GetMessageBroker().SessionManager.CreateSession(this); } } try { if (!IsTunneled) { FluorineRtmpContext.Initialize(this); #if !(NET_1_1) List <object> result = null; #else ArrayList result = null; #endif try { result = RtmpProtocolDecoder.DecodeBuffer(this.Context, _readBuffer); } catch (HandshakeFailedException hfe) { #if !SILVERLIGHT if (log.IsDebugEnabled) { log.Debug(string.Format("Handshake failed: {0}", hfe.Message)); } #endif // Clear buffer if something is wrong in protocol decoding. _readBuffer.Clear(); this.Close(); return; } catch (Exception ex) { // Catch any exception in the decoding then clear the buffer to eliminate memory leaks when we can't parse protocol // Also close Connection because we can't parse data from it #if !SILVERLIGHT log.Error("Error decoding buffer", ex); #endif // Clear buffer if something is wrong in protocol decoding. _readBuffer.Clear(); this.Close(); return; } if (result != null && result.Count > 0) { foreach (object obj in result) { if (log.IsDebugEnabled) { log.Debug(__Res.GetString(__Res.Rtmp_BeginHandlePacket, _connectionId)); } if (obj is ByteBuffer) { ByteBuffer buf = obj as ByteBuffer; Write(buf); } else if (obj is byte[]) { Write(obj as byte[]); } else { _rtmpServer.RtmpHandler.MessageReceived(this, obj); } if (log.IsDebugEnabled) { log.Debug(__Res.GetString(__Res.Rtmp_EndHandlePacket, _connectionId)); } } } } else { //FluorineRtmpContext.Initialize(this); RtmptRequest rtmptRequest = RtmptProtocolDecoder.DecodeBuffer(this, _readBuffer); if (rtmptRequest != null) { HandleRtmpt(rtmptRequest); } } } catch (Exception ex) { HandleError(ex); } //Ready to receive again BeginReceive(false); }
private void OnReceivedCallback(object state) { #if !SILVERLIGHT if (log.IsDebugEnabled) { log.Debug(__Res.GetString(__Res.Rtmp_SocketReadProcessing, _connectionId)); } if (log.IsDebugEnabled) { log.Debug("Begin handling packet " + ToString()); } #endif try { List <object> result; try { result = RtmpProtocolDecoder.DecodeBuffer(Context, _readBuffer); } catch (HandshakeFailedException hfe) { #if !SILVERLIGHT if (log.IsDebugEnabled) { log.Debug(string.Format("Handshake failed: {0}", hfe.Message)); } #endif // Clear buffer if something is wrong in protocol decoding. _readBuffer.Clear(); Close(); return; } catch (Exception ex) { // Catch any exception in the decoding then clear the buffer to eliminate memory leaks when we can't parse protocol // Also close Connection because we can't parse data from it #if !SILVERLIGHT log.Error("Error decoding buffer", ex); #endif // Clear buffer if something is wrong in protocol decoding. _readBuffer.Clear(); Close(); return; } if (Context.State == RtmpState.Handshake) { ByteBuffer resultBuffer = result[0] as ByteBuffer; //Handshake 3d phase if (resultBuffer != null) { resultBuffer.Skip(1); resultBuffer.Compact(); resultBuffer.Limit = RtmpProtocolDecoder.HandshakeSize; } ByteBuffer buffer = ByteBuffer.Allocate(RtmpProtocolDecoder.HandshakeSize); buffer.Put(resultBuffer); Write(buffer); Context.State = RtmpState.Connected; _handler.ConnectionOpened(this); } else { if (result != null && result.Count > 0) { foreach (object obj in result) { if (obj is ByteBuffer) { ByteBuffer buf = obj as ByteBuffer; Write(buf); } else { #if !SILVERLIGHT FluorineRtmpContext.Initialize(this); #endif _handler.MessageReceived(this, obj); } } } } } catch (Exception ex) { HandleError(ex); } #if !SILVERLIGHT if (log.IsDebugEnabled) { log.Debug("End handling packet " + ToString()); } #endif //Ready to receive again BeginReceive(false); }
public virtual void ConnectionClosed(RtmpConnection connection) { FluorineRtmpContext.Initialize(connection); connection.Close(); }