1 #include <proto/exec.h>
5 struct List active_sockets;
7 struct Socket *send_queue_head = NULL;
8 struct Socket *send_queue_tail = NULL;
10 static UBYTE next_stream_id = 1;
12 extern void NewList(struct List *l);
16 NewList(&active_sockets);
19 struct Socket *find_socket(void *sig_task, ULONG socket)
21 for (struct Node *node = active_sockets.lh_Head; node->ln_Succ != NULL; node = node->ln_Succ)
23 struct Socket *s = (struct Socket *)node;
24 if (s->sig_task == sig_task && s->socket == socket)
30 struct Socket *find_socket_by_stream_id(UBYTE stream_id)
32 for (struct Node *node = active_sockets.lh_Head; node->ln_Succ != NULL; node = node->ln_Succ)
34 struct Socket *s = (struct Socket *)node;
35 if (s->stream_id == stream_id)
41 static UBYTE allocate_stream_id()
43 // Bug: If all stream ids are allocated then this loop won't terminate.
47 UBYTE stream_id = next_stream_id;
49 if (find_socket_by_stream_id(stream_id) == NULL)
54 static void free_stream_id(UBYTE stream_id)
56 // Currently do nothing.
57 // Could speed up allocate_stream_id using a bitmap?
60 struct Socket *create_socket(struct Task *task, ULONG id)
62 struct Socket *s = (struct Socket *)AllocMem(sizeof(struct Socket), MEMF_CLEAR);
65 s->stream_id = allocate_stream_id();
66 AddTail(&active_sockets, (struct Node *)s);
70 void delete_socket(struct Socket *s)
72 Remove((struct Node *)s);
73 free_stream_id(s->stream_id);
74 FreeMem(s, sizeof(struct Socket));
77 void add_to_send_queue(struct Socket *s, UWORD required_length)
79 s->send_queue_required_length = required_length;
80 s->next_in_send_queue = NULL;
82 if (send_queue_head == NULL)
85 send_queue_tail->next_in_send_queue = s;
88 s->flags |= SOCKET_IN_SEND_QUEUE;
91 void remove_from_send_queue(struct Socket *s)
93 if (s->flags & SOCKET_IN_SEND_QUEUE)
95 if (send_queue_head == s)
97 send_queue_head = s->next_in_send_queue;
98 if (send_queue_head == NULL)
99 send_queue_tail = NULL;
103 struct Socket *curr = send_queue_head;
104 while (curr->next_in_send_queue != s)
105 curr = curr->next_in_send_queue;
107 curr->next_in_send_queue = s->next_in_send_queue;
108 if (send_queue_tail == s)
109 send_queue_tail = curr;
112 s->next_in_send_queue = NULL;
113 s->flags &= ~SOCKET_IN_SEND_QUEUE;