00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "vfer_accept_queue.h"
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 accept_queue* Accept_Queue_Create (int backlog) {
00045 accept_queue* ac;
00046
00047 DEBUG_PRINT(DEBUG_ACCEPTQ, "accept_queue.c", "Accept_Queue_Create", "Entered with backlog[%d]", backlog);
00048
00049 if (backlog <= 0) return NULL;
00050 if (! ALLOC(ac, accept_queue*, accept_queue, 1)) return NULL;
00051
00052 ac->count = 0;
00053 ac->backlog = backlog;
00054 ac->first = NULL;
00055 ac->last = NULL;
00056 return ac;
00057 }
00058
00059
00060
00061
00062
00063
00064 void Accept_Queue_Delete (accept_queue* ac) {
00065 queued_conn *c1, *c2;
00066 DEBUG_PRINT(DEBUG_ACCEPTQ, "accept_queue.c", "Accept_Queue_Delete", "Entered");
00067
00068 c1 = ac->first;
00069 while (c1 != NULL) {
00070 c2 = c1->next;
00071 RELEASE_PACK(c1->request);
00072 RELEASE(c1, sizeof(queued_conn));
00073 c1 = c2;
00074 }
00075 RELEASE (ac, sizeof(accept_queue));
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 int Accept_Queue_Enqueue (accept_queue* ac, int fd, struct sockaddr sa, packet* request)
00093 {
00094 queued_conn* qc;
00095 queued_conn* tmp;
00096
00097 DEBUG_PRINT(DEBUG_ACCEPTQ, "accept_queue.c", "Accept_Queue_Enqueue", "Enqueueing new connection fd[%d] into accept queue with size[%d]", fd, ac->count);
00098
00099
00100 if (ac->backlog == 0) {
00101 DEBUG_PRINT(DEBUG_ACCEPTQ, "accept_queue.c", "Accept_Queue_Enqueue", "Backlog=0 ; dropping conn fd[%d]", fd);
00102 return 0;
00103 }
00104
00105 if (! ALLOC(qc, queued_conn*, queued_conn, 1)) {
00106 return -1;
00107 }
00108
00109 qc->next = ac->first;
00110 qc->prev = NULL;
00111 qc->fd = fd;
00112 qc->sa = sa;
00113 qc->request = request;
00114
00115 if (ac->count == ac->backlog) {
00116
00117 if (ac->last == ac->first) {
00118
00119
00120 if (ac->first != NULL) {
00121 RELEASE_PACK(ac->last->request);
00122 RELEASE(ac->last, sizeof(struct queued_conn));
00123 }
00124 ac->first = ac->last = qc;
00125 } else {
00126 tmp = ac->last->prev;
00127 RELEASE_PACK(ac->last->request);
00128 RELEASE(ac->last, sizeof(struct queued_conn));
00129 ac->last = tmp;
00130 ac->last->next = NULL;
00131 ac->first->prev = qc;
00132 ac->first = qc;
00133 }
00134 } else {
00135
00136 if (ac->count == 0)
00137 ac->last = qc;
00138 else
00139 ac->first->prev = qc;
00140
00141 ac->first = qc;
00142 ac->count++;
00143 }
00144 DEBUG_PRINT(DEBUG_ACCEPTQ, "accept_queue.c", "Accept_Queue_Enqueue", "New accept queue size[%d]", ac->count);
00145 return 0;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 queued_conn* Accept_Queue_Dequeue (accept_queue* ac) {
00160 queued_conn* saved_top;
00161
00162 DEBUG_PRINT(DEBUG_ACCEPTQ, "accept_queue.c", "Accept_Queue_Dequeue", "Entered with accept queue size[%d]", ac->count);
00163 if (ac->count == 0) {
00164 return NULL;
00165 }
00166
00167 saved_top = ac->last;
00168
00169 if (ac->count == 1) {
00170 ac->last = ac->first = NULL;
00171 } else {
00172 ac->last = ac->last->prev;
00173 }
00174 ac->count--;
00175 return saved_top;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208