// send state packet, xor packet if applicable int network_state_send( void ) { #ifdef __BLACKBERRY__ #else if (!SDLNet_UDP_Send(socket, 0, packet_state_out[0])) { printf("SDLNet_UDP_Send: %s\n", SDL_GetError()); return -1; } // send xor of last network_delay packets if (network_delay > 1 && (last_state_out_sync + 1) % network_delay == 0 && packet_state_out[network_delay - 1] != NULL) { packet_copy(packet_temp, packet_state_out[0]); SDLNet_Write16(PACKET_STATE_XOR, &packet_temp->data[0]); for (int i = 1; i < network_delay; i++) for (int j = 4; j < packet_temp->len; j++) packet_temp->data[j] ^= packet_state_out[i]->data[j]; if (!SDLNet_UDP_Send(socket, 0, packet_temp)) { printf("SDLNet_UDP_Send: %s\n", SDL_GetError()); return -1; } } packets_shift_down(packet_state_out, NET_PACKET_QUEUE); last_state_out_sync++; #endif return 0; }
static void sendIPXPacket(Bit8u *buffer, Bit16s bufSize) { Bit16u srcport, destport; Bit32u srchost, desthost; Bit16u i; Bits result; UDPpacket outPacket; outPacket.channel = -1; outPacket.data = buffer; outPacket.len = bufSize; outPacket.maxlen = bufSize; IPXHeader *tmpHeader; tmpHeader = (IPXHeader *)buffer; srchost = tmpHeader->src.addr.byIP.host; desthost = tmpHeader->dest.addr.byIP.host; srcport = tmpHeader->src.addr.byIP.port; destport = tmpHeader->dest.addr.byIP.port; if(desthost == 0xffffffff) { // Broadcast for(i=0;i<SOCKETTABLESIZE;i++) { if(connBuffer[i].connected && ((ipconn[i].host != srchost)||(ipconn[i].port!=srcport))) { outPacket.address = ipconn[i]; result = SDLNet_UDP_Send(ipxServerSocket,-1,&outPacket); if(result == 0) { LOG_MSG("IPXSERVER: %s", SDLNet_GetError()); continue; } //LOG_MSG("IPXSERVER: Packet of %d bytes sent from %d.%d.%d.%d to %d.%d.%d.%d (BROADCAST) (%x CRC)", bufSize, CONVIP(srchost), CONVIP(ipconn[i].host), packetCRC(&buffer[30], bufSize-30)); } } } else { // Specific address for(i=0;i<SOCKETTABLESIZE;i++) { if((connBuffer[i].connected) && (ipconn[i].host == desthost) && (ipconn[i].port == destport)) { outPacket.address = ipconn[i]; result = SDLNet_UDP_Send(ipxServerSocket,-1,&outPacket); if(result == 0) { LOG_MSG("IPXSERVER: %s", SDLNet_GetError()); continue; } //LOG_MSG("IPXSERVER: Packet sent from %d.%d.%d.%d to %d.%d.%d.%d", CONVIP(srchost), CONVIP(desthost)); } } } }
static void sendIPXPacket(Bit8u *buffer, Bit16s bufSize) { Bit16u srcport, destport; Bit32u srchost, desthost; Bit16u i; Bits result; UDPpacket outPacket; outPacket.channel = -1; outPacket.data = buffer; outPacket.len = bufSize; outPacket.maxlen = bufSize; IPXHeader *tmpHeader; tmpHeader = (IPXHeader *)buffer; srchost = tmpHeader->src.addr.byIP.host; desthost = tmpHeader->dest.addr.byIP.host; srcport = tmpHeader->src.addr.byIP.port; destport = tmpHeader->dest.addr.byIP.port; // update activity timer for sending host for(i=0;i<SOCKETTABLESIZE;i++) { if(connBuffer[i].connected && (ipconn[i].host == srchost) && (ipconn[i].port==srcport)) { connBuffer[i].lastused = time(NULL); } } if(desthost == 0xffffffff) { // Broadcast for(i=0;i<SOCKETTABLESIZE;i++) { if(connBuffer[i].connected && ((ipconn[i].host != srchost)||(ipconn[i].port!=srcport))) { outPacket.address = ipconn[i]; result = SDLNet_UDP_Send(ipxServerSocket,-1,&outPacket); if(result == 0) { LOG_MSG("IPXSERVER: %s", SDLNet_GetError()); continue; } } } } else { // Specific address for(i=0;i<SOCKETTABLESIZE;i++) { if((connBuffer[i].connected) && (ipconn[i].host == desthost) && (ipconn[i].port == destport)) { outPacket.address = ipconn[i]; result = SDLNet_UDP_Send(ipxServerSocket,-1,&outPacket); if(result == 0) { LOG_MSG("IPXSERVER: %s", SDLNet_GetError()); continue; } } } } }
// Send data. int net_send( const char *msg ) { // Set the data // UDPPacket::data is an Uint8, which is similar to char* // This means we can't set it directly. // // std::stringstreams let us add any data to it using << ( like std::cout ) // We can extract any data from a std::stringstream using >> ( like std::cin ) // //str memcpy(gPacket->data, msg, strlen(msg)); gPacket->len = strlen(msg); printf("==========================================================================================================\n"); printf(" Sending %d char : %s \n",gPacket->len,gPacket->data); // Send // SDLNet_UDP_Send returns number of packets sent. 0 means error if ( SDLNet_UDP_Send(gOurSocket, -1, gPacket) == 0 ) { printf( "\tSDLNet_UDP_Send failed : %s\n" ,SDLNet_GetError()); return 0; } else return gPacket->len; }
// // PacketSend // void PacketSend(void) { int c; doomdata_t* sw; sw = (doomdata_t*)packet->data; sw->checksum = host_to_net32(netbuffer->checksum); sw->player = netbuffer->player; sw->retransmitfrom = netbuffer->retransmitfrom; sw->starttic = netbuffer->starttic; sw->numtics = netbuffer->numtics; for (c = 0; c < netbuffer->numtics; ++c) { sw->cmds[c].forwardmove = netbuffer->cmds[c].forwardmove; sw->cmds[c].sidemove = netbuffer->cmds[c].sidemove; sw->cmds[c].angleturn = host_to_net16(netbuffer->cmds[c].angleturn); sw->cmds[c].consistancy = host_to_net16(netbuffer->cmds[c].consistancy); sw->cmds[c].chatchar = netbuffer->cmds[c].chatchar; sw->cmds[c].buttons = netbuffer->cmds[c].buttons; } packet->len = doomcom->datalength; packet->address = sendaddress[doomcom->remotenode]; if (!SDLNet_UDP_Send(udpsocket, -1, packet)) I_Error("Error sending packet: %s", SDLNet_GetError()); }
int udpsend(UDPsocket sock, int channel, UDPpacket *out, UDPpacket *in, Uint32 delay, Uint8 expect, int timeout) { Uint32 t,t2; int err; in->data[0]=0; t=SDL_GetTicks(); do { t2=SDL_GetTicks(); if(t2-t>(Uint32) timeout) { printf("timed out\n"); return(0); } if(!SDLNet_UDP_Send(sock, channel, out)) { printf("SDLNet_UDP_Send: %s\n",SDLNet_GetError()); exit(1); } err=SDLNet_UDP_Recv(sock, in); if(!err) SDL_Delay(delay); } while(!err || (in->data[0]!=expect && in->data[0]!=ERROR)); if(in->data[0]==ERROR) printf("received error code\n"); return(in->data[0]==ERROR?-1:1); }
static void ackClient(IPaddress clientAddr) { IPXHeader regHeader; UDPpacket regPacket; Bits result; SDLNet_Write16(0xffff, regHeader.checkSum); SDLNet_Write16(sizeof(regHeader), regHeader.length); SDLNet_Write32(0, regHeader.dest.network); PackIP(clientAddr, ®Header.dest.addr.byIP); SDLNet_Write16(0x2, regHeader.dest.socket); SDLNet_Write32(1, regHeader.src.network); PackIP(ipxServerIp, ®Header.src.addr.byIP); SDLNet_Write16(0x2, regHeader.src.socket); regHeader.transControl = 0; regPacket.data = (Uint8 *)®Header; regPacket.len = sizeof(regHeader); regPacket.maxlen = sizeof(regHeader); regPacket.address = clientAddr; // Send registration string to client. If client doesn't get this, client will not be registered result = SDLNet_UDP_Send(ipxServerSocket,-1,®Packet); }
//! \return number of targets int UDPNetworkSocket::sendData(const uint8_t * _data, size_t _dataSize) { if (!isOpen() || data->targets.empty() ) return 0; int sendCounter = 0; #ifdef UTIL_HAVE_LIB_SDL2_NET UDPpacket * p = SDLNet_AllocPacket(_dataSize); if (p == nullptr) { std::cerr << "SDLNet_AllocPacket: " << SDLNet_GetError() << "\n"; return sendCounter; } std::copy(_data, _data + _dataSize, p->data); p->len = _dataSize; for(const auto & target : data->targets) { p->address = toSDLIPv4Address(target); sendCounter += SDLNet_UDP_Send(data->udpsock, -1, p); } SDLNet_FreePacket(p); p = nullptr; #elif defined(__linux__) || defined(__unix__) || defined(ANDROID) for(const auto & target : data->targets) { sockaddr_in sockAddr = toSockaddr(target); ssize_t bytesSent = sendto(data->udpSocket, _data, _dataSize, 0, reinterpret_cast<const sockaddr *> (&sockAddr), sizeof(sockaddr_in)); if (bytesSent == -1) { int error = errno; WARN(std::string(strerror(error))); } else { ++sendCounter; } } #endif return sendCounter; }
bool UDPNetworkSocket::sendData(const uint8_t * _data, size_t _dataSize, const IPv4Address & _address) { if (!isOpen()) return false; #ifdef UTIL_HAVE_LIB_SDL2_NET UDPpacket * p = SDLNet_AllocPacket(_dataSize); std::copy(_data, _data + _dataSize, p->data); p->len = _dataSize; p->address = toSDLIPv4Address(_address); int i = SDLNet_UDP_Send(data->udpsock, -1, p); SDLNet_FreePacket(p); p = nullptr; return i > 0; #elif defined(__linux__) || defined(__unix__) || defined(ANDROID) sockaddr_in sockAddr = toSockaddr(_address); ssize_t bytesSent = sendto(data->udpSocket, _data, _dataSize, 0, reinterpret_cast<const sockaddr *> (&sockAddr), sizeof(sockAddr)); if (bytesSent == -1) { int error = errno; WARN(std::string(strerror(error))); return false; } return true; #else return false; #endif }
int_ net_ip_t::send_now(net_ip_write_buffer_t *data){ int_ return_value = 0; net_ip_connection_info_t *tmp_conn = (net_ip_connection_info_t*)find_pointer(data->connection_info_id, "net_ip_connection_info_t"); if(outbound == NULL){ printf("Cannot use outbound port. Check to see if you have proper permissions to use raw sockets\n"); return_value = -1; return return_value; } uint_ position = 0; if(tmp_conn != nullptr){ std::vector<std::string> raw_packets; std::string data_prefix = wrap(NET_PACKET_ID_START, std::to_string(data->packet_id), NET_PACKET_ID_END); if(data->data.size() > NET_MTU-NET_MTU_OVERHEAD){ while(data->data != ""){ uint_ chunk = NET_MTU-NET_MTU_OVERHEAD; std::string pos_prefix = wrap(NET_PACKET_POS_START, std::to_string( position), NET_PACKET_POS_END); std::string tmp_string = data_prefix + pos_prefix + data->data.substr(0,chunk); raw_packets.push_back(tmp_string); if(chunk > data->data.size()){ break; } data->data = data->data.substr(chunk, data->data.size()); position++; } }else{ std::string tmp; tmp = wrap(NET_PACKET_ID_START, std::to_string(data->packet_id), NET_PACKET_ID_END); tmp += wrap(NET_PACKET_POS_START, "0", NET_PACKET_POS_END); tmp += data->data; raw_packets.push_back(tmp); } const uint_ raw_packets_size = raw_packets.size(); raw_packets[0] = NET_PACKET_START + raw_packets[0]; raw_packets[raw_packets_size-1] += NET_PACKET_END; IPaddress IP; SDLNet_ResolveHost(&IP, tmp_conn->ip.c_str(), (unsigned short int)tmp_conn->port); outbound_packet->address.host = IP.host; outbound_packet->address.port = IP.port; uint_ total_byte_size = 0; const uint_ max_total_sent_byte = 8*KILOBYTE_TO_BYTE; for(uint_ i = 0;i < raw_packets_size;i++){ negative_to_positive_trick(&raw_packets[i]); unsigned char* outbound_data = (unsigned char *)raw_packets[i].c_str(); outbound_packet->len = (int)(raw_packets[i].size()+1); outbound_packet->data = outbound_data; SDLNet_UDP_Send(outbound->socket, -1, outbound_packet); total_byte_size += outbound_packet->len; if(unlikely(total_byte_size > max_total_sent_byte)){ // no packet loss if the client is sending data to itself total_byte_size = 0; loop_receive(); } } raw_packets.clear(); }else{ printf_("The connection ID (" + std::to_string(data->connection_info_id) + ") does not match up with anything here\n", PRINTF_STATUS); } return return_value; }
// Win32 (at least, Win 98) seems to accept 255.255.255.255 as a valid broadcast address. // I'll live with that for now. // Also doing that for the BeOS and Mac Classic int SDLNetx_UDP_Broadcast(UDPsocket inSocket, UDPpacket* inPacket) { Uint32 theSavedHostAddress = inPacket->address.host; inPacket->address.host = 0xffffffff; int theResult = SDLNet_UDP_Send(inSocket, -1, inPacket); inPacket->address.host = theSavedHostAddress; return theResult; }
void I_SendPacketTo(packet_header_t* packet, size_t len, UDP_CHANNEL *to) { printf("I_SendPacketTo Packet=%p, len=%d, Dest=%p\n", packet, len, to); packet->checksum = ChecksumPacket(packet, len); memcpy(udp_packet->data, packet, udp_packet->len = len); SDLNet_UDP_Send(udp_socket, *to, udp_packet); }
bool UdpConnection::send() { // returns number of packets sent - 0 is error. if (SDLNet_UDP_Send(socket, -1, packet) == 0 ) { std::cout << "Failed to send packet: " << SDLNet_GetError() << "\n"; return false; } return true; }
int network_udp_send(UDPsocket udp, int channel, UDPpacket* packet) { int sent = 0; sent = SDLNet_UDP_Send(udp, channel, packet); if (sent == 0) { std::cerr << "Failed to send packet over UDP: " << SDLNet_GetError() << "\n"; } return sent; }
void Server::SendData(Uint8* data_, int length) { output->address.host = input->address.host; output->address.port = input->address.port; output->channel = input->channel; output->data = data_; output->len = length; numsend = SDLNet_UDP_Send(sock, output->channel, output); }
int NET::UDPSend(UDPpacket * p, int channel) { datacounter_tx += p->len; if (!SDLNet_UDP_Send(remote_socket, channel, p)) return UDP_FAILURE; else return UDP_SUCCESS; }
static void send_packet (char *msg, int data_size) { send_p->address.host = srvaddr.host; send_p->address.port = srvaddr.port; memcpy (send_p->data, msg, data_size); send_p->len = data_size; SDLNet_UDP_Send (socket, -1, send_p); }
int main(void) { struct UDPsopa sopa = { .sop = NULL, .pap = NULL, .port = 0, // 0 = pick unused port .packet_size = 512, .packet_num = 0, .host_port = 4444 }; strcpy(sopa.host_name, "127.0.0.1"); if (udp_setup(&sopa) != 0) { udp_cleanup(&sopa); exit(EXIT_FAILURE); } // additional client setup if (SDLNet_ResolveHost(&(sopa.pap->address), sopa.host_name, sopa.host_port) != 0) { printf("SDLNet_ResolveHost(&(sopa.pap->address), sopa.host_name, sopa.host_port) != 0\n"); printf("SDLNet_GetError() = '%s'\n", SDLNet_GetError()); udp_cleanup(&sopa); exit(EXIT_FAILURE); } srand(123); for (;;) { sleep(1); // 1 second sprintf((char*)sopa.pap->data, "rand_num = %d", rand()); sopa.pap->len = strlen((char*)sopa.pap->data) + 1; const int channel = -1; // -1 = use packet's address as destination if (SDLNet_UDP_Send(sopa.sop, channel, sopa.pap) == 0) { printf("SDLNet_UDP_Send(sopa.sop, channel, sopa.pap) == 0\n"); printf("SDLNet_GetError() = '%s'\n", SDLNet_GetError()); udp_cleanup(&sopa); exit(EXIT_FAILURE); } switch (SDLNet_UDP_Recv(sopa.sop, sopa.pap)) { case -1: printf("SDLNet_UDP_Recv(sopa.sop, sopa.pap) == -1\n"); printf("SDLNet_GetError() = '%s'\n", SDLNet_GetError()); udp_cleanup(&sopa); exit(EXIT_FAILURE); case 0: printf("SDLNet_UDP_Recv(sopa.sop, sopa.pap) == 0 // no packets received\n"); break; case 1: sopa.packet_num++; udp_print(&sopa); break; } } // for(;;) udp_cleanup(&sopa); exit(EXIT_SUCCESS); }
static void NET_Send(void) { if (!doomcom->remotenode) return; mypacket.len = doomcom->datalength; if (SDLNet_UDP_Send(mysocket,doomcom->remotenode-1,&mypacket) == 0) { CONS_Printf("SDL_Net: %s",SDLNet_GetError()); } }
void ctx_send(cearth_ctx *ctx) { UDPpacket pack; pack.channel = -1; pack.data = ctx->b_in.data; pack.len = ctx->b_in.wpos; SDLNet_UDP_Send(ctx->sk, pack.channel, &pack); }
static __inline__ int snUDPSend(UDPsocket s, int c, UDPpacket *p) { int val = 0; lockSDLNet(); val = SDLNet_UDP_Send(s, c, p); unlockSDLNet(); return val; }
void sock_send( sock_t* s, pack_t* pck ) { int res_us; assert( s && pck ); res_us = SDLNet_UDP_Send( s->sock, -1, pck->p ); if( res_us == 0 ) printf("[E] Error sending UDP packet\n"); s->bytes_sent += ( pck->p->status > 0 ? pck->p->status : 0 ); pack_destroy(pck); }
OSErr NetDDPSendFrame(DDPFramePtr frame, NetAddrBlock *address, short protocolType, short port) { //fdprintf("NetDDPSendFrame\n"); assert(frame->data_size <= ddpMaxData); sUDPPacketBuffer->channel = -1; memcpy(sUDPPacketBuffer->data, frame->data, frame->data_size); sUDPPacketBuffer->len = frame->data_size; sUDPPacketBuffer->address = *address; return SDLNet_UDP_Send(sSocket, -1, sUDPPacketBuffer) ? 0 : -1; }
/* * network_udp_send() - Send packet data via UDP over the given channel * @udp: the socket to send the data through * @channel: the channel to use * @packet: the data to send */ int network_udp_send(UDPsocket udp, int channel, UDPpacket* packet) { int sent = 0; // Define the amount of destinations the data was sent to sent = SDLNet_UDP_Send(udp, channel, packet); // Attempt to send the data if (sent == 0) { // If the data could not be sent to anyone std::cerr << "Failed to send packet over UDP: " << SDLNet_GetError() << "\n"; // Output the error message return -1; // Return -1 on failure } return sent; // Return the amount of destinations that data was sent to on success }
void I_SendPacket(packet_header_t* packet, size_t len) { printf("I_SendPacket Packet=%p, len=%d\n", packet, len); packet->checksum = ChecksumPacket(packet, len); DumpPacketHeader (packet); memcpy(udp_packet->data, packet, udp_packet->len = len); SDLNet_UDP_Send(udp_socket, 0, udp_packet); }
void createAndSendUDPPackets() { SDL_Rect viewport; viewport.w = SCREEN_WIDTH; viewport.h = SCREEN_HEIGHT; int i, player, secondary, counter,UDPpacketLength; Uint32 tmp; packetId++; for (i=0; i<4; i++) { gameData[i] = packetId >> i*8; } for (player=0; player<MAX_CLIENTS; player++) { tmp=0; tmp = (int)ships[player].xPos | (int)ships[player].yPos << 12 | (int)(ships[player].angle/6) << 24; tmp = tmp | ships[player].alive <<30 | clients[player].active << 31; printf("tmp:%u\n",tmp); for (i=0; i<4; i++) { gameData[4+player*4+i] = tmp >> i*8; } } printf("__________________________\n"); for (player=0; player<MAX_CLIENTS; player++) { if (!clients[player].active) continue; if (ships[player].xPos < SCREEN_WIDTH/2) viewport.x=0; else if (ships[player].xPos > STAGE_WIDTH-SCREEN_WIDTH/2) viewport.x=STAGE_WIDTH-SCREEN_WIDTH; else viewport.x=ships[player].xPos-SCREEN_WIDTH/2; if (ships[player].yPos < SCREEN_HEIGHT/2) viewport.y=0; else if (ships[player].yPos > STAGE_HEIGHT-SCREEN_HEIGHT/2) viewport.y=STAGE_HEIGHT-SCREEN_HEIGHT; else viewport.y=ships[player].yPos-SCREEN_HEIGHT/2; for (secondary=0, counter=0; secondary<MAX_BULLETS; secondary++) { if (bullets[secondary].active && isInside((int)bullets[secondary].xPos, (int)bullets[secondary].yPos, &viewport)) { tmp=0; tmp = (int)(bullets[secondary].xPos-viewport.x) | (int)(bullets[secondary].yPos-viewport.y) << 11 | (int)(bullets[secondary].type) << 21; for (i=0; i<3; i++) { gameData[36+counter*3+i] = tmp >> i*8; } counter++; } } UDPpacketLength = 36+counter*3; gameData[UDPpacketLength++]=0xFF; packetOut->data = gameData; packetOut->len = UDPpacketLength; packetOut->address.host=clients[player].ip; packetOut->address.port=clients[player].recvPort; SDLNet_UDP_Send(udpSendSock,-1,packetOut); }
// send packet but don't expect acknoledgment of delivery bool network_send_no_ack( int len ) { packet_out_temp->len = len; if (!SDLNet_UDP_Send(socket, 0, packet_out_temp)) { printf("SDLNet_UDP_Send: %s\n", SDL_GetError()); return false; } return true; }
int UdpSocket::Send(const std::string& pMessage, const Address& pAddress) { mPacket->data = (Uint8*)pMessage.c_str(); mPacket->len = (int)pMessage.size(); mPacket->address.host = pAddress.GetHost(); mPacket->address.port = pAddress.GetPort(); int code = SDLNet_UDP_Send(mSocket, -1, mPacket); if(code == 0) SDL_Log("[UdpSocket::Send] Failed to send packets: %s", SDLNet_GetError()); return code; }
/* 1 byte to server */ int FCEUD_SendDataToServer(uint8 v,uint8 cmd) { UDPpacket upack; upack.channel=0; upack.data=MakeUDP(data,1); upack.len=upack.maxlen=UDPHEADSIZE+1; upack.status=0; SDLNet_UDP_Send(UDPSocket,0,&upack); outcounter++; return(1); }
void Server::ResendToAllClients(Uint32 senderHost) { for(int i = 0; i < numberOfClients; i++) { if(clientsArray[i].host_ != senderHost) { ret->address.host = clientsArray[i].host_; ret->address.port = clientsArray[i].port_; ret->channel = clientsArray[i].channel_; ret->data = input->data; ret->len = input->len; numsend = SDLNet_UDP_Send(sock, ret->channel, ret); } } }