tcp_server_demo.lst

来自「cs8900 c51应用」· LST 代码 · 共 350 行 · 第 1/2 页

LST
350
字号
 155          }
 156          
 157           /*
 158           * Event listener invoked when TCP/IP stack receives TCP data for
 159           * a given socket. Parameters:
 160           * - cbhandle - handle of the socket this packet is intended for. Check it
 161           *      just to be sure, but in general case not needed
 162           * - event - event that is notified. For TCP there are quite a few possible
 163           *      events, check switch structure below for more information
 164           * - par1, par2 - parameters who's use depends on the event that is notified
 165           */
 166          INT32 tcps_demo_eventlistener(INT8 cbhandle, UINT8 event, UINT32 par1, UINT32 par2) reentrant
 167          {
 168          /* This function is called by TCP stack to inform about events  */
 169                  UINT16 i;
 170                  
 171                  if( cbhandle != tcps_demo_soch)         /* Not our handle       */
C51 COMPILER V7.06   TCP_SERVER_DEMO                                                       11/26/2004 11:32:46 PAGE 4   

 172                          return(-1);
 173                  
 174                  switch( event ){
 175                          
 176                          /* Connection request event. Used by TCP/IP stack to inform
 177                           * the application someone is trying to establish a connection.
 178                           * Server can decide, based on provided IP address and port number,
 179                           * whether to allow or not connection establishment.
 180                           * Parameters:
 181                           *  - par1 - remote hosts IP address
 182                           *  - par2 - remote hosts port number
 183                           * 
 184                           * Return values from event listener:
 185                           * -1 - do not allow connection to be established (send reset)
 186                           * -2 - do not send any response for now to the SYN packet (let's
 187                           *              think a little before answering)
 188                           * 1  - allow connection to be established
 189                           */
 190                          case TCP_EVENT_CONREQ:
 191                                  DEBUGOUT("Connection request arrived!\r\n");
 192                                  
 193                                  /* Enable all connections       */
 194                                  return(1);
 195                          
 196                                  break;
 197                                  
 198                          /* Connection abort event. Connection on a given socket is beeing 
 199                           * aborted for somereason (usually retransmissions are used up or 
 200                           * some abnormal situation in communication happened).
 201                           * Parameters:
 202                           *  - par1 - remote hosts IP address
 203                           *  - par2 - remote hosts port number
 204                           */
 205                          case TCP_EVENT_ABORT:
 206                                  DEBUGOUT("Connection aborting!\r\n");
 207                                  break;
 208                          
 209                          /* Connection established event - three-way handshaking performed
 210                           * OK and connection is established.
 211                           * Parameters:
 212                           *  - par1 - remote hosts IP address
 213                           *  - par2 - remote hosts port number
 214                           */
 215                          case TCP_EVENT_CONNECTED:
 216                                  DEBUGOUT("TCP connection established!\r\n");
 217                                  for(i=0;i<24;i++)
 218                                  {
 219                                          Buffer[i] = HelloMsg[i];
 220                                  }
 221                                  BufferLen = 24;
 222                                  tcps_demo_senddata = 1;
 223                                  break;
 224                                  
 225                          /* Connection closing event. Happens when TCP connection is
 226                           * intentionally close by some side calling close function and
 227                           * initializing proper TCP connection close procedure.
 228                           * Parameters:
 229                           *  - par1 - remote hosts IP address
 230                           *  - par2 - remote hosts port number
 231                           */
 232                          case TCP_EVENT_CLOSE:
 233                                  DEBUGOUT("TCP Connection closing...!\r\n");
C51 COMPILER V7.06   TCP_SERVER_DEMO                                                       11/26/2004 11:32:46 PAGE 5   

 234                                  break;
 235                                  
 236                          /* Data acknowledgment event. Happens when data that was
 237                           * previously sent gets acknowledged. This means we can now
 238                           * send some more data! :-)
 239                           * Parameters:
 240                           *  - par1 - remote hosts IP address
 241                           *  - par2 - remote hosts port number
 242                           */
 243                          case TCP_EVENT_ACK:
 244                                  DEBUGOUT("Data acknowledged!\r\n");
 245                                  /* if more data should be sent, adjust variables and
 246                                          set tcps_demo_senddata variable */
 247                                          
 248                                  break;
 249                          
 250                          /* Data received event. Happens when we receive some data over the
 251                           * TCP connection.
 252                           * Parameters:
 253                           *  - par1 - number of data bytes received
 254                           *  - par2 = 0
 255                           */
 256                          case TCP_EVENT_DATA:
 257                                  DEBUGOUT("Data arrived!\r\n");
 258                                  /* read data that was received (and 
 259                                   * probably do something with it :-)
 260                                   */
 261                                  for(i=0;i<par1;i++)
 262                                  {
 263                                          Buffer[i] = RECEIVE_NETWORK_B();
 264                                  }
 265                                  BufferLen = (UINT16)par1;
 266          /* iTS wrong (?) to send data from here :
 267                                  tcp_send(tcps_demo_soch, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, par1);
 268          */                              
 269                                  /* If needed initialize data sending
 270                                   * by setting tcps_demo_senddata variable
 271                                   */
 272                                  tcps_demo_senddata = 1;
 273                                  tcpc_demo_senddata = 1;
 274                                  break;
 275                                  
 276                          /* Regenerate data event. Happens when data needs to be
 277                           * retransmitted because of possible loss on the network.
 278                           * Note that THE SAME DATA must be sent over and over again
 279                           * until TCP_EVENT_ACK is generated (for that data)! 
 280                           * Parameters:
 281                           *  - par1 - amount of data to regenerate (usually all)
 282                           *      - par2 = 0
 283                           */
 284                          case TCP_EVENT_REGENERATE:
 285          /*                      for(i=0;i<par1;i++)
 286                                          net_buf[TCP_APP_OFFSET+i] = Buffer[i];
 287                                  tcp_send(tcps_demo_soch, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, par1);                              *
             -/
 288                                  tcps_demo_send();
 289                                  break;
 290                  
 291                  
 292                          default:
 293                                  return(-1);
 294                  }
C51 COMPILER V7.06   TCP_SERVER_DEMO                                                       11/26/2004 11:32:46 PAGE 6   

 295          }
 296          
 297          INT16 tcps_demo_send(void){
 298                  UINT16 i;
 299                  /* first check if data sending is possible (it may be that
 300                   * previously sent data is not yet acknowledged)
 301                   */
 302                  if(tcp_checksend(tcps_demo_soch) < 0 ){
 303                          /* Not yet */
 304                          return -1;
 305                  }
 306                  
 307                  /* put message in buffer. Message needs to start from TCP_APP_OFFSET
 308                   * because TCP/IP stack will put headers in front of the message to
 309                   * avoid data copying
 310                   */
 311          
 312                  for(i=0;i<BufferLen;i++)
 313                          net_buf[TCP_APP_OFFSET+i]=Buffer[i];
 314          
 315                  /* send data */
 316                  return tcp_send(tcps_demo_soch, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, Buffer
             -Len);
 317          
 318          }

C51 COMPILATION COMPLETE.  5 WARNING(S),  1 ERROR(S)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?