API In C++ Wikied

I submitted some code I converted and repaired for C++ in the wiki. It can be found at http://wiki.mikrotik.com/wiki/API_In_CPP. Just thought I would share it.

Update 02-10-10:
The Wiki page for the C++ version has been updated. The code is now primarily written in C++ rather than just a wrapper for C.

Update 02-13-10:
I updated the code to reflect the changes ayufan suggested below.

Someone should write using “real” C++. Any use of STL (vector, map and string) and even BOOST (async io) would be appreciated and less error-prone :wink:

I agree and I plan on doing it eventually. Actually my roommate may be working on for fun haha. We will see. For now this code should do alright until I can remove all the dynamic memory fun and replace it with actual C++.

was not aware that string was part of standard c++

and thanks for application of code.
edit: linked from main API page in wiki to your sources.

Yup it is. C++ is just the OO version of C as you probably know. So the string lib and stl are standard libraries shipped with any copy of c++ on any platform.

In other news I will be starting on a full conversion to C++ today. Shouldn’t take me more than today to get it working I would imagine.

The Wiki page for the C++ version has been updated. The code is now primarily written in C++ rather than just a wrapper for C.

Better, but have my ideas:

  • InitializeSentence, ClearSentence should be removed this is done by ctor and dtor of Sentence class
  • AddWordToSentence should be method of Sentence class
  • the same applies to InitializeBlock, ClearBlock
  • Why there is wordCount and sentenceCount? You have vector length!
  • You should use const string& in function arguments instead of pure string. There’s no need to perform object copy
  • Headers should do: using namespace std; it should be moved to CPP and in header You should use std::string, std::vector
  • It would be nice if Sentence would have method to split words in to map. Let assume that You have following words in sentence block:

=key1=value
=key2=value2

It would be simpler to access these values using std::map

  • ErrorCode should be removed and std::exception should introduced instead
  • The following code can be written using simpler and more clean form:
		AddWordToSentence(writeSentence, "/login");
		AddWordToSentence(writeSentence, "=name=");
		AddPartWordToSentence(writeSentence, strUsername);
		AddWordToSentence(writeSentence, "=response=00");
		AddPartWordToSentence(writeSentence, md5PasswordToSend);



writeSentence.strWords.push_back("/login");
writeSentence.strWords.push_back("=name="+strUsername);
writeSentence.strWords.push_back("=response=00"+md5PasswordToSend);
  • consider using following form: void ReadBlock(Block& block) and void ReadSentence(Sentence& sentence) because on return compiler performs deep object copy

That would be all for now :slight_smile: This is just my ideas ;

Ok all the changes you suggest have been implemented. I liked where your head was at so I did it. :slight_smile: Thanks for the input!

It’s almost as good as it should be :slight_smile:

#define NONE 0
#define DONE 1
#define TRAP 2
#define FATAL 3
  • change to enumeration
  • functions like Print, GetMap, Length - all that don’t make object context change should be marked as const
	std::string operator[](int index) { return strWords[index]; }
	std::string GetWord(int index) { return strWords[index]; }
  • it’s better to return const std::string&, there’is no object copy :wink:
  • in GetWord use: return strWords.at(index); in case when invalid index is used function throws exception - it’s preferred behavior
MikrotikAPI mt = MikrotikAPI("64.126.135.214", "test", "joey", 8728);
  • this is extremely bad, you should disallow use of copy constructor and assignment operator - it can later lead to unexpected behavior
  • you should also detect when connection is closed by other endpoint: read from socket if I remember correctly returns 0

:wink:

when sending command to router, how many packets it will create for, say /login command?

will it be 3 packets first one with length of command, then command itself and then terminating zero or i am mistaken?

at least in java it did 3 packets with writing to socket as you have it now.

it’s undefined because socket for tcp connections have receive and send buffer where data is accumulated, so if you invoke 3 times send doesn’t really mean that there will be 3 packets

for my Java implementation i used sniffer to check… it always was 3 packets. first in that connection was len, the API sentence then ending 0. Maybe in C++ it is different and is not controllable by user.

AFAIK each socket (connection) has its buffer not tcp/ip stack.