Dependencies: EthernetNetIf mbed NetServicesMin
message.c
00001 00002 #include "message.h" 00003 00004 void (*getMessageHandler)(TCPSocket* pConnectedSocket, char* msg, int len) = defaultMessageHandler; 00005 void (*postMessageHandler)(TCPSocket* pConnectedSocket,char* msg, int len) = defaultMessageHandler; 00006 00007 void defaultMessageHandler(TCPSocket* pConnectedSocket, char* msg, int len) 00008 { 00009 printf(msg); 00010 vSendMessage(pConnectedSocket, "DEFAULT\r\n", 9); 00011 } 00012 void vSendMessage(TCPSocket* pConnectedSocket, char* msg, int len) 00013 { 00014 pConnectedSocket->send("RESPONSE\r\n",10); 00015 pConnectedSocket->send(msg,len); 00016 pConnectedSocket->send("\r\n\r\n",4); 00017 } 00018 void vGetMessage(TCPSocket* pConnectedSocket) 00019 { 00020 char line[BUFFER_LENGTH]; 00021 int len; 00022 bool endOfMessage = false; 00023 while(!endOfMessage) 00024 { 00025 len = iGetLine(pConnectedSocket, line, BUFFER_LENGTH); 00026 if(strncmp(line,"GET\r\n", 5)==0) 00027 { 00028 len = iGetLine(pConnectedSocket, line, BUFFER_LENGTH); 00029 getMessageHandler(pConnectedSocket, line, len); 00030 } 00031 else if(strncmp(line,"POST\r\n",6)==0) 00032 { 00033 len = iGetLine(pConnectedSocket, line, BUFFER_LENGTH); 00034 postMessageHandler(pConnectedSocket, line, len); 00035 } 00036 else if(strncmp(line, "\r\n", 2)==0) 00037 { 00038 endOfMessage = true; 00039 } 00040 } 00041 } 00042 int iGetLine(TCPSocket* pConnectedSocket, char* buf, int len) 00043 { 00044 int counter = 0; 00045 while(counter < len) 00046 { 00047 if(pConnectedSocket->recv(&(buf[counter]),1) > 0) 00048 { 00049 if(counter > 0) 00050 { 00051 if(buf[counter]=='\n' && buf[counter-1]=='\r') 00052 { 00053 return counter+1; 00054 } 00055 } 00056 counter++; 00057 } 00058 } 00059 return -1; 00060 } 00061 void vSetMessageHandler(int messageType, void (*messageHandler)(TCPSocket* pConnectedSocket,char* msg, int len)) 00062 { 00063 switch(messageType) 00064 { 00065 case TYPE_GET: getMessageHandler = messageHandler; break; 00066 case TYPE_POST: postMessageHandler = messageHandler; break; 00067 default: break; 00068 } 00069 }
Generated on Sat Jul 16 2022 21:50:03 by 1.7.2