int main(void) { setup(); while(1) { if(udp1.recv(buf)) { uart1.printf("\r\n============================"); uart1.printf("\r\n本地端口:%d", udp1.localPort ); uart1.printf("\r\n消息来源:%d.%d.%d.%d:%d", udp1.remoteIP[0], udp1.remoteIP[1], udp1.remoteIP[2], udp1.remoteIP[3], udp1.remotePort); uart1.printf("\r\n数据内容:"); uart1.printf((const char *)buf); udp1.sendto(udp1.remoteIP, udp1.remotePort, buf, 100); } if(udp2.recv(buf)) { uart1.printf("\r\n============================"); uart1.printf("\r\n本地端口:%d", udp2.localPort ); uart1.printf("\r\n消息来源:%d.%d.%d.%d:%d", udp2.remoteIP[0], udp2.remoteIP[1], udp2.remoteIP[2], udp2.remoteIP[3], udp2.remotePort); uart1.printf("\r\n数据内容:"); uart1.printf((const char *)buf); udp2.sendto(udp2.remoteIP, udp2.remotePort, buf, 100); } } }
int main(void) { setup(); while(1) { if(udp.recv(recvBuf)) { uart1.printf("\r\n============================"); uart1.printf("\r\n本地端口:%d",udp.localPort ); uart1.printf("\r\n消息来源:%d.%d.%d.%d:%d", udp.remoteIP[0],udp.remoteIP[1],udp.remoteIP[2],udp.remoteIP[3],udp.remotePort); uart1.printf("\r\n数据长度:%d",len); uart1.printf("\r\n数据内容:"); uart1.printf((const char *)recvBuf); udp.sendto(udp.remoteIP,udp.remotePort,recvBuf,100); } // udp.sendto(rip,8080,data,60); // delay_ms(500); } }
int main(void) { setup(); while(1) { res=f_opendir(&DirObject,"/123");//打开目录 if(res==FR_OK) { uart1.printf("\r\n打开目录成功 !\r\n"); uart1.printf("该目录所在簇号:%d\r\n",DirObject.clust); uart1.printf("该目录所在扇区号:%d\r\n",DirObject.sect); } else if(res==FR_NO_PATH) uart1.printf("所找目录路径不存在\r\n"); else uart1.printf("打开目录失败~~~~(>_<)~~~~ \r\n"); if(udp1.recv(buf)) { uart1.printf("\r\n============================"); uart1.printf("\r\n本地端口:%d",udp1.localPort ); uart1.printf("\r\n消息来源:%d.%d.%d.%d:%d", udp1.remoteIP[0],udp1.remoteIP[1],udp1.remoteIP[2],udp1.remoteIP[3],udp1.remotePort); uart1.printf("\r\n数据内容:"); uart1.printf((const char *)buf); uart1.printf("\r\n============================"); udp1.sendto(udp1.remoteIP,udp1.remotePort,buf,100); } delay_ms(1000); } }
//////////////////////////////////////////////////////////////////////////////////// /// /// \brief Creates a connection to the JAUS component given the provided data /// if possible. /// /// \param[in] id JAUS component ID. /// \param[in] destination Destination information for connection creation. /// /// \return Pointer to new connection on success, false on failure. /// //////////////////////////////////////////////////////////////////////////////////// Connection* UDP::CreateConnection(const Address& id, const Info* destination) { CxUtils::UdpSocket* primary = dynamic_cast<CxUtils::UdpSocket*>( ( (CxUtils::Socket*)mpSocket) ); // Only port that has reserved the JUDP port can create UDP connections. if(primary == NULL || primary->GetSourcePort() != this->mParameters.mDestPortNumber) { return NULL; } UDP* newUDP = new UDP(mSingleThreadModeFlag); newUDP->mID = id; newUDP->CopyConnectionData(this); newUDP->mParameters = mParameters; newUDP->mParameters = *destination; newUDP->mParameters.mClientFlag = true; newUDP->mpSocket = primary->CreateNewDestination(destination->mDestIP, destination->mDestPortNumber); if(newUDP->Initialize(&newUDP->mParameters)) { return newUDP; } else { delete newUDP; } return NULL; }
//function used to communicate with the sequencer so as to join the chat group. int chatClient::dojoin(string rs_ip, int rs_port){ string outmsg; int outlen, saddr_len; s_ip = rs_ip; s_port = rs_port; //setupt the UDP socket UDP joinUDP; joinUDP.setRemoteAddr(s_ip.c_str(),s_port); //args: sequencer's ip, port, myIP, myPort,myName; myMsg message = mmaker.makeJoin(name); msgMaker::serialize(outmsg, outlen, message); if(joinUDP.sendToNACK(outmsg.c_str(),outlen)==-2){ cerr<<"Error! Not able to join to the group... App is about to exit..." <<endl; exit(-1); } return 1; }
void setup() { ebox_init(); uart1.begin(115200); uart1.printf("\r\nuart1 9600 ok!"); w5500.begin(2, mac, lip, sub, gw, dns); attach_eth_to_socket(&w5500); w5500.getMAC (ip); uart1.printf("\r\nmac : %02x.%02x.%02x.%02x.%02x.%02x\r\n", ip[0], ip[1], ip[2], ip[3], ip[4], ip[5]); w5500.getIP (ip); uart1.printf("IP : %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); w5500.getSubnet(ip); uart1.printf("mask : %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); w5500.getGateway(ip); uart1.printf("GW : %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); uart1.printf("Network is ready.\r\n"); if(udp1.begin(0, 30000) == 0) uart1.printf("\r\nudp1 server creat ok! listen on 30000"); if(udp2.begin(1, 30001) == 0) uart1.printf("\r\nudp2 server creat ok! listen on 30001"); }
void setup() { // Set the port whis to use for contactign us back Udp.begin(8888); // Sedn the message to the server if (Udp.sendPacket(buffer, 39, remoteIP, port) < 0) { Particle.publish("Error"); } }
bool callback(const PDU &pdu) { // The packet probably looks like this: // // EthernetII / IP / UDP / RawPDU // // So we retrieve each layer, and construct a // DNS PDU from the RawPDU layer contents. EthernetII eth = pdu.rfind_pdu<EthernetII>(); IP ip = eth.rfind_pdu<IP>(); UDP udp = ip.rfind_pdu<UDP>(); DNS dns = udp.rfind_pdu<RawPDU>().to<DNS>(); // Is it a DNS query? if(dns.type() == DNS::QUERY) { // Let's see if there's any query for an "A" record. for(const auto &query : dns.queries()) { if(query.type() == DNS::A) { // Here's one! Let's add an answer. dns.add_answer( DNS::Resource( query.dname(), "127.0.0.1", DNS::A, query.query_class(), // 777 is just a random TTL 777 ) ); } } // Have we added some answers? if(dns.answers_count() > 0) { // It's a response now dns.type(DNS::RESPONSE); // Recursion is available(just in case) dns.recursion_available(1); // Build our packet auto pkt = EthernetII(eth.src_addr(), eth.dst_addr()) / IP(ip.src_addr(), ip.dst_addr()) / UDP(udp.sport(), udp.dport()) / dns; // Send it! sender.send(pkt); } } return true; }
void loop() { // First we need to parse before we can check avaiability. Udp.parsePacket(); // While there is data, we display it. while(Udp.available()) { // Read first char of data received char c = Udp.read(); // Print all the char one by one. Serial.print(c); } }
TEST_F(MatchesResponseTest, DHCP) { uint8_t dhcp_discover[] = { 255, 255, 255, 255, 255, 255, 0, 1, 1, 0, 0, 1, 8, 0, 69, 0, 1, 25, 77, 62, 0, 0, 64, 17, 44, 151, 0, 0, 0, 0, 255, 255, 255, 255, 0, 68, 0, 67, 1, 5, 0, 0, 1, 1, 6, 0, 217, 7, 133, 224, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 130, 83, 99, 53, 1, 1, 61, 7, 1, 0, 1, 1, 0, 0, 1, 255 }; uint8_t dhcp_offer[] = { 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 2, 8, 0, 69, 0, 1, 28, 221, 161, 0, 0, 64, 17, 158, 45, 127, 0, 0, 1, 127, 0, 0, 1, 0, 67, 0, 68, 1, 8, 0, 0, 2, 1, 6, 0, 217, 7, 133, 224, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 130, 83, 99, 53, 1, 2, 54, 4, 127, 0, 0, 1, 51, 4, 0, 0, 1, 44, 255 }; EthernetII discover(dhcp_discover, sizeof(dhcp_discover)); UDP *udp = discover.find_pdu<UDP>(); const RawPDU *raw = discover.find_pdu<RawPDU>(); ASSERT_TRUE(udp != NULL); ASSERT_TRUE(raw != NULL); udp->inner_pdu(raw->to<DHCP>()); EXPECT_TRUE(discover.matches_response(dhcp_offer, sizeof(dhcp_offer))); EXPECT_FALSE(discover.matches_response(dhcp_discover, sizeof(dhcp_discover))); }
void loop() { if (WiFi.ready()) { if (firstTimeReady) { firstTimeReady = false; // First time we get WiFi.ready(), initialize the UDP listener. This happens // every time we reconnect to WiFi. mcastUdp.begin(MCAST_PORT); } if (!haveServerAddr && millis() - serverAddrLastCheck >= SERVER_ADDR_CHECK_RETRY_MS) { serverAddrLastCheck = millis(); // If we don't have a server IP address, request it periodically (every 20 seconds) Serial.printlnf("requesting server address at %s:%d", mcastAddr.toString().c_str(), MCAST_PORT); strcpy(udpBuffer, "\n"); mcastUdp.sendPacket(udpBuffer, strlen(udpBuffer), mcastAddr, MCAST_PORT); } int count = mcastUdp.receivePacket(udpBuffer, UDP_BUFFER_SIZE - 1); if (count > 0) { udpBuffer[count] = 0; // Handle a received UDP packet from the server with the IP address and port of the server int addr[4], port; if (sscanf(udpBuffer, "%u.%u.%u.%u:%u", &addr[0], &addr[1], &addr[2], &addr[3], &port) == 5) { serverAddr = IPAddress(addr[0], addr[1], addr[2], addr[3]); serverPort = port; haveServerAddr = true; Serial.printlnf("serverAddr=%s port=%d", serverAddr.toString().c_str(), serverPort); } } } else { // If WiFi disconnects, we need to reestablish listeners next time firstTimeReady = true; } }
void setup() { ebox_init(); uart1.begin(9600); ret = sd.begin(); if(!ret) uart1.printf("\r\nsdcard init ok!"); f_mount(0, &fs); w5500.begin(mac,lip,sub,gw); attachEthToSocket(&w5500); attachSDCardToFat(&sd); w5500.getMAC (ip); uart1.printf("\r\nmac : %02x.%02x.%02x.%02x.%02x.%02x\r\n", ip[0],ip[1],ip[2],ip[3],ip[4],ip[5]); w5500.getIP (ip); uart1.printf("IP : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]); w5500.getSubnet(ip); uart1.printf("mask : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]); w5500.getGateway(ip); uart1.printf("GW : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]); uart1.printf("Network is ready.\r\n"); if(udp1.begin(0,30000) == 0) uart1.printf("\r\nudp1 server creat ok! listen on 30000"); uart1.printf("\r\nret = %d",ret); ret = sd.getCID(buf); uart1.printf("\r\nret = %d",ret); uart1.printf((const char*)buf); ret = sd.getCSD(buf); uart1.printf("\r\nret = %d",ret); rl = sd.getCapacity(); x = (float)rl; uart1.printf("\r\n容量 = %f",x/1024/1024); res=f_getfree("/",&free_clust,&fss); uart1.printf("该分区所有扇区数为:%d\r\n",(fss->max_clust-2)*(fss->csize)); if(res==FR_OK) { uart1.printf("该分区所有扇区数为:%d\r\n",(fss->max_clust-2)*(fss->csize)); uart1.printf("该分区大小为:%dM\r\n",(fss->max_clust-2)*(fss->csize)/2048); uart1.printf("该分区空簇数为:%d\r\n",free_clust); uart1.printf("该分区空扇区数为:%d\r\n",free_clust*(fss->csize)); } else uart1.printf("获取分区空簇失败\r\n,res = %d",res); uart1.printf("\r\n"); }
int chatClient::dojoin(string &rs_ip, int &rs_port, UDP &listener){ string outmsg; int outlen, saddr_len, recvlen, MAX_MSG_LEN=2048; char recvMsg[2048]; const string myName,myIP; int myPort,myID; s_ip = rs_ip; s_port = rs_port; //setupt the UDP socket UDP joinUDP; joinUDP.setRemoteAddr(s_ip.c_str(),s_port); //args: sequencer's ip, port, myIP, myPort,myName; myMsg message = mmaker.makeJoin(name); msgMaker::serialize(outmsg, outlen, message); if(joinUDP.sendToNACK(outmsg.c_str(),outlen)==-2){ cerr<<"Error! Not able to join to the group... App is about to exit..." <<endl; exit(-1); } while(1){ recvlen = listener.recvFromNACK(recvMsg, MAX_MSG_LEN, myName, myIP, myPort, myID); string reMsg(recvMsg,recvlen); msgParser tempParser(reMsg.c_str(),recvlen); processMSG(reMsg.c_str(),recvlen); if(tempParser.msgTypeIs()==join_ack){ rs_ip = s_ip; rs_port = s_port; break; } } return 1; }
void openSocket() { udp.stop(); // Get OpenThread interface index (OpenThread interface is named "th1" on all Mesh devices) uint8_t idx = 0; if_name_to_index("th1", &idx); Log.trace("th1 index = %u", idx); // Create UDP socket and bind to OpenThread interface auto ur = udp.begin(PORT, idx); Log.trace("UDP.begin() = %u", ur); // Subscribe to ff03::1:1001 HAL_IPAddress addr = {}; addr.v = 6; inet_inet_pton(AF_INET6, MULTICAST_ADDR, addr.ipv6); mcastAddr = addr; int r = udp.joinMulticast(mcastAddr); Log.trace("Subscribed to %s: %d", MULTICAST_ADDR, r); }
/* executes continuously after setup() runs */ void loop() { static system_tick_t last = 0; ssize_t s = udp.receivePacket(buf, sizeof(buf)); if (s > 0) { char tmp[IP_ADDR_STRLEN_MAX] = {}; auto ip = udp.remoteIP(); Log.trace("Received %d bytes from %s#%u", s, inet_inet_ntop(AF_INET6, ip.raw().ipv6, tmp, sizeof(tmp)), udp.remotePort()); } if ((millis() - last) >= PERIOD) { size_t len = random(1, sizeof(buf)); rng.gen((char*)buf, len); auto r = udp.sendPacket(buf, len, mcastAddr, PORT); Log.trace("Sent %u bytes to %s#%u : %d %d", len, MULTICAST_ADDR, PORT, r, errno); last = millis(); if (r < 0) { openSocket(); } } }
TEST_F(MatchesResponseTest, DHCPv6) { uint8_t request[] = { 51, 51, 0, 1, 0, 2, 0, 2, 179, 193, 64, 207, 134, 221, 96, 0, 0, 0, 0, 52, 17, 128, 254, 128, 0, 0, 0, 0, 0, 0, 2, 2, 179, 255, 254, 193, 64, 207, 255, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 2, 34, 2, 35, 0, 52, 243, 80, 1, 0, 83, 101, 0, 1, 0, 14, 0, 1, 0, 6, 17, 195, 100, 52, 0, 22, 118, 189, 211, 18, 0, 3, 0, 12, 0, 0, 0, 1, 255, 255, 255, 255, 255, 255, 255, 255, 0, 8, 0, 2, 0, 100 }; uint8_t reply[] = { 0, 2, 179, 193, 64, 207, 0, 21, 242, 4, 141, 219, 134, 221, 96, 0, 0, 0, 0, 209, 17, 128, 254, 128, 0, 0, 0, 0, 0, 0, 2, 21, 242, 255, 254, 4, 141, 219, 254, 128, 0, 0, 0, 0, 0, 0, 2, 2, 179, 255, 254, 193, 64, 207, 2, 35, 2, 34, 0, 209, 127, 59, 2, 0, 83, 101, 0, 1, 0, 14, 0, 1, 0, 6, 17, 195, 100, 52, 0, 22, 118, 189, 211, 18, 0, 3, 0, 132, 0, 0, 0, 1, 0, 0, 14, 16, 0, 0, 21, 24, 0, 5, 0, 24, 102, 0, 0, 0, 0, 0, 0, 0, 213, 240, 138, 111, 157, 131, 93, 191, 0, 1, 81, 128, 0, 2, 163, 0, 0, 13, 0, 88, 0, 0, 49, 32, 97, 100, 100, 114, 101, 115, 115, 32, 103, 114, 97, 110, 116, 101, 100, 46, 32, 89, 111, 117, 32, 109, 97, 121, 32, 105, 110, 99, 108, 117, 100, 101, 32, 73, 65, 65, 68, 68, 82, 32, 105, 110, 32, 73, 65, 32, 111, 112, 116, 105, 111, 110, 44, 32, 105, 102, 32, 121, 111, 117, 32, 119, 97, 110, 116, 32, 116, 111, 32, 112, 114, 111, 118, 105, 100, 101, 32, 97, 32, 104, 105, 110, 116, 46, 0, 2, 0, 14, 0, 1, 0, 1, 17, 195, 197, 84, 0, 224, 129, 73, 16, 201, 0, 7, 0, 1, 0, 0, 12, 0, 16, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6 }; EthernetII pkt(request, sizeof(request)); UDP *udp = pkt.find_pdu<UDP>(); const RawPDU *raw = pkt.find_pdu<RawPDU>(); ASSERT_TRUE(udp != NULL); ASSERT_TRUE(raw != NULL); udp->inner_pdu(raw->to<DHCPv6>()); EXPECT_TRUE(pkt.matches_response(reply, sizeof(reply))); EXPECT_FALSE(pkt.matches_response(request, sizeof(request))); }
void cyclic_task() { head.get_control = full_Controller; udptoFPGA.Zip_packet(); if(udptoFPGA.Send()){ if(udptoFPGA.Received()){ udptoFPGA.Unzip_packet(); }//udp.Received }//udptoFPGA.Send() //Talk to the controllers //send tar // = tar.target_velocity; // receave cur // cur.position = ; // cur.actual_velocity = ; }
// // UDP stuff // void sendUDPmessage( String msg, int port = 5100 ) { IPAddress multicast( 225, 1, 1, 1 ); UDP server; if ( server.begin( port ) ) { server.beginPacket( multicast, port ); server.write( msg ); server.endPacket(); server.stop(); } }
void setup() { eBoxInit(); uart1.begin(9600); w5500.begin(mac,lip,sub,gw); w5500.attchInterruputEvent(ethEvent); attachEthToSocket(&w5500); w5500.getMAC (ip); uart1.printf("mac : %02x.%02x.%02x.%02x.%02x.%02x\r\n", ip[0],ip[1],ip[2],ip[3],ip[4],ip[5]); w5500.getIP (ip); uart1.printf("IP : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]); w5500.getSubnet(ip); uart1.printf("mask : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]); w5500.getGateway(ip); uart1.printf("GW : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]); uart1.printf("Network is ready.\r\n"); if(udp.begin(0,30000) == 0) uart1.printf("\r\nudp1 server creat ok! listen on 30000"); }
int WolClient::wake(const char* mac) { uint8_t contents[MAGIC_HEADER_LENGTH + REPEAT_MAC * MAC_BYTES]; uint8_t rawMac[MAC_BYTES]; parseMacAddress(mac, rawMac); UDP udp; udp.begin(port); udp.beginPacket(broadcastIP, port); for (int i = 0; i < MAGIC_HEADER_LENGTH; i++) { contents[i] = 0xFF; } for (uint8_t i = MAGIC_HEADER_LENGTH; i < sizeof contents; i++) { contents[i] = rawMac[(i - MAGIC_HEADER_LENGTH) % MAC_BYTES]; } udp.write(contents, sizeof contents); udp.endPacket(); udp.stop(); return TRUE; }
TEST_F(UDPTest, DefaultContructor) { UDP udp; EXPECT_EQ(udp.dport(), 0); EXPECT_EQ(udp.sport(), 0); EXPECT_FALSE(udp.inner_pdu()); }
void UDPTest::test_equals(const UDP& udp1, const UDP& udp2) { EXPECT_EQ(udp1.dport(), udp2.dport()); EXPECT_EQ(udp1.sport(), udp2.sport()); EXPECT_EQ(udp1.length(), udp2.length()); EXPECT_EQ(udp1.size(), udp2.size()); EXPECT_EQ(udp1.header_size(), udp2.header_size()); EXPECT_EQ(bool(udp1.inner_pdu()), bool(udp2.inner_pdu())); }
void HapiliMessage::Serialize(UDP& to) { to.write((byte*)&header, sizeof(Header)); }
TEST_F(UDPTest, SPort) { UDP udp; uint16_t port = 0x1234; udp.sport(port); ASSERT_EQ(udp.sport(), port); }
TEST_F(UDPTest, Length) { UDP udp; uint16_t length = 0x1234; udp.length(length); ASSERT_EQ(udp.length(), length); }
TEST_F(UDPTest, PDUType) { UDP udp; EXPECT_EQ(udp.pdu_type(), PDU::UDP); }
//fuction actually used to process the message. //it will analyze the header of the message and take actions accordingly. int chatClient::processMSG(const char* msg, int mlen) { peer newUser; string newName, newIP, textmsg, outmsg; int newPort, newID; int outlen,i; int seqNum =0; myMsg tempMsg; vector<peer>::iterator aIter; msgParser parser(msg, mlen); if(parser.isACK()){ //return 0; //since all the UDP_ACKs are handled in the underlaying UDP-wrapper layer, we simply //ignore the this message. } else{ switch (parser.msgTypeIs()) { case join: if(status!=ELEC){ status=NORMAL; //setup and send a Navi message mmaker.setInfo(sname,s_ip, s_port,s_id); tempMsg = mmaker.makeNavi(); msgMaker::serialize(outmsg,outlen,tempMsg); parser.senderInfo(newIP,newName,newPort,newID); UDP joinUDP; joinUDP.setRemoteAddr(newIP.c_str(),newPort); joinUDP.sendToNACK(outmsg.c_str(),outlen); mmaker.setInfo(name,IP, port,C_ID); return 1; } else{ //receive a Join request message during the election, //push it into message queue and //handle it later on. inMsgQ.push(msg); return 1; } break; case navi: //the chatClient get a navi message, which means the one //he asked is not the sequencer, and the info of the //sequencer is returned via this message. //get the info of the sequencer and send another join //message to it. parser.senderInfo(newIP, newName, newPort,newID); dojoin(newIP, newPort); return 1; break; case join_ack: //get the peerlist and client_id decided by the sequencer //and store them locally for future use. parser.joinFeedback(msgMaxCnt, C_ID, clientList); parser.senderInfo(newIP,newName,newPort,newID); s_id = newID; s_ip = newIP; s_port = newPort; sname = newName; clntUDP.setRemoteAddr(s_ip.c_str(), s_port); //call the setInfo again so as to set the C_ID field mmaker.setInfo(name,IP, port,C_ID); displayClients(); status = NORMAL; return 1; break; case join_broadcast: //get the ip ,port name and client ID of the new user //and store them locally. if(status!=ELEC){ status=NORMAL; parser.senderInfo(newIP, newName, newPort, newID); if(newID==C_ID){ return 1; } parser.joinName(newName); memset(&newUser, 0, sizeof(peer)); memcpy(newUser.name,newName.c_str(),newName.size()); memcpy(newUser.ip, newIP.c_str(), newIP.size()); newUser.c_id = newID; newUser.port = newPort; clientList.push_back(newUser); cout<<"NOTICE "<<newUser.name<<" joined on " <<newUser.ip<<":"<<newUser.port<<endl; return 1; } else{ //in wrong state, push the message into message queue //and handle later on cerr<<"Unexpected Join Broadcast message @"<<name <<" !"<<endl; string temps(msg,mlen); inMsgQ.push(temps); return -1; } break; case leave_broadcast:{ //remove the specific user from the peer list vector<peer> timeoutList; if(status!=ELEC){ status=NORMAL; parser.senderInfo(newIP, newName, newPort,newID); for(aIter = clientList.begin(); aIter != clientList.end(); aIter++){ if((*aIter).c_id==newID){ cout<<"NOTICE "<<aIter->name <<" left the chat or crashed"<<endl; clientList.erase(aIter); break; } } //sequencer leaves if (newID==s_id) { for(aIter = clientList.begin(); aIter != clientList.end(); aIter++){ if((*aIter).c_id==newID){ clientList.erase(aIter); cout<<"NOTICE "<<aIter->name <<" left the chat or crashed"<<endl; break; } } if(doElection()==1){ return 10; } else return -10; } } else{ cerr<<"Unexpected Leave Broadcast message @"<<name <<" !"<<endl; string temps(msg,mlen); inMsgQ.push(temps); return -1; } break; } case msg_broadcast: //show the message to the standard output if(status!=ELEC){ status =NORMAL; if(parser.getMsg(seqNum,textmsg)<0){ cerr<<"failed to get the message content! @" <<name<<endl; exit(-1); } //normal case if(seqNum==(msgMaxCnt+1)){ cout<<textmsg<<endl; msgMaxCnt++; } //receive duplicated message, ignore. else if(seqNum<(msgMaxCnt+1)){ //ignore; } //missed earlier messages, search & wait for retransimit. else if(seqNum>(msgMaxCnt+1)){ dspMsg.insert(pair<int,string>(seqNum,textmsg)); it = dspMsg.find((msgMaxCnt+1)); while(it!=dspMsg.end()){ cout<<(*it).second<<endl; dspMsg.erase(it); msgMaxCnt++; it = dspMsg.find((msgMaxCnt+1)); } } return 1; } else{ cerr<<"Unexpected msg Broadcast message @"<<name <<" !"<<endl; string temps(msg,mlen); inMsgQ.push(temps); return -1; } break; case election_req:{ //do BULLY if(status==NORMAL){ parser.senderInfo(newIP, newName, newPort,newID); if(C_ID>=newID){ if(doElection()>0){ return 10; } else return -10; } } else return -1; break; } case election_ok: //do BULLY if(status!=ELEC){ cerr<<"Unexpected msg election ok message @"<<name <<" !"<<endl; exit(-1); } else{ electWin=false; } return 1; break; case leader_broadcast: //get and setup the info of the new sequencer //here we just ignore the state which this client is in. //Cause it's safe //to change the sequencer here. if(status==ELEC_CLIENT||status==NORMAL){ status = NORMAL; parser.senderInfo(newIP, newName, newPort,newID); if(newID!=C_ID&&newID!=s_id){ s_ip = newIP; s_port = newPort; s_id = newID; sname = newName; pthread_mutex_lock(&udpMutex); clntUDP.updateSocket(s_ip.c_str(),s_port); pthread_mutex_unlock(&udpMutex); return 9; } }return 1; break; default: //not used here. break; } } }