Linux开发——实战(一)LinkList实现智能网关代理

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_25490573/article/details/102157281

目录

 

懒人链接:

需求分析

链表实现:

Http实现 

tcp实现


懒人链接:

                     点我下载:https://download.csdn.net/download/qq_25490573/11834524

需求分析

1.实现LinkList双向链表

2.链表管理

3.加载数据库的项

功能实现,嵌入式中智能网关网络代理程序

                  能够连接串口,wifi(tcp通信,http通信,udp通信)

运行截图:

链表实现:

linklist.h

#include "tcp.h"
static struct node_data tcp_data =
{
        .id             = ID_SOCK_TCP,
        .flay   = ENABLE,
        .Init   = init_tcp_data,
        .SendData = send_buffer_tcp_data,
};

int init_tcp_data(struct node_data * data)
{
	struct sockaddr_in servaddr;	
	
	if((data->sock_fd =socket(AF_INET, SOCK_STREAM, 0))<0)
	{
		printf("create socket error!!!\n");
		exit(0);
	}
	bzero(&servaddr,sizeof(servaddr));
	servaddr.sin_family=AF_INET;
	//servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
	servaddr.sin_port=htons(TCP_PORT);
	if(inet_pton(AF_INET,TCP_ADDR,&servaddr.sin_addr)<=0)
	{
		printf("inet_pton fail,process stop!\n");
		exit(0);
	}
	if(connect(data->sock_fd,(struct sockaddr *)(&servaddr), sizeof(servaddr)) < 0)
	{
		printf("connect fail!!! \n");
		exit(0);
	}
	return 0;
}

int send_buffer_tcp_data(struct node_data * data,void * databuffer)
{

	printf("%s\n",(char*)databuffer);
	int ret = write(data->sock_fd,databuffer,strlen(databuffer));
	if(ret<0)
	{
		printf("tcp send fail!!!,error:%d,result:%s\n",errno,strerror(errno));
		exit(0);
	}else{
		printf("tcp send sucess!!!,send length:%d\n",(int)strlen(databuffer));
	}
	return ret;
}

void tcp_init_methor(list_ptr list,void * ndata)
{
	if(list==NULL)
		{
		errno = EINVAL;
		return ;
		}
	list_insert_before(&list,0,ndata);
}
void Init_tcp(list_ptr list)
{
	tcp_init_methor(list,(void *)&tcp_data);
}


LinkList.c

#include "linklist.h"

list_ptr Create_list(void){
	list_ptr list_head = NULL;
	list_head = (list_ptr)malloc(sizeof(_T_list));
	if(list_head == NULL){
		errno = ENOMEM; //ENOMEM  linux错误码   没有内存
		return NULL;
	}
	list_head->limit_size = 0;
	list_head->head = (node_t*)malloc(sizeof(node_t));
	if(list_head->head == NULL){
		errno = ENOMEM; //ENOMEM  linux错误码   没有内存
		return NULL;
	}
	list_head->head->data = NULL;
	list_head->head->next = list_head->head->prev = NULL;
	list_head->last = list_head->head;
	return list_head;
}

int list_insert_before(_T_list ** list_head,int num,void * new_node_data)
{
	u32 counter = 0;
	node_ptr current = NULL;
	node_ptr new_node = NULL;

	if(list_head==NULL||(*list_head)==NULL){
		errno = EINVAL;		//错误的值
		return -1;
	}
	if((*list_head)->limit_size !=0)
	{
		new_node = (node_ptr)malloc(sizeof(node_ptr));
		if((*list_head)->head == NULL){
			errno = ENOMEM; //ENOMEM  linux错误码   没有内存
			return -1;
		}
		new_node->next = new_node->prev = NULL;
		if(new_node_data == NULL){
			errno = EINVAL;		//错误的值
			free(new_node);
			return -1;
		}
		new_node->data = new_node_data;
		if(num>=0&&num<(*list_head)->limit_size)
		{
			current = (*list_head)->head;
			while(counter < num)
				{
					counter++;
					current = current->next;
				}
			if(counter == 0)
				{
					(*list_head)->head->prev = new_node;
					new_node->next = (*list_head)->head;
					(*list_head)->head = new_node;
					(*list_head)->limit_size++;
					return 0;
				}
			else if(counter >0)
				{
					new_node->prev = current->prev;
					new_node->next = current;
					new_node->prev->next = new_node;
					current->prev = new_node;
					(*list_head)->limit_size++;
					return 0;
				}
			else
				errno = EINVAL;
				free(new_node);
				new_node==NULL;
				return -1;
		}
	}else if((*list_head)->limit_size == 0){
		if(num!=0)
			{
				errno = EINVAL;
				return -1;
			}	
		(*list_head)->head->data = (void *)new_node_data;
		(*list_head)->limit_size++;
	}
	return 0;
}

