Пример #1
0
        private SipContext ConvertToContext(byte[] bytes, IPEndPoint from,  IPEndPoint to)
        {
            Datagram datagram = new Datagram(bytes, from , to);
            var message = new SipParser2(_messageFactory, _headerFactory).Parse(datagram);

            return CreateSipContext(message, datagram);
        }
Пример #2
0
        public SipMessage Parse(Datagram datagram)
        {
            Check.Require(datagram, "datagram");

            _logger.Trace("Parsing datagram...");

            new SipLexer().Lex(datagram.DataBytes, OnFirstLine, OnHeaderLine, OnBody);

            _logger.Trace("Datagram parsed succesfully.");

            return _message;
        }
Пример #3
0
        public SipContext CreateContext(Datagram datagram)
        {
            Check.Require(datagram, "datagram");

            _logger.Trace("Calling CreateContext...");

            var parserContext = new SipParserContext(_messageFactory, _headerFactory);
            SipContext context = null;
            parserContext.ParseCompleted += (s, e) => context = ParserContext_ParseCompleted(e, datagram);

            _logger.Trace("Calling Parse on parser context...");
            parserContext.Parse(datagram.DataBytes);
            return context;
        }
Пример #4
0
        private SipContext CreateSipContext(SipMessage message, Datagram datagram)
        {
            var request = message as SipRequest;
            var response = message as SipResponse;

            var c = new SipContext();

            c.Request = request;
            c.Response = response;
            c.RemoteEndPoint = datagram.RemoteEndPoint;
            c.LocalEndPoint = datagram.LocalEndPoint;

            return c;
        }
Пример #5
0
        SipContext ParserContext_ParseCompleted(ParseCompletedEventArgs e, Datagram datagram)
        {
            var c = new SipContext();

            _logger.Trace("Parse on parser context completed.");

            c.Request = e.Message as SipRequest;
            c.Response = e.Message as SipResponse;
            c.RemoteEndPoint = datagram.RemoteEndPoint;
            c.LocalEndPoint = datagram.LocalEndPoint;

            if (c.Request != null)
                _logger.Debug("Request Received '" + c.Request.RequestLine.Method + " " + c.Request.RequestLine.Uri.FormatToString() + "' from " +
                              datagram.RemoteEndPoint);

            if (c.Response != null)
                _logger.Debug("Response Received '" + c.Response.StatusLine.StatusCode + " " + c.Response.StatusLine.ReasonPhrase + "' from " +
                              datagram.RemoteEndPoint);
            return c;
        }
Пример #6
0
        private SipContext CreateSipContext(SipMessage message, Datagram datagram)
        {
            var request = message as SipRequest;
            var response = message as SipResponse;
            
            if (request != null)
            {
                _logger.Debug("Request Received '" + request.RequestLine.Method + " " + request.RequestLine.Uri.FormatToString() + "' from " +
                              datagram.RemoteEndPoint);
            }
            else
            {
                _logger.Debug("Response Received '" + response.StatusLine.StatusCode + " " + response.StatusLine.ReasonPhrase + "' from " +
                             datagram.RemoteEndPoint);
            }
               
            var c = new SipContext();

            _logger.Trace("Parse on parser context completed.");

            c.Request = request;
            c.Response = response;
            c.RemoteEndPoint = datagram.RemoteEndPoint;
            c.LocalEndPoint = datagram.LocalEndPoint;

            return c;
        }
Пример #7
0
        private void ReceiveIncomingDatagram()
        {
            var buffer = new byte[1024];

            while (_isRunning)
            {
                try
                {
                    if (_socket.Poll(0, SelectMode.SelectRead))
                    {
                        EndPoint remoteEp = new IPEndPoint(IPAddress.Any, 0);
                        int received = _socket.ReceiveFrom(buffer, ref remoteEp);

                        /*diagnostics*/
                        BytesReceived += received;
                        PacketsReceived++;

                        byte[] data = new byte[received];
                        Array.Copy(buffer, data, received);

                        var datagram = new Datagram(data, _ipEndPoint, (IPEndPoint) remoteEp);

                        _threadPool.QueueWorkItem(ProcessIncomingDatagram, datagram, OnProcessIncomingDatagramCompleted, CallToPostExecute.Always);
                        
                    }
                }
                catch (SocketException se)
                {
                    _logger.ErrorException(string.Format("Failed to receive message from socket. SocketErrorCode: {0}, ErrorCode: {1}", se.SocketErrorCode, se.ErrorCode), se);
                }
                catch (Exception e)
                {
                    _logger.ErrorException("Failed to receive message", e);
                }

                Thread.Sleep(10);
            }
        }