The Receive Callback
The receive callback ถูกเรียก, เมื่อ the server รับบาง data จาก the client. ต่อไปนี้แสดงวิธีจัดการ data นี้.
1. ก่อนอื่นผมกำลังสร้าง a new Packet Buffer (
txBuf), ซึ่งจะถูกใช้ส่ง the data.
2.
char *remoteIP = ipaddr_ntoa(addr); แปลง the IP address จาก integer format เป็น the regular IP format.
3. นี่เป็นเพียงกรณีที่เราต้องการใช้ the address and port ของ the client.
4. ต่อไปฉันกำลังผสม the incoming data กับ some additional data.
5. เพื่อส่ง data ใหม่นี้ไปยัง the client, สิ่งแรกเราจำเป็นต้องจัดสรรบาง memory สำหรับ the packet buffer.
6. นี้สามารถถูกทำด้วย
pbuf_alloc.
7. จากนั้นเราจะคัดลอก the data ลงใน the packet buffer โดยใช้
pbuf_take.
8. ถัดไปเราจะเชื่อมต่อถึง the client.
9. นี้สามารถถูกทำโดยการใช้
udp_connect. The parameters, address and port ของ the client, เป็นส่วนหนึ่งของ the
function, เมื่อ
udp_connect กำลังถูกเรียก.
10. จากนั้นเราส่ง the data โดยใช้ the
udp_send(upcb, txBuf); 11. ที่นี่ upcb คือ the control block ซึ่งมี the information ทั้งหมดเกี่ยวกับ the local and remote connection.
12.
txBuf คือ the packet buffer, และมันมี the information ทั้งหมดเกี่ยวกับ the data.
13. เมื่อ the data ถูกส่งแล้ว, เราจะยกเลิกการเชื่อมต่อ the client, ดังนั้น a new client สามารถเชื่อมต่อได้.
14. และในท้ายสุดเราจะปล่อยฟรี the memories ทั้งหมด.