About Store Forum Documentation Contact



Post Reply 
[solved] sending file over p2p steam
Author Message
RedcrowProd Offline
Member

Post: #1
[solved] sending file over p2p steam
Hey,

EDIT : i was struggling to make p2p with files using byte packet, here is the SOLVED and working version of the code

i am using the Steamwork SDK for this, using 2 func, ( sendp2p/readp2p)

Code:
void SendP2PPacket(ulong TargetCSID, EP2PSend UDPTCPBUFF, File &f, int Channel=0)
{  
   f.pos(0);
   byte *data;
   void* buffer = Alloc(data, f.size());
   f.get(buffer, f.size());
   if(!SteamNetworking().SendP2PPacket( CSteamID(TargetCSID), buffer, f.size(), UDPTCPBUFF, Channel ))
   {
      Gui.msgBox(S, S+"Error Sending Packet via P2P");
   }
   Free( buffer );
}

So far nothing too hard, i pack my File with what i want to send, i pass it along to this function, pos is set back to (0) and send some byte buffer that get created with it. the rest of the args doesnt really matter, its just the target and the method used.

then on my loop(receiver) i am catching the packets using steam func like this

Code:
void UpdateP2PPacket()
{
   uint32 msgSize=0;
   int nChannel = 0;
   while(SteamNetworking().IsP2PPacketAvailable(&msgSize, nChannel))
   {
      byte *data;
      void* packet = Alloc(data, msgSize);
      CSteamID steamIDRemote;
      uint32 bytesRead = 0;
      if(SteamNetworking().ReadP2PPacket(data, msgSize, &bytesRead, &steamIDRemote))
      {
         File f_packet;
         f_packet.writeMem().putN(data, msgSize);
         f_packet.pos(0);
         byte t=0;
         int test=0;
         t=f_packet.getByte();
         f_packet.decIntV(test);
         Gui.msgBox(S, S+"WORKS"+t+"/"+test);  
      }
      Free( packet );
   }
}


there again, nothing crazy. we check if a packet is available, if it is then we get the size in byte.
then we malloc for the byte ( depending of msgsize ).

then we call the func to get this packet, and store it in our mem. then read write it as file, then read the file itself

thanks for the helps smile
(This post was last modified: 02-20-2019 05:47 AM by RedcrowProd.)
02-19-2019 02:55 AM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #2
RE: malloc and how to properly read it
What exactly does ReadP2PPacket do?

looks to me that the copy of data is also including the initial steam identified message bytes(unless you remove those identifiers in ReadP2PPacket and return a modified packet data)
(This post was last modified: 02-19-2019 03:35 AM by Zervox.)
02-19-2019 03:29 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #3
RE: malloc and how to properly read it
Steam doc only state this "Returns the packet data by copying it into this buffer."
02-19-2019 03:36 AM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #4
RE: malloc and how to properly read it
honestly I am dumbfounded, does the packet report the correct size(does the send file size match the received packet size?), is the data reversed?

also I am not entirely sure, but I believe byte *data then Alloc(data) is incorrect as I don't think that includes File's data variables(you are afterall sending the entire File structure.(basically how SteamNetwork sends void* from your File data is not handling it like how EE's send function does, so you need to compensate/correct that data conversion)

I guess you could try and send the data in File by sending f.get(buffer) SendP2PPacket(buffer) and see if that works to test.
(This post was last modified: 02-19-2019 04:05 AM by Zervox.)
02-19-2019 03:56 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #5
RE: malloc and how to properly read it
it seems that sending the file indeed cause some issue, sending just the buffer of byte[] seems to work. ima do some test
(This post was last modified: 02-19-2019 05:33 AM by RedcrowProd.)
02-19-2019 04:06 AM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #6
RE: malloc and how to properly read it
( CSteamID(TargetCSID), &f, f.size(), proto, Channel ))
"&f" is the problem
you should use f.get / f.put to get bytes from the file.
02-19-2019 08:57 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #7
RE: malloc and how to properly read it
Yes i was trying to figured out the result when the actual sending was wrong : /
Thanks again for clearing this up for me smile
Ill update the first post with correct way to do this. In case someone else is ever interested with steam p2p
(This post was last modified: 02-19-2019 06:17 PM by RedcrowProd.)
02-19-2019 06:14 PM
Find all posts by this user Quote this message in a reply
Post Reply