Board index » cppbuilder » Function Pointers in c++

Function Pointers in c++


2003-11-11 10:58:46 AM
cppbuilder55
I'd like to create a server that parses off the first 4 bytes in stream sent
by the client and use a std::map to with this 4 bytes being the key which
will return a function pointer that I can use with the remaining bytes in
the buffer.
My functions will look like...I already have my Packet class defined.
void someFunction1(const Packet& p);
void someFunction2(const Packet& p);
...
void someFunctionN(const Packet& p);
QUESTION 1: I'm not really sure how to define my FunctionPointerType which
is used below?
When I'm in the constructor of my server I'd do something like the following
typedef std::map<unsigned int, FunctionPointerType>myMapType
typedef std::map<unsigned int, FunctionPointerType>::value_type
myMapValueType
myMapType myMap;
myMap.insert(myMapValueType(1, someFunction1);
myMap.insert(myMapValueType(2, someFunction2);
...
myMap.insert(myMapValueType(N, someFunctionN);
Then upon reseiving the 4 bytes and the rest of my packet I'd like to call
the function. Some more pseduo code to demenstrate what I mean.
unsigned int key; //the 4 bytes received
Packet p; //the remainder of the buffer
myMap::iterator itr = myMap.find(key);
if(itr != myMap.end() )
{
*itr.second(p);
}
else
{
//client sending bogus data
}
QUESTION 2: I'm not sure how I would call the function. Is this correct,
*itr.second(p); ?
Is this a good way of handing the problem. In previous implementations of
servers I just used a series of if statements which gets kind of hard to
manage. I thought this would be a little easier to add new commands to the
server and this one is going to be quite a bit more extensive then anything
I've worked on in the past.
I think i could code this using delphi closuers pretty easily such as
typedef void __fastcall (__closure *FunctionPointerType)(const Packet& p);
but I've been trying to make the code I write more ansi c++ compatible...and
I'm fairly sure the __fastcall and __closure aren't ansi c++ compatible
although I could be mistaken.
I pretty much don't know whats standard and whats delphi c++. I've been
using c++ builder pretty much exclusivly as my c++ compiler my entire
student career. Before that Delphi so I get mixed up a bit.
 
 

Re:Function Pointers in c++

"Junk Mail" < XXXX@XXXXX.COM >wrote in message
Quote
QUESTION 1: I'm not really sure how to define my
FunctionPointerType which is used below?
typedef void (*FunctionPointerType)(const Packet&);
Quote
myMap.insert(myMapValueType(1, someFunction1);
myMap.insert(myMapValueType(2, someFunction2);
...
myMap.insert(myMapValueType(N, someFunctionN);
An alternative would be the following:
myMap[1] = &someFunction1;
myMap[2] = &someFunction2;
...
myMap[N] = &someFunctionN;
Quote
*itr.second(p);
That might work, but this would be cleaner:
itr->second(p);
Gambit
 

Re:Function Pointers in c++

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Junk Mail" < XXXX@XXXXX.COM >wrote in message
news:3fb05045$ XXXX@XXXXX.COM ...

>QUESTION 1: I'm not really sure how to define my
>FunctionPointerType which is used below?

typedef void (*FunctionPointerType)(const Packet&);

>myMap.insert(myMapValueType(1, someFunction1);
>myMap.insert(myMapValueType(2, someFunction2);
>...
>myMap.insert(myMapValueType(N, someFunctionN);

An alternative would be the following:

myMap[1] = &someFunction1;
myMap[2] = &someFunction2;
...
myMap[N] = &someFunctionN;

>*itr.second(p);

That might work, but this would be cleaner:

itr->second(p);
Thanks...that seems easy enough. Was expecting it to be more difficult than
that.
I know its all speculative and opinion based but any comments on this would
appreciated.
Quote
Is this a good way of handing the problem. In previous implementations of
servers I just used a series of if statements which gets kind of hard to
manage. I thought this would be a little easier to add new commands to the
server and this one is going to be quite a bit more extensive then anything
I've worked on in the past.
I've been messing around with client-server communications and have found
that its easiest break things up into messages. I have been using the first
4 bytes to indicate the total length of the message; the next 4 bytes to
indicate the command. This can be kind of viewed as the header of the
message. The remaining bytes of the message I am using as the parameters to
the command.
I'm just courious is this how most people do this? The reason I mentioned a
header is that I'm debating wether a client should send some type of extra
information in the header for encryption and decreption purposes. Where do
you suggest I add such inforamation. I haven't done much research on the
issue but I was thinking that a simple xor of the data would suffice.
Should the client send what the xor decryption key be or should the server
somehow know what to xor the data with.
Reason I was thinking of xor is that data could easily be converted from
encrypted to decrypted simply by xor ing all the data with a given key.
This seems simply enough to encomplish but would be difficult to break
unless the hacker knew what to xor the data with.
I've come up with a scheme that for each client that logs into the server I
have assigned them a GUID. I double check that no other client has the same
GUID (all though be it rare still possible...the server generates the GUID
for the client curently). I was thinking that the client could also
generate a GUID and the combination of these to GUID's could be used to
encrypt and decrypt the data.
Any comments, good or bad, is appreciated. I'm still in the semi design
phase for this project.
Thanks again gambit you're always a great help.
 

{smallsort}

Re:Function Pointers in c++

"Junk Mail" < XXXX@XXXXX.COM >wrote in message
Quote
The reason I mentioned a header is that I'm debating wether a
client should send some type of extra information in the header
for encryption and decreption purposes. Where do you suggest
I add such inforamation.
One possibility is to add an offset or size value into the header, to help
indicate the actual offset of the data from the beginning of the packet.
That way, you can extend the header with new features as you go along, and
existing clients will still be able to determine where the actual data
begins and ignore any features they don't understand by simply skipping over
the extra header fields that have been added. It wouldn't be a bad idea to
add a packet version field as well, so that if an existing client gets a
packet from a version is doesn't understand, it can ignore them as well.
Quote
I haven't done much research on the issue but I was thinking that
a simple xor of the data would suffice. Should the client send what
the xor decryption key be or should the server somehow know
what to xor the data with.
If you're serious about encryption, then you should learn how to use real
encryption algorithms. Blowfish is probably one the simplier algorithms to
implement, and there are plenty of components readily available for it. You
can then implement more secure algorithms as needed, such as MD5.
Quote
Reason I was thinking of xor is that data could easily be converted
from encrypted to decrypted simply by xor ing all the data with a
given key. This seems simply enough to encomplish but would be
difficult to break unless the hacker knew what to xor the data with.
Given enough packets, that wouldn't be very hard to figure out. I have a
project that implements an algorithm similar to what you describe. Except
that I use three xors - a random xor key generated for each new packet, a
user supplied password, and a master static key. Makes it a bit harder to
decypher packets, even when packets that are identical in data are
"encrypted" differently. Even the header itself is xor'ed.
Gambit
 

Re:Function Pointers in c++

"Junk Mail" < XXXX@XXXXX.COM >writes:
Quote
>>myMap.insert(myMapValueType(1, someFunction1);
>>myMap.insert(myMapValueType(2, someFunction2);
>>...
>>myMap.insert(myMapValueType(N, someFunctionN);
>
>An alternative would be the following:
>
>myMap[1] = &someFunction1;
>myMap[2] = &someFunction2;
>...
>myMap[N] = &someFunctionN;
Thanks...that seems easy enough. Was expecting it to be more difficult than
that.
Both ways will work, but if you know that the item is not in the map,
the the insert version will be slighly more efficient, because when
you use the bracket operator it returns a reference to an object.
Since that object doesn't yet exist, it must be default constructed
and for primitative types, that means they're zero-initialized. Then
the value is overwritten through the assignment. Thus, the
zero-initializing is wasted effort. When you insert like your first
example, the value is not "default constructed then overwritten", but
instead is simply copy-constructed.
For this case, it probably makes no difference, but it's a good idea
to keep those issues in mind for other cases, especially if default
construction of the objects is more expensive.
--
Chris (TeamB);
 

Re:Function Pointers in c++

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >writes:
Quote
>*itr.second(p);

That might work, but this would be cleaner:

itr->second(p);
Only your way works, because . precedes * .
An alternative to your way would be
(*itr).second(p);