Example #1
0
        public override bool OnEvent(SocketAsyncEventArgs e)
        {
            e.ReturnPool();
            if (e.SocketError != SocketError.Success)
            {
                return(false);
            }
            var protocol = ProtocolFactoryManager.CreateProtocolChain(_protocolChain, _customParameters);

            if (protocol == null)
            {
                Logger.FATAL("Unable to create protocol chain");
                _closeSocket = true;
                return(false);
            }
            var tcpCarrier = new TCPCarrier(Socket)
            {
                Protocol = protocol.FarEndpoint
            };

            protocol.FarEndpoint.IOHandler = tcpCarrier;
            tcpCarrier.ReadEnabled         = true;
            if (!SignalProtocolCreated(protocol, _customParameters))
            {
                protocol.Dispose();
                _closeSocket = true;
                return(false);
            }
            _success     = true;
            _closeSocket = false;
            IOHandlerManager.EnqueueForDelete(this);
            return(true);
        }
Example #2
0
        public bool Accept(SocketAsyncEventArgs socketAsyncEvent)
        {
            if (!_enabled)
            {
                Logger.WARN("Acceptor is not enabled.");
                _droppedCount++;
                return(true);
            }
            Logger.INFO("Client connected:{0}:{1} -> {2}:{3}", (socketAsyncEvent.AcceptSocket.RemoteEndPoint as IPEndPoint).Address.ToString(), (socketAsyncEvent.AcceptSocket.RemoteEndPoint as IPEndPoint).Port, _ipAddress, _port);
            BaseProtocol pProtocol = ProtocolFactoryManager.CreateProtocolChain(_protocolChain, Parameters);

            if (pProtocol == null)
            {
                Logger.FATAL("Unable to create protocol chain");
                socketAsyncEvent.AcceptSocket.Close();
                return(false);
            }
            var pTcpCarrier = new TCPCarrier(socketAsyncEvent.AcceptSocket)
            {
                Protocol    = pProtocol.FarEndpoint,
                ReadEnabled = true
            };

            pProtocol.FarEndpoint.IOHandler = pTcpCarrier;

            //6. Register the protocol stack with an application
            if (Application != null)
            {
                pProtocol             = pProtocol.NearEndpoint;
                pProtocol.Application = Application;
            }

            //if (pProtocol.NearEndpoint.OutputBuffer != null)
            //    pProtocol.NearEndpoint.EnqueueForOutbound();

            if (AcceptEnabled)
            {
                socketAsyncEvent.AcceptSocket = null;
                try
                {
                    if (!Socket.AcceptAsync(socketAsyncEvent))
                    {
                        OnEvent(socketAsyncEvent);
                    }
                }
                catch (Exception ex)
                {
                    Logger.FATAL(ex.Message);
                    return(false);
                }
            }
            else
            {
                socketAsyncEvent.ReturnPool();
            }
            _acceptedCount++;


            return(true);
        }