int list_insert_back(list_ptr * list_last,int num,void * new_node_data)
{
	u32 counter = 0;
	node_ptr current = NULL;
	node_ptr new_node = NULL;

	if(list_last==NULL||(*list_last)==NULL){
		errno = EINVAL;		//错误的值
		return -1;
	}
	counter = (*list_last)->limit_size-1;
	if((*list_last)->limit_size !=0)
	{
		new_node = (node_ptr)malloc(sizeof(node_ptr));
		if((*list_last)->head == NULL){
			errno = ENOMEM; //ENOMEM  linux错误码   没有内存
			return -1;
		}
		new_node->next = new_node->prev = NULL;
		if(new_node_data == NULL){
			errno = EINVAL;		//错误的值
			free(new_node);
			return -1;
		}
		new_node->data = new_node_data;
		if(num>=0&&num<(*list_last)->limit_size)
		{
			current = (*list_last)->last;
			while(counter < num)
				{
					counter--;
					current = current->prev;
				}
			if(counter == ((*list_last)->limit_size-1))
				{
					(*list_last)->last->next = new_node;
					new_node->prev = (*list_last)->last;
					(*list_last)->last = new_node;
					(*list_last)->limit_size++;
					return 0;
				}
			else if(counter >0)
				{
					new_node->next = current->next;
					current->next = new_node;
					new_node->prev = current;
					new_node->next->prev = new_node;
					(*list_last)->limit_size++;
					return 0;
				}
			else
				errno = EINVAL;
				free(new_node);
				new_node==NULL;
				return -1;
		}
	}else if((*list_last)->limit_size == 0){
		if(num!=0)
			{
				errno = EINVAL;
				return -1;
			}
		(*list_last)->head->data = new_node_data;
		(*list_last)->limit_size++;
	}
	return 0;
}

int list_append(list_ptr * list_name,void * new_node_data){
	int a = list_insert_back(list_name,((*list_name)->limit_size-1),new_node_data);
	return a;
}

int list_delete(list_ptr * list_head,int begin,int number)
{
	int begin_i = 0;
	node_ptr current = NULL;
	node_ptr current_h = NULL;
	node_ptr current_p = NULL;
	if(list_head==NULL||(*list_head)==NULL)
		{
		errno = EINVAL;
		return -1;
		}
	if(begin<0||(number+begin)>(*list_head)->limit_size){
		errno = EINVAL;
		return -1;
	}
	current = (*list_head)->head;
	current_h = (*list_head)->last;
	if(begin == 0 && (number+begin)<(*list_head)->limit_size)
		{
			while(number > 0){
			current_h = current->next;
			free(current);
			current = current_h;
			number--;
			}
			current->prev = NULL;
			(*list_head)->head = current;
			(*list_head)->limit_size -= number;
			return 0;
		}
	else if(begin != 0 && (number+begin)==(*list_head)->limit_size)
		{
			while(number > 0){
			current = current_h->prev;
			free(current_h);
			current_h = current;
			number--;
			}
			current_h->next =NULL;
			(*list_head)->last = current_h;
			(*list_head)->limit_size -= number;
			return 0;
		}
	else if(begin != 0 && (number+begin)<(*list_head)->limit_size)
		{
			while(begin_i < begin)
			{
			begin++;
			current = current->next;
			}
			current_p = current->prev;
			while(number > 0){
			current_h = current->next;
			free(current);
			current = current_h;
			number--;
			}
			current_p->next = current;
			current->prev = current_p;
			(*list_head)->limit_size -= number;
			return 0;
		}
	else if(begin == 0 && (number+begin)==(*list_head)->limit_size)
		{
			begin_i=1;
			while(begin_i<(*list_head)->limit_size)
				{
				current_h = current->next;
				free(current);
				current = current_h;
				begin_i++;
				}
			free((*list_head)->last->data);
			(*list_head)->last->data = NULL;
			(*list_head)->last->next = (*list_head)->last->prev = NULL;
			(*list_head)->head=(*list_head)->last;
			(*list_head)->limit_size ==0;
			return 0;
		}
	errno = EINVAL;
	return -1;
}

