main_loop.xml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
  4. <section id="main_loop" xmlns:xi="http://www.w3.org/2001/XInclude">
  5. <sectioninfo>
  6. <revhistory>
  7. <revision>
  8. <revnumber>$Revision$</revnumber>
  9. <date>$Date$</date>
  10. </revision>
  11. </revhistory>
  12. </sectioninfo>
  13. <title>Main Loop</title>
  14. <para>
  15. Upon startup, all children execute <function>recvfrom</function>
  16. function. The process will enter the kernel mode. When there is no data
  17. to be processed at the moment, the kernel will put the process on list
  18. of processes waiting for data and the process will be put asleep.
  19. </para>
  20. <para>
  21. When data to be processed was received, the first process on the list
  22. will be removed from the list and woken up. After the process finished
  23. processing of the data, it will call <function>recvfrom</function>
  24. again and will be put by the kernel at the end of the list.
  25. </para>
  26. <para>
  27. When next data arrives, the first process on the list will be removed,
  28. processes the data and will be put on the end of the list again. And so
  29. on...
  30. </para>
  31. <para>
  32. The main loop logic can be found in function
  33. <function>udp_rcv_loop</function> in file
  34. <filename>udp_server.c</filename>.
  35. </para>
  36. <para>
  37. The message is received using <function>recvfrom</function>
  38. function. The received data is stored in buffer and zero terminated.
  39. </para>
  40. <para>
  41. If configured so, basic sanity checks over the received message will be
  42. performed.
  43. </para>
  44. <para>
  45. The message is then processed by <function>receive_msg</function>
  46. function and <function>recvfrom</function> is called again.
  47. </para>
  48. <section id="receive_msg">
  49. <title><function>receive_msg</function> Function</title>
  50. <para>
  51. The function can be found in <filename>receive.c</filename> file.
  52. </para>
  53. <itemizedlist>
  54. <listitem>
  55. <para>
  56. In the server, a request or response is represented by
  57. <structname>sip_msg</structname> structure. The structure
  58. is allocated in this function. The original message is
  59. stored in <structfield>buf</structfield> attribute of the
  60. structure and is zero terminated. Then, another copy of the
  61. received message will be created and the copy will be
  62. stored in <structfield>orig</structfield> field. The
  63. original copy will be not modified during the server
  64. operation. All changes will be made to the copy in
  65. <structfield>buf</structfield> field. The second copy of
  66. the message will be removed in the future.
  67. </para>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. The message will be parsed (function
  72. <function>parse_msg</function>). We don't need the whole
  73. message header to be parsed at this stage. Only the first
  74. line and first Via header need to be parsed. The server
  75. needs to know if the message is request or response - hence
  76. the first line. The server also needs the first Via to be
  77. able to add its own Via - hence the first Via. Nothing else
  78. will be parsed at the moment - this saves time. (Message
  79. parser as well as <structname>sip_msg</structname>
  80. structure will be described later).
  81. </para>
  82. </listitem>
  83. <listitem>
  84. <para>
  85. A module may register callbacks. Each callback have
  86. associated an event, that will trigger the callback. One
  87. such callback is <emphasis>pre-script</emphasis>
  88. callback. Such callback will be called immediately before
  89. the routing part of the config file will be executed. If
  90. there are such callbacks registered, they will be executed
  91. now.
  92. </para>
  93. </listitem>
  94. <listitem>
  95. <para>
  96. As the next step we will determine type of the message. If
  97. the message being processed is a REQUEST then basic sanity
  98. checks will be performed (make sure that there is the first
  99. Via and parsing was successful) and the message will be
  100. passed to routing engine. The routing engine is one of the
  101. most complicated parts of the server and will be in detail
  102. described in chapter <link linkend="routing_engine">The
  103. Routing Engine</link>.
  104. </para>
  105. </listitem>
  106. <listitem>
  107. <para>
  108. If the message is a RESPONSE, it will be simply forwarded
  109. to its destination.
  110. </para>
  111. </listitem>
  112. <listitem>
  113. <para>
  114. After all, <emphasis>post-script</emphasis> callbacks will
  115. be executed if any and the structure representing the
  116. message will be released.
  117. </para>
  118. </listitem>
  119. <listitem>
  120. <para>
  121. Processing of the message is done now and the process is ready for another
  122. <acronym>SIP</acronym> message.
  123. </para>
  124. </listitem>
  125. </itemizedlist>
  126. </section> <!-- recv-message -->
  127. </section>