⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10e445bbbcba001c10fdb966a28cf85c

📁 基于UCOS的嵌入式系统的应用
💻
字号:
#ifndef _NETSOCKET_C_
#define _NETSOCKET_C_

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <sys/alt_cache.h>
#include "includes.h"
#include "alt_lwip_dev.h"
#include "lwip/sys.h"
#include "system.h"
#include "netsocket.h"
#include "lwip/netif.h"
#include "lwip/sockets.h"
#include "alt_types.h"
#include "user.h"
#include <io.h>
#include <sys/alt_irq.h>

int iAllLinker = 0;
ConnList* connHead = NULL;
ConnSocket myConn;
extern alt_u8* image_head;
int fd_listen=0;
ConnSocket     conn[MAX_NUM_LINK];
/************************************
 * 函数名称:RestClientSocket
 * 参    数: 
 * struct ConnSocket* pConn  客户端SOCKET
 * 功能描述:复位连接
 *************************************/
void RestClientSocket(ConnSocket* pConn)
{
  memset(pConn,0x0,sizeof(ConnSocket));
  
  pConn->fd = -1;
  pConn->state = READY;
  pConn->keep_alive_count = NET_KEEP_ALIVE_COUNT;
  pConn->activity_time = alt_nticks();
  pConn->rx_buffer = NULL;
  pConn->tx_buffer = NULL;
  pConn->rx_rd_pos = NULL;
  pConn->rx_wr_pos = NULL;  
}
/************************************
 * 函数名称:NewClientSocket
 * 参    数: 
 * struct ConnList* *pHead  客户端链表的首地址
 * 返    回:
 * struct ConnList*  新客户端的地址
 * 功能描述:建立一个新的客户端连接
 *************************************/
ConnList* NewClientSocket()
{
  /*如果达到最大连接,则不进行建立连接*/
  printf("NewClientSocket is called\n");
  if(iAllLinker > MAX_NUM_LINK) return NULL;
  ConnList* newConn;
  newConn = (ConnList*)alt_uncached_malloc(sizeof(ConnList));
  //Setting Default
   
  RestClientSocket(&newConn->conn);  
  
  newConn->next = NULL;
  
  iAllLinker++;

  newConn->iPosition = iAllLinker;
  return newConn;
}
/************************************
 * 函数名称:DelClientSocket
 * 参    数: 
 * struct ConnList* *pHead  客户端链表的首地址
 * int iPos 需要删除连接的位置,0<iPos <MAX_NUM_LINK+1
 * 功能描述:删除连接
 *************************************/
void DelClientSocket(ConnList* *pHead,int iPos)
{
  ConnList *ps,*head,*psPrev;
  head = *pHead;
  if(NULL == head || iPos < 0 || iPos > MAX_NUM_LINK)
    return;
  printf("Delelte a Client request link and the client number %d\n",
         iAllLinker - 1);
  ps = head;
  psPrev = head;
  /*删除头部*/
  if(1 == iPos)
  {
    head = ps->next;
    alt_uncached_free(ps);
    *pHead = head;
    iAllLinker--;
    return;
  }
  while(iPos > 1)
  {
    psPrev = ps;
    ps = ps->next;
    iPos--;
  }
  if(NULL == ps->next)
  {
    alt_uncached_free(ps);
    psPrev->next = NULL;
  }
  else
  {
    psPrev->next = ps->next;
    alt_uncached_free(ps);
  }
  iAllLinker--;
  
}
/************************************
 * 函数名称:AddClientSocket
 * 参    数: 
 * struct ConnList* *pHead  客户端链表的首地址
 * 功能描述:生成一个新的连接
 *************************************/
ConnSocket* AddClientSocket(ConnList* *pHead)
{
  ConnList *ps,*head,*conn;
  head = *pHead;
  ConnSocket* pCon;
  printf("AddClientSocket is called\n");
  conn = NewClientSocket();
  if(NULL == conn) /*新建不成功*/
    return NULL;
  printf("The New Client Enter and the client number is %d\n",iAllLinker);
  /*没有任何一个连接*/
  if(NULL == head)
  {
    *pHead = conn;
    pCon = &conn->conn;
    return pCon;
  }
  /*链表已有连接,则加入链表中*/
  ps = head;
  while(NULL != ps->next)
  {
    ps = ps->next;
  }
  ps->next = conn;
  pCon = &conn->conn;
  return pCon;
}
/************************************
 * 函数名称:AcceptClientRequest
 * 参    数: 
 * int listen_socket  监听SOCKET
 * ConnList* *pHead  链表首地址
 * 功能描述:管理接受客户端连接
 *************************************/
