协议:如何实现一个最简单的通信协议(线程模拟)
程序员文章站
2022-06-27 22:54:10
*Author : DavidLin
*Date : 2014-12-20pm
*Email : linpeng1577@163.com or linpe...
*Author : DavidLin *Date : 2014-12-20pm *Email : linpeng1577@163.com or linpeng1577@gmail.com *world : the city of SZ, in China *Ver : 000.000.001 *For : threads for rxtx! *history : editor time do * 1)LinPeng 2014-12-20 created this file! * 2) */
一位工程师前辈问:怎么鄙人博客很多转载文章,只能说陈皓,李先静他们博客写得太好了,除了当场点赞,还必须转发,广而告之,大家分享。 切入正题,什么是通信协议,通信协议有哪些?TCP/IP, LTE,PDT,Tetra, Wifi, bluetooth,还是ZigBee, Can, rs232,这些都是,无论是高端大气上档次的LTE,互联网 基石的TCP/IP,或者是单片机时代的RS232,协议到底是什么?协议基于什么?scoket是协议吗,什么是路由? 刚开始的看《TCP/IP协议卷》3部曲,怎么也看不通透; 毕业设计时ZigBee协议原语看起来就头大; 工作的时间DMR协议读起来不知所谓; 《大话无线通信》诚然是一部好书,不过那是一部很好科普读物。 那天在南山科技园,听到一个网优工程师和协议工程师的话,深有感触: 协议工程师:协议越来越庞大,不管是制定协议,还是实习协议,每个人都只是对自己负责的那一块很熟悉,以后如何做架构,框架? 网优工程师:你再怎么变,我都是抓包专家! 一语点醒梦中人,再复杂的程序,不都是26个字母组成的吗? 哈哈,开个玩笑,再复杂的协议,抽象出来不也是ISO 7层协议可以描述的吗? 本文描写的就是一个用线程间通信模拟的一个最简单的生产者-消费者模型, 也可以认为是客户端-服务器模型,注释有空再写,不过大家应该一看就明白了, “一切的软件问题都可以通过在上面加一层封装来解决”,这是同事小周的老大说的, 不过封装就像化妆,真面目永远在妆容之下,所以准备谈女朋友的程序员,就 应当请她去游泳! 真相会浮出水面! (一直不明白为什么课本,侦探小说老是说真相浮出水面,直到我长大......,所以说小孩子还是懂得比大人少) #include #include #includestruct msg_packet_t { char head; char address; char opcode; char length; char tail; }; /* Who is 9527, I see you say :) */ #define is_msg(msg) \ (((msg->head) == 95) && ((msg->tail) == 27))) #define is_42 do \ { \ printf("The Answer to the Ultimate Question of Life," \ "The Universe and Everything is 42\n"); \ }while(0) #define INIT_MSG_PACKET(msg) \ { \ .head = 95, \ .address = 00, \ .opcode = 01, \ .length = 00, \ .tail = 27, \ } #define msg_info(msg) do \ { \ printf("\t %s. \n"\ "\t head :%d\n"\ "\t address :%d\n"\ "\t opcode :%d\n"\ "\t length :%d\n"\ "\t tail :%d\n"\ , \ __FUNCTION__,\ ((struct msg_packet_t*)msg)->head ,\ ((struct msg_packet_t*)msg)->address,\ ((struct msg_packet_t*)msg)->opcode ,\ ((struct msg_packet_t*)msg)->length ,\ ((struct msg_packet_t*)msg)->tail \ ); \ }while(0) struct msg_packet_t g_pool = INIT_MSG_PACKET(g_pool); pthread_cond_t had_tx = PTHREAD_COND_INITIALIZER; pthread_mutex_t lock = PTHREAS_MUTEX_INITIALIZER; char msg_pack(char opcode); char msg_unpack(struct msg_packet *msg); void phy_tx(void* buff); void phy_rx(void* buff); void *thread_rx (void* buff) { struct msg_packet_t msg; pthread_mutex_lock(lock); printf("\n\t wait msg\n"); pthread_cond_wait(&had_tx, &lock); msg_unpack(&msg); msg_info(&msg); pthread_mutex_unlock(&lock); sleep(rand()%1); } void *thread_tx (void *buff) { struct msg_packet_t msg; while(1) { msg.opcode = rand()%43; pthread_mutex_lock(&lock); msg_pack(msg.opcode); pthread_mutex_unlock(&lock); pthread_cond_signal(&had_tx); printf(msg tx !\n); sleep(rand()%10); } } int main(int argc, char *argv[]) { pthread_t tid_tx; pthread_t tid_rx; srand(time(NULL)); pthread_create(&tid_tx, NULL, thread_tx, NULL); pthread_create(&tid_rx, NULL, thread_rx, NULL); pthread_join(tid_tx, NULL); pthread_join(tid_rx, NULL); exit(0); } char msg_unpack(struct msg_packet_t* msg) { phy_rx(msg); if(!is_msg(msg)) { return; } switch(msg->opcode) { case 42: is_42; break; default: break; } } char msg_pack(char opcode) { struct msg_packet_t* ptr = &g_pool; ptr->opcode = opcode; phy_tx(&g_pool); } void phy_tx(void* buff) { g_pool = *(struct msg_packet_t*)buff; //phy tx } void phy_rx(void* buff) { //phy rx *(struct msg_packet_t*)buff = g_pool; }