public static ByteBuffer ReturnMessage(byte pollingDelay, 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)pollingDelay); sw.Flush(); BinaryWriter bw = new BinaryWriter(buffer); byte[] buf = data.ToArray(); bw.Write(buf); bw.Flush(); return(buffer); }
private void HandleRtmpt() { if (this._rtmptRequest == null) { string str5; BufferStreamReader reader = new BufferStreamReader(this._readBuffer); string[] strArray = reader.ReadLine().Split(new char[] { ' ' }); string httpMethod = strArray[0]; string pattern = strArray[1]; int startIndex = 0; while ((startIndex = pattern.IndexOf("%", startIndex)) != -1) { pattern = pattern.Substring(0, startIndex) + Uri.HexUnescape(pattern, ref startIndex) + pattern.Substring(startIndex); } string protocol = strArray[2]; Hashtable headers = new Hashtable(); string str6 = null; while (((str5 = reader.ReadLine()) != null) && (str5 != string.Empty)) { if ((str6 != null) && char.IsWhiteSpace(str5[0])) { Hashtable hashtable2; object obj2; (hashtable2 = headers)[obj2 = str6] = hashtable2[obj2] + str5; } else { int index = str5.IndexOf(":"); if (index != -1) { str6 = str5.Substring(0, index); string str7 = str5.Substring(index + 1).Trim(); headers[str6] = str7; } else { break; } } } this._rtmptRequest = new RtmptRequest(this, pattern, protocol, httpMethod, headers); } if (this._readBuffer.Remaining == this._rtmptRequest.ContentLength) { RtmptEndpoint endpoint = this.Endpoint.GetMessageBroker().GetEndpoint("__@fluorinertmpt") as RtmptEndpoint; if (endpoint != null) { this._readBuffer.Compact(); this._rtmptRequest.Data = this._readBuffer; this._readBuffer = ByteBuffer.Allocate(0x1000); this._readBuffer.Flip(); endpoint.Service(this._rtmptRequest); this._rtmptRequest = null; } } }
private void HandleRtmpt(RtmptRequest rtmptRequest) { IEndpoint endpoint = this.Endpoint.GetMessageBroker().GetEndpoint(RtmptEndpoint.FluorineRtmptEndpointId); RtmptEndpoint rtmptEndpoint = endpoint as RtmptEndpoint; if (rtmptEndpoint != null) { rtmptEndpoint.Service(rtmptRequest); } }
public static ByteBuffer ReturnMessage(byte message, RtmptRequest request) { ByteBuffer buffer = ByteBuffer.Allocate(100); StreamWriter sw = new StreamWriter(buffer); 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("Content-Length: 1\r\n"); 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)message); sw.Flush(); return(buffer); }
public static ByteBuffer HandleBadRequest(string message, RtmptRequest request) { ByteBuffer buffer = ByteBuffer.Allocate(100); StreamWriter sw = new StreamWriter(buffer); if (request.HttpVersion == 1) { sw.Write("HTTP/1.1 400 " + message + "\r\n"); sw.Write("Cache-Control: no-cache\r\n"); } else { sw.Write("HTTP/1.0 400 " + message + "\r\n"); sw.Write("Pragma: no-cache\r\n"); } sw.Write("Content-Type: text/plain\r\n"); sw.Write("Content-Length: " + message.Length.ToString() + "\r\n"); sw.Write("Connection: Keep-Alive\r\n"); sw.Write("\r\n"); sw.Write(message); sw.Flush(); return(buffer); }
public void Service(RtmptRequest rtmptRequest) { _rtmptServer.Service(rtmptRequest); }
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); }
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); }