-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_socket_server.c
708 lines (612 loc) · 23.6 KB
/
simple_socket_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//Lab 7
// Authors : Soumitro, Auddy UID - 1000798164
// M. S. Raghavendra Sriram UID - 1000854840
/******************************************************************************
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
* All rights reserved. All use of this software and documentation is *
* subject to the License Agreement located at the end of this file below. *
*******************************************************************************
* Date - October 24, 2006 *
* Module - simple_socket_server.c *
* *
******************************************************************************/
/******************************************************************************
* Simple Socket Server (SSS) example.
*
* This example demonstrates the use of MicroC/OS-II running on NIOS II.
* In addition it is to serve as a good starting point for designs using
* MicroC/OS-II and Altera NicheStack TCP/IP Stack - NIOS II Edition.
*
* -Known Issues
* None.
*
* Please refer to the Altera NicheStack Tutorial documentation for details on this
* software example, as well as details on how to configure the NicheStack TCP/IP
* networking stack and MicroC/OS-II Real-Time Operating System.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* MicroC/OS-II definitions */
#include "includes.h"
/* Simple Socket Server definitions */
#include "simple_socket_server.h"
#include "alt_error_handler.h"
/* Nichestack definitions */
#include "ipport.h"
#include "tcpport.h"
/* LCD Specific Headers */
#include "system.h"
#include "alt_types.h"
#include "altera_avalon_timer_regs.h"
#include "altera_avalon_pio_regs.h"
#include "altera_avalon_lcd_16207_regs.h"
/* User Message */
char userMessage[50] = {};
/* CLient and Server Addresses */
char server[14] = {};
char client[14] = {};
/* Flags */
int displayFlag = 0;
int LCDFlag = 0;
int runFlag = 0;
int userMsgFlag = 0;
/* Switch specific variables */
int swDecC = 0, swDecP = 0;
char switchMessage[18][33] = {};
int switchCurrent[18] = {}, switchPrev[18] = {};
/* Function to display messages to the LCD.
* The function receives a string, which is then displayed on the first line
* of the LCD display on the board. */
void LCD_DisplayFunc(char *message){
printf("\nEnter LCD Display\n");
int i;
IOWR( LCD_BASE, LCD_WR_COMMAND_REG, 0x80 );
for(i = -1; i < 32; i++){ //Probable error here//
if(i >= strlen(message)){
IOWR( LCD_BASE, LCD_WR_DATA_REG, "" );
}
else{
IOWR( LCD_BASE, LCD_WR_DATA_REG, message[i] );
}
usleep(100);
}
printf("\nExit LCD Display\n");
}
void dec2bin(int *array,int decNum){
int i = 0;
while(i < 18){
array[i] = decNum % 2;
decNum /= 2;
i++;
}
}
void messageCompare(int *previous, int *current){
int i;
printf("\nEnters swMsgSubSystem()\n");
for(i = 0; i < 18; i++){
if(previous[i] != current[i] && current[i] != 0){
printf("\nMessage for switch#%d is = %s\n",switchMessage[i]);
LCD_DisplayFunc(switchMessage[i]);
break;
}
}
}
/* Switch message subsystem */
void swMsgSubSystem(){
// printf("Switch message subsystem begins....\n");
int i;
while(1){
if(displayFlag == 1){
swDecC = IORD_ALTERA_AVALON_PIO_DATA( SW_BASE );
printf("\nSwitch value = %d\n",swDecC);
if(swDecC != swDecP){
dec2bin(switchCurrent,swDecC);
messageCompare(switchPrev,switchCurrent);
memcpy(&switchPrev,&switchCurrent,sizeof(switchCurrent));
swDecP = swDecC;
}
}
}
}
/*
* Global handles (pointers) to our MicroC/OS-II resources. All of resources
* beginning with "SSS" are declared and created in this file.
*/
/*
* This SSSLEDCommandQ MicroC/OS-II message queue will be used to communicate
* between the simple socket server task and Nios Development Board LED control
* tasks.
*
* Handle to our MicroC/OS-II Command Queue and variable definitions related to
* the Q for sending commands received on the TCP-IP socket from the
* SSSSimpleSocketServerTask to the LEDManagementTask.
*/
OS_EVENT *SSSLEDCommandQ;
#define SSS_LED_COMMAND_Q_SIZE 30 /* Message capacity of SSSLEDCommandQ */
void *SSSLEDCommandQTbl[SSS_LED_COMMAND_Q_SIZE]; /*Storage for SSSLEDCommandQ*/
/*
* Handle to our MicroC/OS-II LED Event Flag. Each flag corresponds to one of
* the LEDs on the Nios Development board, D0 - D7.
*/
OS_FLAG_GRP *SSSLEDEventFlag;
/*
* Handle to our MicroC/OS-II LED Lightshow Semaphore. The semaphore is checked
* by the LED7SegLightshowTask each time it updates 7 segment LED displays,
* U8 and U9. The LEDManagementTask grabs the semaphore away from the lightshow task to
* toggle the lightshow off, and gives up the semaphore to turn the lightshow
* back on. The LEDManagementTask does this in response to the CMD_LEDS_LIGHTSHOW
* command sent from the SSSSimpleSocketServerTask when the user sends a toggle
* lightshow command over the TCPIP socket.
*/
OS_EVENT *SSSLEDLightshowSem;
/* Definition of Task Stacks for tasks not invoked by TK_NEWTASK
* (do not use NicheStack)
*/
OS_STK LEDManagementTaskStk[TASK_STACKSIZE];
OS_STK LED7SegLightshowTaskStk[TASK_STACKSIZE];
OS_STK LCDDisplayTaskStk[TASK_STACKSIZE];
/*
* Create our MicroC/OS-II resources. All of the resources beginning with
* "SSS" are declared in this file, and created in this function.
*/
void SSSCreateOSDataStructs(void)
{
INT8U error_code;
/*
* Create the resource for our MicroC/OS-II Queue for sending commands
* received on the TCP/IP socket from the SSSSimpleSocketServerTask()
* to the LEDManagementTask().
*/
SSSLEDCommandQ = OSQCreate(&SSSLEDCommandQTbl[0], SSS_LED_COMMAND_Q_SIZE);
if (!SSSLEDCommandQ)
{
alt_uCOSIIErrorHandler(EXPANDED_DIAGNOSIS_CODE,
"Failed to create SSSLEDCommandQ.\n");
}
/* Create our MicroC/OS-II LED Lightshow Semaphore. The semaphore is checked
* by the SSSLEDLightshowTask each time it updates 7 segment LED displays,
* U8 and U9. The LEDTask grabs the semaphore away from the lightshow task to
* toggle the lightshow off, and gives up the semaphore to turn the lightshow
* back on. The LEDTask does this in response to the CMD_LEDS_LIGHTSHOW
* command sent from the SSSSimpleSocketServerTask when the user sends the
* toggle lightshow command over the TCPIP socket.
*/
SSSLEDLightshowSem = OSSemCreate(1);
if (!SSSLEDLightshowSem)
{
alt_uCOSIIErrorHandler(EXPANDED_DIAGNOSIS_CODE,
"Failed to create SSSLEDLightshowSem.\n");
}
/*
* Create our MicroC/OS-II LED Event Flag. Each flag corresponds to one of
* the LEDs on the Nios Development board, D0 - D7.
*/
SSSLEDEventFlag = OSFlagCreate(0, &error_code);
if (!SSSLEDEventFlag)
{
alt_uCOSIIErrorHandler(error_code, 0);
}
}
/* This function creates tasks used in this example which do not use sockets.
* Tasks which use Interniche sockets must be created with TK_NEWTASK.
*/
void SSSCreateTasks(void)
{
INT8U error_code;
error_code = OSTaskCreateExt(LED7SegLightshowTask,
NULL,
(void *)&LED7SegLightshowTaskStk[TASK_STACKSIZE-1],
LED_7SEG_LIGHTSHOW_TASK_PRIORITY,
LED_7SEG_LIGHTSHOW_TASK_PRIORITY,
LED7SegLightshowTaskStk,
TASK_STACKSIZE,
NULL,
0);
alt_uCOSIIErrorHandler(error_code, 0);
error_code = OSTaskCreateExt(LEDManagementTask,
NULL,
(void *)&LEDManagementTaskStk[TASK_STACKSIZE-1],
LED_MANAGEMENT_TASK_PRIORITY,
LED_MANAGEMENT_TASK_PRIORITY,
LEDManagementTaskStk,
TASK_STACKSIZE,
NULL,
0);
alt_uCOSIIErrorHandler(error_code, 0);
error_code = OSTaskCreateExt(swMsgSubSystem,
NULL,
(void *)&LCDDisplayTaskStk[TASK_STACKSIZE-1],
LCD_MANAGEMENT_TASK_PRIORITY,
LCD_MANAGEMENT_TASK_PRIORITY,
LCDDisplayTaskStk,
TASK_STACKSIZE,
NULL,
0);
alt_uCOSIIErrorHandler(error_code, 0);
}
/*
* sss_reset_connection()
*
* This routine will, when called, reset our SSSConn struct's members
* to a reliable initial state. Note that we set our socket (FD) number to
* -1 to easily determine whether the connection is in a "reset, ready to go"
* state.
*/
void sss_reset_connection(SSSConn* conn)
{
memset(conn, 0, sizeof(SSSConn));
conn->fd = -1;
conn->state = READY;
conn->rx_wr_pos = conn->rx_buffer;
conn->rx_rd_pos = conn->rx_buffer;
return;
}
/*
* sss_send_menu()
*
* This routine will transmit the menu out to the telent client.
*/
void sss_send_menu(SSSConn* conn)
{
alt_u8 tx_buf[SSS_TX_BUF_SIZE];
alt_u8 *tx_wr_pos = tx_buf;
tx_wr_pos += sprintf(tx_wr_pos,"=================================\n\r");
tx_wr_pos += sprintf(tx_wr_pos,"Nios II Simple Socket Server Menu\n\r");
tx_wr_pos += sprintf(tx_wr_pos,"=================================\n\r");
tx_wr_pos += sprintf(tx_wr_pos,"0-7: Toggle board LEDs D0 - D7\n\r");
tx_wr_pos += sprintf(tx_wr_pos,"S: 7-Segment LED Light Show\n\r");
tx_wr_pos += sprintf(tx_wr_pos,"Q: Terminate session\n\r");
tx_wr_pos += sprintf(tx_wr_pos,"=================================\n\r");
tx_wr_pos += sprintf(tx_wr_pos,"Enter your choice & press return:\n\r");
send(conn->fd, tx_buf, tx_wr_pos - tx_buf, 0);
return;
}
/*
* sss_handle_accept()
*
* This routine is called when ever our listening socket has an incoming
* connection request. Since this example has only data transfer socket,
* we just look at it to see whether its in use... if so, we accept the
* connection request and call the telent_send_menu() routine to transmit
* instructions to the user. Otherwise, the connection is already in use;
* reject the incoming request by immediately closing the new socket.
*
* We'll also print out the client's IP address.
*/
void sss_handle_accept(int listen_socket, SSSConn* conn)
{
int socket, len;
struct sockaddr_in incoming_addr;
len = sizeof(incoming_addr);
if ((conn)->fd == -1)
{
if((socket=accept(listen_socket,(struct sockaddr*)&incoming_addr,&len))<0)
{
alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE,
"[sss_handle_accept] accept failed");
}
else
{
(conn)->fd = socket;
sss_send_menu(conn);
printf("[sss_handle_accept] accepted connection request from %s\n",
inet_ntoa(incoming_addr.sin_addr));
strcpy(client,inet_ntoa(incoming_addr.sin_addr));
printf("\nAddress = %s\n",client);
sprintf(client,"%d",inet_ntoa(incoming_addr.sin_addr)); // Client Address //
send(conn->fd,client,sizeof(client),0);
// sprintf(client,"%d",inet_ntoa(incoming_addr.sin_addr)); // Client Address //
// printf("\nAddress = %s of size = %d\n",inet_ntoa(incoming_addr.sin_addr),sizeof(inet_ntoa(incoming_addr.sin_addr)));
}
}
else
{
printf("[sss_handle_accept] rejected connection request from %s\n",
inet_ntoa(incoming_addr.sin_addr));
}
return;
}
/*
* sss_exec_command()
*
* This routine is called whenever we have new, valid receive data from our
* sss connection. It will parse through the data simply looking for valid
* commands to the sss server.
*
* Incoming commands to talk to the board LEDs are handled by sending the
* MicroC/OS-II SSSLedCommandQ a pointer to the value we received.
*
* If the user wishes to quit, we set the "close" member of our SSSConn
* struct, which will be looked at back in sss_handle_receive() when it
* comes time to see whether to close the connection or not.
*/
void sss_exec_command(SSSConn* conn)
{
int bytes_to_process = conn->rx_wr_pos - conn->rx_rd_pos;
INT8U tx_buf[SSS_TX_BUF_SIZE];
INT8U *tx_wr_pos = tx_buf;
INT8U error_code;
/*
* "SSSCommand" is declared static so that the data will reside
* in the BSS segment. This is done because a pointer to the data in
* SSSCommand
* will be passed via SSSLedCommandQ to the LEDManagementTask.
* Therefore SSSCommand cannot be placed on the stack of the
* SSSSimpleSocketServerTask, since the LEDManagementTask does not
* have access to the stack of the SSSSimpleSocketServerTask.
*/
static INT32U SSSCommand;
SSSCommand = CMD_LEDS_BIT_0_TOGGLE;
while(bytes_to_process--)
{
SSSCommand = toupper(*(conn->rx_rd_pos++));
if(SSSCommand >= ' ' && SSSCommand <= '~')
{
tx_wr_pos += sprintf(tx_wr_pos,
"--> Simple Socket Server Command %c.\n",
(char)SSSCommand);
if (SSSCommand == CMD_QUIT)
{
tx_wr_pos += sprintf(tx_wr_pos,"Terminating connection.\n\n\r");
conn->close = 1;
}
else
{
error_code = OSQPost(SSSLEDCommandQ, (void *)SSSCommand);
alt_SSSErrorHandler(error_code, 0);
}
}
}
// send(conn->fd, tx_buf, tx_wr_pos - tx_buf, 0);
return;
}
/*
* sss_handle_receive()
*
* This routine is called whenever there is a sss connection established and
* the socket associated with that connection has incoming data. We will first
* look for a newline "\n" character to see if the user has entered something
* and pressed 'return'. If there is no newline in the buffer, we'll attempt
* to receive data from the listening socket until there is.
*
* The connection will remain open until the user enters "Q\n" or "q\n", as
* deterimined by repeatedly calling recv(), and once a newline is found,
* calling sss_exec_command(), which will determine whether the quit
* command was received.
*
* Finally, each time we receive data we must manage our receive-side buffer.
* New data is received from the sss socket onto the head of the buffer,
* and popped off from the beginning of the buffer with the
* sss_exec_command() routine. Aside from these, we must move incoming
* (un-processed) data to buffer start as appropriate and keep track of
* associated pointers.
*/
void sss_handle_receive(SSSConn* conn)
{
send(conn->fd, "Connected....", sizeof("Connected...."), 0);
int data_used = 0;
int swnumFlag = 0, swmsgFlag = 0;
int sw;
char input[1500];
conn->rx_rd_pos = conn->rx_buffer;
conn->rx_wr_pos = conn->rx_buffer;
printf("[sss_handle_receive] processing RX data\n");
while(conn->state != CLOSE)
{
send(conn->fd,"Hello\n",30,0);
printf("\nEntered while after processing RX data\n");
recv(conn->fd,input,sizeof(input),0);
printf("\n%s\n",input);
if(strcmp(input,"run") == 0 && runFlag != 1){
printf("\nI RUN!!\n");
runFlag = 1;
}
else if(strcmp(input,"stop") == 0){
printf("\nI STOPPED :(\n");
runFlag = 0; displayFlag = 0;
}
else if(strcmp(input,"display") == 0 && runFlag == 1){
displayFlag = 1;
printf("DispFlag = %d\n",displayFlag);
}
else if(strcmp(input,"config") == 0 && runFlag != 1){
printf("\nConfiguring Switch...\n");
swnumFlag = 1;
}
else if(swnumFlag == 1){
sw = atoi(input);
printf("\nConfiguring Switch number#%d...\n",sw);
swnumFlag = 0; swmsgFlag = 1;
}
else if(swmsgFlag == 1){
printf("\nMessage received for Switch number#%d...\n",sw);
strcpy(switchMessage[sw],input);
switchMessage[sw][32] = '\0';
printf("Entered message = %s\n",switchMessage[sw]);
swmsgFlag = 0;
}
else if(strcmp(input,"get") == 0){
printf("\nSending Client address...\n");
send(conn->fd, client, sizeof(client), 0);
}
else if(strcmp(input,"send") == 0){
printf("\nAccepting Client's string....\n");
userMsgFlag = 1;
}
else if(userMsgFlag == 1){
printf("\nSending Client's string back to Client\n");
send(conn->fd, userMessage, sizeof(userMessage), 0);
userMsgFlag = 0;
}
/*
* When the quit command is received, update our connection state so that
* we can exit the while() loop and close the connection
*/
conn->state = conn->close ? CLOSE : READY;
/* Manage buffer */
data_used = conn->rx_rd_pos - conn->rx_buffer;
memmove(conn->rx_buffer, conn->rx_rd_pos,
conn->rx_wr_pos - conn->rx_rd_pos);
conn->rx_rd_pos = conn->rx_buffer;
conn->rx_wr_pos -= data_used;
memset(conn->rx_wr_pos, 0, data_used);
}
printf("[sss_handle_receive] closing connection\n");
close(conn->fd);
sss_reset_connection(conn);
return;
}
/*
* SSSSimpleSocketServerTask()
*
* This MicroC/OS-II thread spins forever after first establishing a listening
* socket for our sss connection, binding it, and listening. Once setup,
* it perpetually waits for incoming data to either the listening socket, or
* (if a connection is active), the sss data socket. When data arrives,
* the approrpriate routine is called to either accept/reject a connection
* request, or process incoming data.
*/
void SSSSimpleSocketServerTask()
{
int fd_listen, max_socket;
struct sockaddr_in addr;
static SSSConn conn;
fd_set readfds;
/*
* Sockets primer...
* The socket() call creates an endpoint for TCP of UDP communication. It
* returns a descriptor (similar to a file descriptor) that we call fd_listen,
* or, "the socket we're listening on for connection requests" in our sss
* server example.
*
* Traditionally, in the Sockets API, PF_INET and AF_INET is used for the
* protocol and address families respectively. However, there is usually only
* 1 address per protocol family. Thus PF_INET and AF_INET can be interchanged.
* In the case of NicheStack, only the use of AF_INET is supported.
* PF_INET is not supported in NicheStack.
*/
if ((fd_listen = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE,"[sss_task] Socket creation failed");
}
/*
* Sockets primer, continued...
* Calling bind() associates a socket created with socket() to a particular IP
* port and incoming address. In this case we're binding to SSS_PORT and to
* INADDR_ANY address (allowing anyone to connect to us. Bind may fail for
* various reasons, but the most common is that some other socket is bound to
* the port we're requesting.
*/
addr.sin_family = AF_INET;
addr.sin_port = htons(SSS_PORT);
addr.sin_addr.s_addr = INADDR_ANY;
if ((bind(fd_listen,(struct sockaddr *)&addr,sizeof(addr))) < 0)
{
alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE,"[sss_task] Bind failed");
}
/*
* Sockets primer, continued...
* The listen socket is a socket which is waiting for incoming connections.
* This call to listen will block (i.e. not return) until someone tries to
* connect to this port.
*/
if ((listen(fd_listen,1)) < 0)
{
alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE,"[sss_task] Listen failed");
}
/* At this point we have successfully created a socket which is listening
* on SSS_PORT for connection requests from any remote address.
*/
sss_reset_connection(&conn);
printf("[sss_task] Simple Socket Server listening on port %d\n", SSS_PORT);
while(1)
{
/*
* For those not familiar with sockets programming...
* The select() call below basically tells the TCPIP stack to return
* from this call when any of the events I have expressed an interest
* in happen (it blocks until our call to select() is satisfied).
*
* In the call below we're only interested in either someone trying to
* connect to us, or data being available to read on a socket, both of
* these are a read event as far as select is called.
*
* The sockets we're interested in are passed in in the readfds
* parameter, the format of the readfds is implementation dependant
* Hence there are standard MACROs for setting/reading the values:
*
* FD_ZERO - Zero's out the sockets we're interested in
* FD_SET - Adds a socket to those we're interested in
* FD_ISSET - Tests whether the chosen socket is set
*/
FD_ZERO(&readfds);
FD_SET(fd_listen, &readfds);
max_socket = fd_listen+1;
if (conn.fd != -1)
{
FD_SET(conn.fd, &readfds);
if (max_socket <= conn.fd)
{
max_socket = conn.fd+1;
}
}
select(max_socket, &readfds, NULL, NULL, NULL);
/*
* If fd_listen (the listening socket we originally created in this thread
* is "set" in readfs, then we have an incoming connection request. We'll
* call a routine to explicitly accept or deny the incoming connection
* request (in this example, we accept a single connection and reject any
* others that come in while the connection is open).
*/
if (FD_ISSET(fd_listen, &readfds))
{
sss_handle_accept(fd_listen, &conn);
}
/*
* If sss_handle_accept() accepts the connection, it creates *another*
* socket for sending/receiving data over sss. Note that this socket is
* independant of the listening socket we created above. This socket's
* descriptor is stored in conn.fd. If conn.fs is set in readfs... we have
* incoming data for our sss server, and we call our receiver routine
* to process it.
*/
else
{
if ((conn.fd != -1) && FD_ISSET(conn.fd, &readfds))
{
sss_handle_receive(&conn);
}
}
} /* while(1) */
}
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2009 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/