Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Green Telnet


The send_data() Function

The send_data() function sends data over the wire to the client. Again, the server application itself never interacts directly with a socket, but rather writes data to an intermediate queue. When the client is awake, the send_data() function reads data from the intermediate queue and writes it to the client's socket. The send_data() function is in Listing Two. Lines 18 and 19 ensure that the client is still awake and awaits notification of the server application placing data in the queue. The condition on the loop breaks when the client_wants_ sleep flag is set to True via the signal handler that catches a notification from recv_data() of the client transitioning to a sleep state. Next, starting on line 22 we loop through the intermediate queue, sending each message placed into the queue by the server application to the client. Lines 25 through 29 mark the replay buffer used to sync any data that had its transmission interrupted by the client going to sleep. Lines 30 through 44 simply close the queue if the client goes to sleep prior to transmission of all data in the queue, while lines 46 through 48 close the queue upon successful transmission of all its data.


 1. void send_data(int s_client, int send_data_fd, struct replay_buffer *replay_buff) {
 2.   struct gqnode dequeued_data;
 3.   struct stat   queue_stats;
 4.   struct flock  fl;
 5.   char gt_packet[BUF_SIZE + 2];
 6.   char data_notifier;
 7.   char *p;
 8.   char c;
 9.   long queue_file_size;
10.   int temp = 0;
11.   int bytes_written = 0;
12.   int queue_fd;
13.   int status;
14.   int messages_written;
15.   int bytes_read = 0;
16.   int i = 0;
17.
18.   while (!client_wants_sleep &&
19.     read(send_data_fd, &data_notifier, sizeof(data_notifier)) > 0) {
20.     queue_fd = open (queue_file_path, O_RDWR);
21.     fl = acquire_queue_lock (queue_fd);
22.     while (read(queue_fd, &dequeued_data, sizeof(dequeued_data)) > 0) {
23.       if (!client_wants_sleep &&
24.        (bytes_written = write (s_client, dequeued_data.buf,               dequeued_data.nbytes)) > 0);
25.       replay_buff->byte_count = replay_buff->byte_count + bytes_written;
26.       for (i = 0; i < bytes_written; i++) {
27.         replay_buff->buffer[replay_buff->index] = dequeued_data.buf[i];
28.         replay_buff->index = (replay_buff->index + 1) % RB_MAX;
29.       }
30.       if (client_wants_sleep) {
31.         status = fstat (queue_fd, &queue_stats);
32.         queue_file_size = queue_stats.st_size;
33.         temp_queue_fd = open (queue_file_path, O_RDWR);
34.         messages_written = 0;
35.         while (read(queue_fd, &dequeued_data, sizeof(dequeued_data)) > 0) {
36.           write(temp_queue_fd, &dequeued_data, sizeof(dequeued_data));
37.           messages_written++;
38.         }
39.         close(temp_queue_fd);
40.         ftruncate(queue_fd, messages_written*sizeof(dequeued_data));
41.         release_queue_lock(queue_fd, fl);
42.         close(queue_fd);
43.         return;
44.       }
45.     }
46.     ftruncate(queue_fd, 0);
47.     release_queue_lock(queue_fd, fl);
48.     close(queue_fd);
49.   }
50.   return;
51. }
Listing Two


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.