void init_network() { state = STATE_INIT; sockfd = socket(AF_INET, SOCK_DGRAM, 0); if(sockfd < 0) { perror("network(): create socket"); exit(1); } int one = 1; if(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(int)) == -1) { perror("network(): setsockopt SO_BROADCAST"); exit(1); } if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int)) == -1) { perror("network(): setsockopt SO_REUSEADDR"); exit(1); } int x; x=fcntl(sockfd,F_GETFL,0); fcntl(sockfd,F_SETFL,x | O_NONBLOCK); broadcast_addr.sin_family = AF_INET; broadcast_addr.sin_port = htons(port); broadcast_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); if(bind(sockfd, (sockaddr *) &broadcast_addr, sizeof(sockaddr_in)) < 0) { perror("network(): bind"); exit(1); } request_slot(0); }
/** * Handles network traffic. */ void network() { char buffer[1024]; size_t size; struct sockaddr src_addr; socklen_t addrlen; FD_ZERO(&readset); FD_SET(sockfd,&readset); tv.tv_sec = 0; tv.tv_usec = 0; if(select(sockfd+1,&readset,NULL,NULL,&tv) > 0) { size = recvfrom(sockfd, buffer, 1024, 0, &src_addr, &addrlen); //printf(">> %s\n", buffer); if(strncmp(buffer,"omg ",4)==0) { char * data = buffer+4; switch(state) { case STATE_INIT: break; case STATE_REQUESTED: if(CMD("nak")) { int slot; sscanf(data, "nak %d", &slot); printf("Recived nak for slot %i\n", slot); ++slot; if(slot < 4) request_slot(slot); else { printf("No free slots, shutting down"); exit(2); } } break; case STATE_PLAYING: if(CMD("req")) { int slot; sscanf(data, "req %d", &slot); if(me != NULL && me->id == slot) { sprintf(buffer, "omg nak %d", slot); send_msg(buffer); } else { sprintf(buffer, "omg hai %d %f %f %f", me->id, me->pos.x, me->pos.y, me->angle); send_msg(buffer); } } else if(CMD("mov")) { int id; sscanf(data, "mov %d", &id); Player * p = get_or_create_plajur(id); if(p != NULL) { sscanf(data, "mov %d %f %f %f %d %f %f %f",&id, &p->pos.x, &p->pos.y, &p->angle, (int*)&p->current_base_texture, &p->velocity.x, &p->velocity.y, &p->da); } } else if(CMD("hai")) { int id; sscanf(data, "hai %d", &id); Player * p = get_or_create_plajur(id); if(p != NULL) { sscanf(data, "hai %d %f %f %f",&id, &p->pos.x, &p->pos.y, &p->angle); } } else if(CMD("rot")) { int id; sscanf(data, "rot %d", &id); Player * p = get_or_create_plajur(id); if(p != NULL) { sscanf(data, "rot %d %f %f",&id, &p->angle, &p->da); } } else if(CMD("fir")) { int id; sscanf(data, "fir %d", &id); Player * p = get_or_create_plajur(id); if(id != me->id && id < NUM_PLAYERS) { p->fire = true; } } else if(CMD("nof")) { int id; sscanf(data, "nof %d", &id); Player * p = players[id]; if(id != me->id && p!=NULL) { p->fire = false; } } else if(CMD("kil")) { int id; sscanf(data, "kil %d", &id); Player * p = players[id]; if(p!=NULL) { p->dead = 1; } } break; } } else { buffer[size] = 0; fprintf(stderr,"Recieved invalid data: %s\n", buffer); } } //Check if the slot request has time out (aka succeded) if(request_sent_time+0.5 < curtime() && state == STATE_REQUESTED) { ready = true; me = create_player(myname, requested_slot); state = STATE_PLAYING; } }
static int dma_tx_fragment(struct b43_dmaring *ring, struct sk_buff *skb) { const struct b43_dma_ops *ops = ring->ops; struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); struct b43_private_tx_info *priv_info = b43_get_priv_tx_info(info); u8 *header; int slot, old_top_slot, old_used_slots; int err; struct b43_dmadesc_generic *desc; struct b43_dmadesc_meta *meta; struct b43_dmadesc_meta *meta_hdr; u16 cookie; size_t hdrsize = b43_txhdr_size(ring->dev); /* Important note: If the number of used DMA slots per TX frame * is changed here, the TX_SLOTS_PER_FRAME definition at the top of * the file has to be updated, too! */ old_top_slot = ring->current_slot; old_used_slots = ring->used_slots; /* Get a slot for the header. */ slot = request_slot(ring); desc = ops->idx2desc(ring, slot, &meta_hdr); memset(meta_hdr, 0, sizeof(*meta_hdr)); header = &(ring->txhdr_cache[(slot / TX_SLOTS_PER_FRAME) * hdrsize]); cookie = generate_cookie(ring, slot); err = b43_generate_txhdr(ring->dev, header, skb, info, cookie); if (unlikely(err)) { ring->current_slot = old_top_slot; ring->used_slots = old_used_slots; return err; } meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header, hdrsize, 1); if (b43_dma_mapping_error(ring, meta_hdr->dmaaddr, hdrsize, 1)) { ring->current_slot = old_top_slot; ring->used_slots = old_used_slots; return -EIO; } ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr, hdrsize, 1, 0, 0); /* Get a slot for the payload. */ slot = request_slot(ring); desc = ops->idx2desc(ring, slot, &meta); memset(meta, 0, sizeof(*meta)); meta->skb = skb; meta->is_last_fragment = true; priv_info->bouncebuffer = NULL; meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1); /* create a bounce buffer in zone_dma on mapping failure. */ if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) { priv_info->bouncebuffer = kmemdup(skb->data, skb->len, GFP_ATOMIC | GFP_DMA); if (!priv_info->bouncebuffer) { ring->current_slot = old_top_slot; ring->used_slots = old_used_slots; err = -ENOMEM; goto out_unmap_hdr; } meta->dmaaddr = map_descbuffer(ring, priv_info->bouncebuffer, skb->len, 1); if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) { kfree(priv_info->bouncebuffer); priv_info->bouncebuffer = NULL; ring->current_slot = old_top_slot; ring->used_slots = old_used_slots; err = -EIO; goto out_unmap_hdr; } } ops->fill_descriptor(ring, desc, meta->dmaaddr, skb->len, 0, 1, 1); if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) { /* Tell the firmware about the cookie of the last * mcast frame, so it can clear the more-data bit in it. */ b43_shm_write16(ring->dev, B43_SHM_SHARED, B43_SHM_SH_MCASTCOOKIE, cookie); } /* Now transfer the whole frame. */ wmb(); ops->poke_tx(ring, next_slot(ring, slot)); return 0; out_unmap_hdr: unmap_descbuffer(ring, meta_hdr->dmaaddr, hdrsize, 1); return err; }
static int dma_tx_fragment(struct b43_dmaring *ring, struct sk_buff *skb) { const struct b43_dma_ops *ops = ring->ops; struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); struct b43_private_tx_info *priv_info = b43_get_priv_tx_info(info); u8 *header; int slot, old_top_slot, old_used_slots; int err; struct b43_dmadesc_generic *desc; struct b43_dmadesc_meta *meta; struct b43_dmadesc_meta *meta_hdr; u16 cookie; size_t hdrsize = b43_txhdr_size(ring->dev); old_top_slot = ring->current_slot; old_used_slots = ring->used_slots; slot = request_slot(ring); desc = ops->idx2desc(ring, slot, &meta_hdr); memset(meta_hdr, 0, sizeof(*meta_hdr)); header = &(ring->txhdr_cache[(slot / TX_SLOTS_PER_FRAME) * hdrsize]); cookie = generate_cookie(ring, slot); err = b43_generate_txhdr(ring->dev, header, skb, info, cookie); if (unlikely(err)) { ring->current_slot = old_top_slot; ring->used_slots = old_used_slots; return err; } meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header, hdrsize, 1); if (b43_dma_mapping_error(ring, meta_hdr->dmaaddr, hdrsize, 1)) { ring->current_slot = old_top_slot; ring->used_slots = old_used_slots; return -EIO; } ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr, hdrsize, 1, 0, 0); slot = request_slot(ring); desc = ops->idx2desc(ring, slot, &meta); memset(meta, 0, sizeof(*meta)); meta->skb = skb; meta->is_last_fragment = true; priv_info->bouncebuffer = NULL; meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1); if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) { priv_info->bouncebuffer = kmemdup(skb->data, skb->len, GFP_ATOMIC | GFP_DMA); if (!priv_info->bouncebuffer) { ring->current_slot = old_top_slot; ring->used_slots = old_used_slots; err = -ENOMEM; goto out_unmap_hdr; } meta->dmaaddr = map_descbuffer(ring, priv_info->bouncebuffer, skb->len, 1); if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) { kfree(priv_info->bouncebuffer); priv_info->bouncebuffer = NULL; ring->current_slot = old_top_slot; ring->used_slots = old_used_slots; err = -EIO; goto out_unmap_hdr; } } ops->fill_descriptor(ring, desc, meta->dmaaddr, skb->len, 0, 1, 1); if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) { b43_shm_write16(ring->dev, B43_SHM_SHARED, B43_SHM_SH_MCASTCOOKIE, cookie); } wmb(); ops->poke_tx(ring, next_slot(ring, slot)); return 0; out_unmap_hdr: unmap_descbuffer(ring, meta_hdr->dmaaddr, hdrsize, 1); return err; }