Example #3
0
        bool BindAcceptor(Variant node)
        {
            //1. Get the chain
            var chain = ProtocolFactoryManager.ResolveProtocolChain(node[CONF_PROTOCOL]);

            if (chain.Count == 0)
            {
                WARN("Invalid protocol chain: {0}", node[CONF_PROTOCOL]);
            }
            //2. Is it TCP or UDP based?
            if (chain[0] == ProtocolTypes.PT_TCP)
            {
                //3. This is a tcp acceptor. Instantiate it and start accepting connections
                var pAcceptor = new TCPAcceptor(node[CONF_IP], node[CONF_PORT], node, chain);
                if (!pAcceptor.Bind())
                {
                    FATAL("Unable to fire up acceptor from this config node:{0}", node.ToString());
                    return(false);
                }
                acceptors.Add(pAcceptor);
                return(true);
            }
            else if (chain[0] == ProtocolTypes.PT_UDP)
            {
                //4. Ok, this is an UDP acceptor. Because of that, we can instantiate
                //the full stack. Get the stack first
                var pProtocol = ProtocolFactoryManager.CreateProtocolChain(chain, node);
                if (pProtocol == null)
                {
                    FATAL("Unable to instantiate protocol stack {0}", node[CONF_PROTOCOL]);
                    return(false);
                }
                //5. Create the carrier and bind it
                var pUDPCarrier = UDPCarrier.Create(node[CONF_IP], node[CONF_PORT], pProtocol);
                if (pUDPCarrier == null)
                {
                    FATAL("Unable to instantiate UDP carrier on {0}:{1}", node[CONF_IP],
                          node[CONF_PORT]);
                    pProtocol.EnqueueForDelete();
                    return(false);
                }
                pUDPCarrier.Parameters = node;
                acceptors.Add(pUDPCarrier);
                return(true);
            }
            else
            {
                FATAL("Invalid carrier type");
                return(false);
            }
        }
        public bool AddTrack(Variant track, bool isAduio)
        {
            var _track         = isAduio ? _audioTrack : _videoTrack;
            var _oppositeTrack = isAduio ? _videoTrack : _audioTrack;

            var rr = isAduio ? _audioRR : _videoRR;

            if (_track != null)
            {
                return(false);
            }
            var application = _rtsp.Application;

            if (application == null)
            {
                FATAL("RTSP protocol not yet assigned to an application");
                return(false);
            }
            if (isAduio)
            {
                _audioTrack = track;
            }
            else
            {
                _videoTrack = track;
            }
            _track = track;
            if (_oppositeTrack != null)
            {
                if (_oppositeTrack["isTcp"] != _track["isTcp"])
                {
                    return(false);
                }
            }
            _forceTcp = _track["isTcp"];
            var dummy = new Variant();
            var rtp   = (InboundRtpProtocol)ProtocolFactoryManager.CreateProtocolChain(Defines.CONF_PROTOCOL_INBOUND_UDP_RTP, dummy);

            if (rtp == null)
            {
                FATAL("Unable to create the protocol chain");
                Cleanup();
                return(false);
            }
            if (isAduio)
            {
                _rtpAudio = rtp;
            }
            else
            {
                _rtpVideo = rtp;
            }
            var rtcp = (RtcpProtocol)ProtocolFactoryManager.CreateProtocolChain(Defines.CONF_PROTOCOL_UDP_RTCP, dummy);

            if (rtcp == null)
            {
                FATAL("Unable to create the protocol chain");
                Cleanup();
                return(false);
            }
            if (isAduio)
            {
                _rtcpAudio = rtcp;
            }
            else
            {
                _rtcpVideo = rtcp;
            }
            if (_track["isTcp"])
            {
                var dataIdx = 0u;
                var rtcpIdx = 0u;
                if (_track["portsOrChannels", "data"] && _track["portsOrChannels", "rtcp"])
                {
                    dataIdx = _track["portsOrChannels", "data"];
                    rtcpIdx = _track["portsOrChannels", "rtcp"];
                }
                else
                {
                    dataIdx = (uint)(_track["globalTrackIndex"] * 2);
                    rtcpIdx = dataIdx + 1;
                }

                if ((dataIdx >= 256) || (rtcpIdx >= 256))
                {
                    FATAL("Invalid channel numbers");
                    return(false);
                }
                if (_protocols.ContainsKey(dataIdx) || _protocols.ContainsKey(rtcpIdx))
                {
                    FATAL("Invalid channel numbers");
                    return(false);
                }
                _protocols[dataIdx] = rtp;
                _protocols[rtcpIdx] = rtcp;
                rr.Write(8, rtp.SSRC);   //SSRC of packet sender
                rr.Write(40, rtcp.SSRC); //SSRC of packet sender

                rr[1] = (byte)rtcpIdx;
            }
            else
            {
                if (!CreateCarriers(rtp, rtcp))
                {
                    FATAL("Unable to create carriers");
                    return(false);
                }
            }
            rtp.Application  = application;
            rtcp.Application = application;
            return(true);
        }
        public bool InitializePorts(out Socket socket, out int port, ref NATTraversalProtocol ppNATData, out Socket rtcp, out int rtcpPort, ref NATTraversalProtocol ppNATRTCP)
        {
            UDPCarrier carrier1 = null;
            UDPCarrier carrier2 = null;

            for (int i = 0; i < 10; i++)
            {
                if (carrier1 != null)
                {
                    carrier1.Dispose();
                    carrier1 = null;
                }
                if (carrier2 != null)
                {
                    carrier2.Dispose();
                    carrier2 = null;
                }

                carrier1 = UDPCarrier.Create("0.0.0.0", 0);
                if (carrier1 == null)
                {
                    WARN("Unable to create UDP carrier for RTP");
                    continue;
                }

                carrier2 = (carrier1.NearPort % 2) == 0 ? UDPCarrier.Create("0.0.0.0", carrier1.NearPort + 1) : UDPCarrier.Create("0.0.0.0", carrier1.NearPort - 1);

                if (carrier2 == null)
                {
                    WARN("Unable to create UDP carrier for RTCP");
                    continue;
                }

                if (carrier1.NearPort > carrier2.NearPort)
                {
                    WARN("Switch carriers");
                    UDPCarrier pTemp = carrier1;
                    carrier1 = carrier2;
                    carrier2 = pTemp;
                }

                Variant dummy = Variant.Get();
                socket    = carrier1.Socket;
                port      = carrier1.NearPort;
                ppNATData = (NATTraversalProtocol)ProtocolFactoryManager.CreateProtocolChain(Defines.CONF_PROTOCOL_RTP_NAT_TRAVERSAL, dummy);
                if (ppNATData == null)
                {
                    rtcp     = null;
                    rtcpPort = 0;
                    FATAL("Unable to create the protocol chain {0}", Defines.CONF_PROTOCOL_RTP_NAT_TRAVERSAL);
                    return(false);
                }
                carrier1.Protocol = ppNATData.FarEndpoint;
                ppNATData.FarEndpoint.IOHandler = carrier1;
                rtcp      = carrier2.Socket;
                rtcpPort  = carrier2.NearPort;
                ppNATRTCP = (NATTraversalProtocol)ProtocolFactoryManager.CreateProtocolChain(Defines.CONF_PROTOCOL_RTP_NAT_TRAVERSAL, dummy);
                if (ppNATRTCP == null)
                {
                    FATAL("Unable to create the protocol chain {0}", Defines.CONF_PROTOCOL_RTP_NAT_TRAVERSAL);
                    ppNATData.EnqueueForDelete();
                    return(false);
                }
                carrier2.Protocol = ppNATRTCP.FarEndpoint;
                ppNATRTCP.FarEndpoint.IOHandler = carrier2;
                return(true);
            }
            if (carrier1 != null)
            {
                carrier1.Dispose();
                carrier1 = null;
            }
            if (carrier2 != null)
            {
                carrier2.Dispose();
                carrier2 = null;
            }
            socket   = null;
            port     = 0;
            rtcp     = null;
            rtcpPort = 0;
            return(false);
        }