Author Topic: Constructors for SystemAddress  (Read 5962 times)

Entelin

  • Jr. Member
  • **
  • Posts: 60
  • Karma: 0
    • View Profile
    • Logicaldreams.net
Constructors for SystemAddress
« on: November 03, 2008, 08:58:15 PM »
Here's the declaration of SystemAddress, I propose adding one or more additional constructors to help keep user code clean.

Code: [Select]
struct RAK_DLL_EXPORT SystemAddress
{
// these constructors would be helpful in cases where you are just creating one for the sake of passing it to a function
// like:   sendsomethingto (SystemAddress("127.0.0.1",10000))
SystemAddress(const char *str, unsigned short); // something like this

///The peer address from inet_addr.
unsigned int binaryAddress;
///The port number
unsigned short port;

// Return the systemAddress as a string in the format <IP>:<Port>
// Note - returns a static string.  Not thread-safe or safe for multiple calls per line.
char *ToString(bool writePort=true) const;

// Sets the binary address part from a string.  Doesn't set the port
void SetBinaryAddress(const char *str);

SystemAddress& operator = ( const SystemAddress& input )
{
binaryAddress = input.binaryAddress;
port = input.port;
return *this;
}

bool operator==( const SystemAddress& right ) const;
bool operator!=( const SystemAddress& right ) const;
bool operator > ( const SystemAddress& right ) const;
bool operator < ( const SystemAddress& right ) const;
};
« Last Edit: November 03, 2008, 09:02:22 PM by Entelin »

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Constructors for SystemAddress
« Reply #1 on: November 04, 2008, 01:45:15 AM »
I could although at this point there hasn't been any reason to do so. Under what context are you doing this?

Entelin

  • Jr. Member
  • **
  • Posts: 60
  • Karma: 0
    • View Profile
    • Logicaldreams.net
Re: Constructors for SystemAddress
« Reply #2 on: November 04, 2008, 10:55:19 AM »
generally its not an issue since you already have some systemaddress built or are getting it from the packet, etc. However in the case where you want to populate a list with some arbitrary data it would be easier to type something like:

somemap.insert(SystemAddress("127.0.0.1", 10000), "server1");

Such a case might be encountered when testing such as my multi_index post in the help forum.  Or in the case you want to connect to some explicit host. Any situation where you want to make a new system address it allows you to just type one line instead of 3.

OvermindDL1

  • Anti-Spam Moderator
  • Hero Member
  • ****
  • Posts: 855
  • Karma: 40
  • Programmer
    • View Profile
    • OvermindDL1's Site
Re: Constructors for SystemAddress
« Reply #3 on: December 08, 2008, 02:22:22 AM »
It looks like it would follow standard struct style, meaning that you could construct it like a normal C struct all inline, as long as you do not create any constructor for it at all though...

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Constructors for SystemAddress
« Reply #4 on: December 09, 2008, 12:42:44 AM »
Right now SystemAddress doesn't have a constructor, and I can't add one because of this:

Code: [Select]
const SystemAddress UNASSIGNED_SYSTEM_ADDRESS =
{
0xFFFFFFFF, 0xFFFF
};
[code]

Good idea for the next version though.
[/code]

oskar

  • Not-a-newbie
  • *
  • Posts: 26
  • Karma: 0
    • View Profile
Re: Constructors for SystemAddress
« Reply #5 on: March 05, 2009, 02:58:55 PM »
Sorry for reviving this, but I'd like a constructor for SystemAddress as well. I don't know how to convert a decimal ip address to binary so I have no way of creating one right now.

EDIT: Nevermind! I just forgot to add #include <arpa/inet.h> at the top of my file. I can now create a SystemAddress object by using inet_addr to create the binaryAddress.
« Last Edit: March 05, 2009, 03:22:45 PM by oskar »

jeckle

  • Not-a-newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: Constructors for SystemAddress
« Reply #6 on: April 12, 2009, 08:12:35 AM »
Hi

Just a quick note on the constructors, which I agree they could help :)

You can overcome the UNASSIGNED_SYSTEM_ADDRESS issue, by having one of the constructor to take binary address and port.
Code: [Select]
explicit SystemAddress(const char *, unsigned short) {};  // explicit will avoid misconvertion
explicit SystemAddress(unsigned int , unsigned short) {}; // explicit will avoid misconvertion

then UNASSIGNED_SYSTEM_ADDRESS declaration becomes :
Code: [Select]
const SystemAddress UNASSIGNED_SYSTEM_ADDRESS(0xFFFFFFFF, 0xFFFF);

Cedric

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: Constructors for SystemAddress
« Reply #7 on: April 13, 2009, 01:33:24 PM »
Thanks