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); }
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; }
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; }
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; }
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; }
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; }
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); } }