private void ReturnMessage(RtmptConnection connection, ByteBuffer data, RtmptRequest request) { ByteBuffer buffer = ByteBuffer.Allocate((int)data.Length + 30); StreamWriter sw = new StreamWriter(buffer); int contentLength = data.Limit + 1; if (request.HttpVersion == 1) { sw.Write("HTTP/1.1 200 OK\r\n"); sw.Write("Cache-Control: no-cache\r\n"); } else { sw.Write("HTTP/1.0 200 OK\r\n"); sw.Write("Pragma: no-cache\r\n"); } sw.Write(string.Format("Content-Length: {0}\r\n", contentLength)); sw.Write("Connection: Keep-Alive\r\n"); sw.Write(string.Format("Content-Type: {0}\r\n", ContentType.RTMPT)); sw.Write("\r\n"); sw.Write((char)connection.PollingDelay); sw.Flush(); BinaryWriter bw = new BinaryWriter(buffer); byte[] buf = data.ToArray(); bw.Write(buf); bw.Flush(); request.Connection.Write(buffer); }
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); }
private void ReturnMessage(RtmptConnection connection, ByteBuffer data, HttpResponse response) { response.StatusCode = 200; response.ClearHeaders(); response.AppendHeader("Connection", "Keep-Alive"); int contentLength = data.Limit + 1; response.AppendHeader("Content-Length", contentLength.ToString()); response.Cache.SetCacheability(HttpCacheability.NoCache); response.ContentType = ContentType.RTMPT; response.Write((char)connection.PollingDelay); byte[] buffer = data.ToArray(); response.OutputStream.Write(buffer, 0, buffer.Length); response.Flush(); }
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 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 ReturnPendingMessages(RtmptConnection connection, RtmptRequest request) { ByteBuffer data = connection.GetPendingMessages(RtmptConnection.RESPONSE_TARGET_SIZE); if (data == null) { // no more messages to send... if (connection.IsClosing) { // Tell client to close connection ReturnMessage(0, request); } else { ReturnMessage(connection.PollingDelay, request); } return; } ReturnMessage(connection, data, request); }
private void ReturnPendingMessages(RtmptConnection connection, RtmptRequest request) { ByteBuffer data = connection.GetPendingMessages(RtmptConnection.RESPONSE_TARGET_SIZE); if (data == null) { // no more messages to send... if (connection.IsClosing) { // Tell client to close connection ReturnMessage(0, request); } else ReturnMessage(connection.PollingDelay, request); return; } ReturnMessage(connection, data, request); }
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); }