void IpAddress::simplifyIP() {
     sockaddr_storage saddr;
     this->getSockAddr((sockaddr*)&saddr, 0);
     IpAddress newAddr((sockaddr*)&saddr);
     ip = newAddr.ip;
 }
Example #2
0
void FSClient::SendFileMsg(std::string &message, s32 id)
{
  //Message from the server that we need to send a file to a client
  //message should look like "filename IP port"
  istring filename, ip, sPort;
  u32 port;
  
  std::istringstream stream(message);
  stream >> filename;
  stream >> ip;
  stream >> sPort;
  port = std::stoi(sPort);

  shareFiles.OpenFile(filename);

  //we should probably do some check here
  Address newAddr(inet_addr(ip.c_str()), port);
  s32 newCon = net->Connect(newAddr);

  istring command = "receivefile|";

  //-1 for the \n and sizeof(u32) for the message sequence number
  s32 bufSize = net->
    GetMaxDataSize() - command.size() - filename.size() - sizeof(u32) - 1;
  assertion(bufSize > 0);
  c08* buffer = new c08[bufSize];
  LOG("Sending file " << filename << " Chunck Size: " << bufSize << " to " << ip << " on port " << port);
  filename += '\n';
  //Send like a clear file message or something
  s32 totalSent = 0;
  s32 DBG_COUNT = 0;
  while((bufSize = shareFiles.ReadFile(buffer, bufSize)) > 0)
  {
    Encoder encode(false, net->GetMaxDataSize());
    encode.StartWriting();
    {
      encode.WriteData(command.c_str(), command.size());
      encode.Write(NextMessageSequence());
      encode.WriteData(filename.c_str(), filename.size());
      encode.WriteData(buffer, bufSize);
    }
    encode.DoneWriting();

    Networking::Message m((c08*)encode.GetData(), encode.GetDataSize());

    net->Send(m, newCon);
    totalSent += bufSize;
    ++DBG_COUNT;
    SLEEPMILLI(20);
  }

  //send the message signifying end
  Encoder encode(false, command.size() + sizeof(u32));
  encode.StartWriting();
  encode.WriteData(command.c_str(), command.size());
  encode.Write(0U);
  encode.DoneWriting();
  Networking::Message m((c08*)encode.GetData(), encode.GetDataSize());
  net->Send(m, newCon);
  //reset the message sequence for next file transfer
  nextMessageSeq = 1;

  LOG("SendCount: " << DBG_COUNT);
  LOG(" Done sending file " << filename);
}