int list_delete_one(list_ptr * list_head,int begin)
{
	int err = list_delete(list_head,begin,1);
	return err;
}

int list_set_data(list_ptr * list_name,int num,void * new_node_data)
{
	int begin = 0;
	node_ptr current = NULL;
	if(list_name==NULL||(*list_name)==NULL)
	{
		errno = EINVAL;
		return -1;
	}
	if(begin<0||num>=(*list_name)->limit_size){
		errno = EINVAL;
		return -1;
	}
	current = (*list_name)->head;
	while(begin<num)
	{
		begin++;
		current = current->next;
	}
	free(current->data);
	current->data = new_node_data;
	return 0;
}

void * list_get_data(list_ptr * list_name,int num)
{
	int begin = 0;
	node_ptr current = NULL;
	if(list_name==NULL||(*list_name)==NULL)
	{
		errno = EINVAL;
		return NULL;
	}	
	if(begin<0||num>=(*list_name)->limit_size){
		errno = EINVAL;
		return NULL;
	}
	current = (*list_name)->head;
	while(begin<num)
	{
		begin++;
		current = current->next;
	}
	return current->data;
}

void list_travel(list_ptr * list_name,void (*cmd_function)(void *))
{
	node_ptr current = NULL;
	if(list_name==NULL||(*list_name)==NULL)
	{
		errno = EINVAL;
		return ;
	}
	if((*list_name)->limit_size==0)
		return ;
	current = (*list_name)->head;
	while(current->next !=NULL)
	{
		(*cmd_function)(current->data);
		current = current->next;
	}
	(*cmd_function)(current->data);
}

int list_search(list_ptr * list_name,void * find_value,int(*campare)(void *,void *))
{
	int number = 0;
	node_ptr current = NULL;
	if(list_name==NULL||(*list_name)==NULL)
	{
		errno = EINVAL;
		return -1;
	}
	if((*list_name)->limit_size==0)
		return 0;
	current = (*list_name)->head;
	while(current->next !=NULL&&campare(find_value,current->data))
	{
		number++;
		current = current->next;
	}
	if(campare(find_value,current->data)){
		return number;
	}
	return -1;
}

Http实现 

http.h

#ifndef _HTTP_H_
#define _HTTP_H_


#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include "linklist.h"
#include <unistd.h>

#define HTTP_PORT 6666
#define HTTP_ADDR "192.168.10.110"

int init_http_data(struct node_data * data);
int send_buffer_http_data(struct node_data * data,void * databuffer);

void http_init_methor(list_ptr list,void * ndata);
void Init_http(list_ptr list);


#endif

http.c

#include "http.h"

struct node_data http_data={
        .id     = ID_HTTP,
        .flay   = ENABLE,
        .Init   = init_http_data,
        .SendData = send_buffer_http_data,
};


int init_http_data(struct node_data * data)
{
	struct sockaddr_in servaddr;	
	
	if((data->sock_fd =socket(AF_INET, SOCK_STREAM, 0))<0)
	{
		printf("create socket error!!!\n");
		exit(0);
	}
	bzero(&servaddr,sizeof(servaddr));
	servaddr.sin_family=AF_INET;
	//servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
	servaddr.sin_port=htons(HTTP_PORT);
	if(inet_pton(AF_INET,HTTP_ADDR,&servaddr.sin_addr)<=0)
	{
		printf("inet_pton fail,process stop!\n");
		exit(0);
	}
	if(connect(data->sock_fd,(struct sockaddr *)(&servaddr), sizeof(servaddr)) < 0)
	{
		printf("connect fail!!! \n");
		exit(0);
	}
	return 0;
}