int AcceptClientRequest(int listen_socket, ConnList* *pHead)
{
  int  socket, len;
  struct sockaddr_in  rem;
  ConnSocket* conn;
  len = sizeof(rem);
  printf("AcceptClientRequest is called\n");  
  if((socket = accept(listen_socket,(struct sockaddr*)&rem,&len)) < 0)
  {
    printf("Rejust the request\n");
    return socket;
  }

  if(iAllLinker == MAX_NUM_LINK)
  {
    close(socket);
    printf("!!!Warnning:Can't accept the client!!!!\n");
    return -1;
  }

  printf("Accepted connection request from %s\n",
               inet_ntoa(rem.sin_addr));
  
  conn = AddClientSocket(pHead);
  conn->fd = socket;
  conn->tx_buffer = image_head;
  conn->file_length = IMAGE_SIZE;
  
  TransData(conn);
  ReceiveData(conn);
  //printf("\nmyConn.fd=%d\n",conn->fd);
  
  return socket;
}
/************************************
 * 函数名称:ManageClientSocket
 * 参    数: 
 * struct ConnList* *pHead  客户端链表的首地址
 * int iPos 需要删除连接的位置,0<iPos <MAX_NUM_LINK+1
 * 功能描述:管理客户端连接
 *************************************/
void ManageClientSocket(ConnList* *pHead,ConnSocket* pConn,int iPos)
{
  ConnList *head;
  head = *pHead;

  alt_u32 current_time = 0;
  
  if(pConn->state == READY || pConn->state == PROCESS || pConn->state == DATA)
  {
    current_time = alt_nticks();
    
    if( (current_time - pConn->activity_time) >= NET_KEEP_ALIVE_TIME )
    {
      pConn->state = RESET;
    }
  }
  
  if(pConn->state == COMPLETE)
  {
   
    //pConn->keep_alive_count--;
    pConn->data_sent = 0;
    
    if(pConn->keep_alive_count == 0)
    {
      pConn->close = 1;
    }
    
    pConn->state = pConn->close ? CLOSE : READY;
  }
  
  if(pConn->state == RESET)
  {  
    if(NULL != pConn->rx_buffer)
      memset(pConn->rx_buffer, 0, NET_RX_BUF_SIZE);
    pConn->state = CLOSE;
  }
  
  /* Close the TCP connection */
  if(pConn->state == CLOSE)
  {
    close(pConn->fd);
    //删除
    DelClientSocket(pHead,iPos);
  } 
}
void bind_task()
{
    
  struct  sockaddr_in addr;
  INT8U return_code = OS_NO_ERR;
  
  int i;
  
  OSSemPend(bind_sem, 0, &return_code);
  //printf("bind_task is called\n");
  if ((fd_listen = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    die_with_error("[http_task] Listening socket creation failed");
  }
  
  
  addr.sin_family = AF_INET;
  addr.sin_port = htons(4000);
  addr.sin_addr.s_addr = INADDR_ANY;
    
  if ((bind(fd_listen,(struct sockaddr *)&addr,sizeof(addr))) < 0)
  {
    die_with_error("[http_task] Bind failed");
  }
    
  
  if ((listen(fd_listen,1)) < 0)
  {
    die_with_error("[http_task] Listen failed");
  }

  printf("start listenning networking[port:%d].....\n",4000); 
   
  for(i=0; i<MAX_NUM_LINK; i++)
  {
    //printf("http_reset_connection socket\n"); 
    RestClientSocket(&conn[i]);
    //printf("conn[%d]=%d\n",i,conn[i].fd);
  }  
  //printf("myConn.fd=%d\n",Conn.fd);
  
      
  OSSemPost(bind_sem);
  OSSemPost(connect_sem);
  printf("OSSemPost(connect_sem);\n");
  OSTaskDel(OS_PRIO_SELF);    
}
void connect_task()
{
  int     max_socket, client;
 // int i;
  struct  timeval select_timeout;
  fd_set  readfds, writefds;
  ConnList* ps;  
  INT8U return_code = OS_NO_ERR;
  
  //if(mode == 0)
  OSSemPend(connect_sem, 0, &return_code);
  //printf("connect_task is called\n");
  printf("fd_listen=%d\n",fd_listen);
  ps = connHead;
  printf("ps->conn.fd=%d\n",ps->conn.fd);
  //printf("fd_listen=%d\n",fd_listen);
  while(1)
  {   
    
    FD_ZERO(&readfds);
    //printf("readfds=%d\n",readfds);
    FD_ZERO(&writefds);
    FD_SET(fd_listen, &readfds);
    //printf("readfds=%d\n",readfds);
    max_socket = fd_listen+1;
    
    ps = connHead;
    while(ps != NULL)
    {
        //printf("ps->conn.fd=%d\n",ps->conn.fd);
        if (max_socket <= ps->conn.fd)
        {
          max_socket = ps->conn.fd+1;
        }
        ps = ps->next;      
    }
    if (myConn.fd != -1)
    {
      //printf("myConn.fd=%d\n",myConn.fd);
      FD_SET(myConn.fd, &readfds);
      if (max_socket <= myConn.fd)
      {
        max_socket = myConn.fd+1;
      }
    }
    select_timeout.tv_sec = 0;
    select_timeout.tv_usec = 500;

    
    select(max_socket, &readfds, &writefds, NULL, &select_timeout);
    
    
    if(FD_ISSET(fd_listen, &readfds))
    {
      printf("FD_ISSET(fd_listen, &readfds)==1\n");
      client = AcceptClientRequest(fd_listen,&connHead);
      //ReceiveData(ConnSocket* pConn);
      //TransData(&myConn);
      //printf("myConn.fd=%d\n",myConn.fd);
    }
    
    /*for(i=0; i<MAX_NUM_LINK; i++)
    {
      //printf("我是第%d个\n",conn[i].fd);
      if (conn[i].fd != -1)
      { 
        printf("hahahaha\n");
        if(FD_ISSET(conn[i].fd,&readfds))
        {
          //printf("hahahaha\n");
          ReceiveData(&conn[i]);
        }
        
        //if(FD_ISSET(conn[i].fd,&writefds))
        //{
          //http_handle_transmit(&conn[i], i);
        //}
        //printf("http_manage_connection conn[%d]\n",i);
        ManageClientSocket(&connHead,&conn[i],i);
         
      }     
    }*/ 
    //TransData(fd_listen);
    //TransData(myConn);  
  } //while(1)
       
}
/************************************
 * 函数名称:TransData
 * 参    数: 
 * ConnSocket* pConn
 * 功能描述:传数数据
 *************************************/
int TransData(ConnSocket* pConn)
{
  int len,iSendLen = 0;
  //pConn->activity_time = alt_nticks();
  char buf[32];
  sprintf(buf, "welcome to the NIOS WEB!");
  len = sizeof(buf);
  //iSendLen = send(pConn->fd,buf,len,MSG_DONTWAIT);
  iSendLen = send(pConn->fd,buf,len,0);
  if(iSendLen <=0)
  {
    printf("Sever Send data error\n");
    pConn->state = RESET;
  }
  pConn->state = READY;
  return pConn->state;
}
/************************************
 * 函数名称:ReceiveData
 * 参    数: 
 * ConnSocket* pConn
 * 功能描述:接收数据Work out what the request we received was, and handle it.
 *************************************/
 int ReceiveData(ConnSocket* pConn)
 {
  //int data_used;
  printf("running ReceiveData!\n");
  int rx_code;
  //char buf[1024];

  //if (pConn->state == READY)
  while(1)
  {
    rx_code = recv(pConn->fd, pConn->rx_wr_pos, 
              (NET_RX_BUF_SIZE - (pConn->rx_wr_pos - pConn->rx_buffer) -1), 
              MSG_DONTWAIT);
    //rx_code = recv(pConn->fd, pConn->rx_buffer, sizeof(rx_buffer),0);
        
    /* 
     * If a valid data received, take care of buffer pointer & string 
     * termination and more on. Otherwise, we need to return and wait for more
     * data to arrive (until we time out).
     */
    if(rx_code > 0)
    {
      printf("有数据接收到!\n");
      pConn->rx_wr_pos += rx_code;
      *(pConn->rx_wr_pos+1) = 0;
      
      if(strstr(pConn->rx_buffer, HTTP_END_OF_HEADERS))
      {
        pConn->state = PROCESS;
      }
    //return pConn->state;  
    }
    //else
    //{
      //printf("没有数据接收到!\n");
      //pConn->state = READY;
      //return pConn->state;
    //}
  }
  
  //data_used = pConn->rx_rd_pos - pConn->rx_buffer;
  //memmove(pConn->rx_buffer,pConn->rx_rd_pos,pConn->rx_wr_pos-pConn->rx_rd_pos);
  //pConn->rx_rd_pos = pConn->rx_buffer;
  //pConn->rx_wr_pos -= data_used;
  //memset(pConn->rx_wr_pos, 0, data_used);  
 }
#endif //_NETSOCKET_C_

⌨️ 快捷键说明

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