void* dabInputSlipThread(void* args) { dabInputSlipData* data = (dabInputSlipData*)args; TcpSocket* client; while ((client = data->server->accept()) != NULL) { int size = 0; etiLog.log(info, "SLIP server got a new client.\n"); #ifdef _WIN32 WaitForSingleObject(data->semWrite, INFINITE); WaitForSingleObject(data->semQueue, INFINITE); #else sem_wait(&data->semWrite); sem_wait(&data->semQueue); #endif UdpPacket* packet = data->packets[data->nbPackets]; #ifdef _WIN32 ReleaseSemaphore(data->semQueue, 1, NULL); #else sem_post(&data->semQueue); #endif while ((size = client->read(packet->getData(), packet->getSize())) > 0) { packet->setLength(size); #ifdef _WIN32 WaitForSingleObject(data->semQueue, INFINITE); #else sem_wait(&data->semQueue); #endif data->nbPackets++; #ifdef _WIN32 ReleaseSemaphore(data->semQueue, 1, NULL); #else sem_post(&data->semQueue); #endif #ifdef _WIN32 WaitForSingleObject(data->semWrite, INFINITE); WaitForSingleObject(data->semQueue, INFINITE); #else sem_wait(&data->semWrite); sem_wait(&data->semQueue); #endif packet = data->packets[data->nbPackets]; #ifdef _WIN32 ReleaseSemaphore(data->semQueue, 1, NULL); #else sem_post(&data->semQueue); #endif } etiLog.log(info, "SLIP server client deconnected.\n"); client->close(); } etiLog.log(error, "SLIP thread can't accept new client (%s)\n", inetErrDesc, inetErrMsg); return NULL; }
int UdpSocket::send(UdpPacket& packet) { int ret = sendto(listenSocket, packet.getData(), packet.getSize(), 0, packet.getAddress().getAddress(), sizeof(*packet.getAddress().getAddress())); if (ret == SOCKET_ERROR && errno != ECONNREFUSED) { setInetError("Can't send UDP packet"); return -1; } return 0; }
/** * Send an UDP packet. * @param packet The UDP packet to be sent. It includes the data and the * destination address * return 0 if ok, -1 if error */ int UdpSocket::send(UdpPacket &packet) { #ifdef DUMP TRACE_CLASS("UdpSocket", "send(UdpPacket)"); #endif int ret = sendto(listenSocket, packet.getData(), packet.getLength(), 0, packet.getAddress().getAddress(), sizeof(*packet.getAddress().getAddress())); if (ret == SOCKET_ERROR #ifndef _WIN32 && errno != ECONNREFUSED #endif ) { setInetError("Can't send UDP packet"); return -1; } return 0; }
//----------------------------------------------------------------------------- // 描述: 收到数据包 //----------------------------------------------------------------------------- void MainUdpServer::onRecvData(void *packetBuffer, int packetSize, const InetAddress& peerAddr) { int groupIndex; // 先进行数据包分类,得到组别号 iseApp().iseBusiness().classifyUdpPacket(packetBuffer, packetSize, groupIndex); // 如果组别号合法 if (groupIndex >= 0 && groupIndex < requestGroupCount_) { UdpPacket *p = new UdpPacket(); if (p) { p->recvTimestamp_ = time(NULL); p->peerAddr_ = peerAddr; p->packetSize_ = packetSize; p->setPacketBuffer(packetBuffer, packetSize); // 添加到请求队列中 requestGroupList_[groupIndex]->getRequestQueue().addPacket(p); } } }
/** * Receive an UDP packet. * @param packet The packet that will receive data. The address will be set * to the source address. * @return 0 if ok, -1 if error */ int UdpSocket::receive(UdpPacket &packet) { TRACE_CLASS("UdpSocket", "receive(UdpPacket)"); socklen_t addrSize; addrSize = sizeof(*packet.getAddress().getAddress()); int ret = recvfrom(listenSocket, packet.getData(), packet.getSize() - packet.getOffset(), 0, packet.getAddress().getAddress(), &addrSize); if (ret == SOCKET_ERROR) { packet.setLength(0); #ifndef _WIN32 if (errno == EAGAIN) return 0; #endif setInetError("Can't receive UDP packet"); return -1; } packet.setLength(ret); if (ret == (long)packet.getSize()) { packet.setSize(packet.getSize() << 1); } return 0; }
int UdpSocket::receive(UdpPacket& packet) { socklen_t addrSize; addrSize = sizeof(*packet.getAddress().getAddress()); ssize_t ret = recvfrom(listenSocket, packet.getData(), packet.getSize(), 0, packet.getAddress().getAddress(), &addrSize); if (ret == SOCKET_ERROR) { packet.setSize(0); if (errno == EAGAIN) { return 0; } setInetError("Can't receive UDP packet"); return -1; } packet.setSize(ret); return 0; }