int send_buffer_http_data(struct node_data * data,void * databuffer)
{

	u8 *http_data =NULL;
	u8 * length = (u8 *)malloc(128);
	int len = strlen(databuffer);
	sprintf(length,"%d",len);
	http_data = (u8*)malloc(len+1024);
	memset(http_data,0,sizeof(http_data));
	strcat(http_data,"POST /webservices/qqOnlineWebSerice.asm/qq");
	strcat(http_data,"");
	strcat(http_data,"");
	strcat(http_data,"");
	strcat(http_data,length);
	strcat(http_data,"\r\n");
	strcat(http_data,databuffer);
	strcat(http_data,"\r\n\r\n");
	printf("%s\n",http_data);
	int ret = write(data->sock_fd,http_data,strlen(http_data));
	if(ret<0)
	{
		printf("http send fail!!!,error:%d,result:%s\n",errno,strerror(errno));
		exit(0);
	}else{
		printf("http send sucess!!!,send length:%d\n",len);
	}
	free(http_data);
	free(length);
	return ret;
}

void http_init_methor(list_ptr list,void * ndata)
{
	if(list==NULL)
		{
		errno = EINVAL;
		return ;
		}
	list_insert_before(&list,0,ndata);
}
void Init_http(list_ptr list)
{
	http_init_methor(list,(void *)&http_data);
}

tcp实现

tcp.h

#ifndef _TCP_H_
#define _TCP_H_


#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include "linklist.h"
#include <unistd.h>


#define TCP_PORT 6666
#define TCP_ADDR "192.168.10.110"

int init_tcp_data(struct node_data * data);
int send_buffer_tcp_data(struct node_data * data,void * databuffer);


void tcp_init_methor(list_ptr list,void * ndata);
void Init_tcp(list_ptr list);


#endif

sock_tcp.c

#include "tcp.h"
static struct node_data tcp_data =
{
        .id             = ID_SOCK_TCP,
        .flay   = ENABLE,
        .Init   = init_tcp_data,
        .SendData = send_buffer_tcp_data,
};

int init_tcp_data(struct node_data * data)
{
	struct sockaddr_in servaddr;	
	
	if((data->sock_fd =socket(AF_INET, SOCK_STREAM, 0))<0)
	{
		printf("create socket error!!!\n");
		exit(0);
	}
	bzero(&servaddr,sizeof(servaddr));
	servaddr.sin_family=AF_INET;
	//servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
	servaddr.sin_port=htons(TCP_PORT);
	if(inet_pton(AF_INET,TCP_ADDR,&servaddr.sin_addr)<=0)
	{
		printf("inet_pton fail,process stop!\n");
		exit(0);
	}
	if(connect(data->sock_fd,(struct sockaddr *)(&servaddr), sizeof(servaddr)) < 0)
	{
		printf("connect fail!!! \n");
		exit(0);
	}
	return 0;
}

int send_buffer_tcp_data(struct node_data * data,void * databuffer)
{

	printf("%s\n",(char*)databuffer);
	int ret = write(data->sock_fd,databuffer,strlen(databuffer));
	if(ret<0)
	{
		printf("tcp send fail!!!,error:%d,result:%s\n",errno,strerror(errno));
		exit(0);
	}else{
		printf("tcp send sucess!!!,send length:%d\n",(int)strlen(databuffer));
	}
	return ret;
}

void tcp_init_methor(list_ptr list,void * ndata)
{
	if(list==NULL)
		{
		errno = EINVAL;
		return ;
		}
	list_insert_before(&list,0,ndata);
}
void Init_tcp(list_ptr list)
{
	tcp_init_methor(list,(void *)&tcp_data);
}

最后是main函数

#include "stdio.h"
#include "linklist.h"
#include "http.h"
#include "tcp.h"

void node_send_data(void * vdata)
{
	int ret = 0;
	node_data_ptr data = (node_data_ptr)vdata;
	if(data->flay == ENABLE)
		{
		data->Init(data);
		ret = data->SendData(data,"hello,world!");
		printf("type:%d send success!\n",data->id);
		}
	
}


int main(int argc,char*argv[])
{
	list_ptr remote_list = Create_list();
	Init_http(remote_list);
	Init_tcp(remote_list);

	list_travel(&remote_list, node_send_data);
}

猜你喜欢

转载自blog.csdn.net/qq_25490573/article/details/102157281