PutFloatArray problem

Post here your questions about the C++ API for SFS2X

Moderators: Lapo, Bax, MBagnati

ganquan
Posts: 38
Joined: 17 Apr 2013, 01:42

PutFloatArray problem

Postby ganquan » 19 Apr 2013, 09:31

with vs2010 and c++ api 0.9.3

i try to send some float to serve like:

Code: Select all

std::string* stdRequest = new string("updatePos");
   ISFSObject * param = new SFSObject( );
   std::string* stdKey = new string("pos");
   vector<float*>* playerpos = new vector<float*>();
   float pos[3];
   pos[0] = 1.0;pos[1] = 2.0;pos[2] = 3.0;
   playerpos->push_back(pos);
   playerpos->push_back(pos+1);
   playerpos->push_back(pos+2);
   param->PutFloatArray(stdKey,playerpos);

   long int testint[3];
   testint[0] = 1;testint[1] = 2;testint[2] = 3;
   vector<long int*>* playerIds = new vector<long int*>();
   playerIds->push_back((long int *)testint[0]);
   playerIds->push_back((long int *)testint[1]);
   playerIds->push_back((long int *)testint[2]);
   std::string* stdKeyName = new string("testint");
   param->PutIntArray(stdKeyName,playerIds);
   
   mSmartFox->Send( new ExtensionRequest(stdRequest,param,mMatchRoom) );


and i reieve it at serve:

Code: Select all

public void handleClientRequest(User user, ISFSObject params)
   {
      // Get position
      Collection<Float> position = params.getFloatArray("pos");
      Collection<Integer> testint= params.getIntArray("testint");
      
      trace("position: " + position + "   id: " + testint);
   }


i got "poisition: [0.0,0.0,0.0] id: [1,2,3]"

is something wrong in my code?

i try playerpos->push_back((float *)pos[0]) like testint, but error when compile.
ganquan
Posts: 38
Joined: 17 Apr 2013, 01:42

Re: PutFloatArray problem

Postby ganquan » 19 Apr 2013, 10:13

and if i send float from server to client

Code: Select all

ISFSObject sendObj = SFSObject.newInstance();
       List<Float> sendPos = new ArrayList<Float>();
       sendPos.add((float) 1.0);sendPos.add((float) 2.0);sendPos.add((float) 3.0);
       sendObj.putFloatArray("pos", sendPos);
       sendObj.putIntArray("posint", Arrays.asList(100000,200000,300000));
       Room currentRoom = getParentExtension().getParentRoom();
       List<User> userList = currentRoom.getUserList();
       this.send("testFloat", sendObj, userList);


the client is work fine with IntArray , but GetFloatArray would get some big float

Code: Select all

string * strKey = new string("pos");
      vector<float> * playerpos = dataObject->GetFloatArray(strKey);
      string * strKeyInt = new string("posint");
      vector<long>  * playerposint = dataObject->GetIntArray(strKeyInt);
MBagnati
Posts: 126
Joined: 12 Feb 2013, 10:57

Re: PutFloatArray problem

Postby MBagnati » 21 Apr 2013, 09:42

I think that there are two aspects to point out:

1) There is an error into C++ API about the serialization/deserialization of float/double arrays
Error will be fixed in next release 0.9.4

2) In your software

long int testint[3];
testint[0] = 1;testint[1] = 2;testint[2] = 3;
vector<long int*>* playerIds = new vector<long int*>();
playerIds->push_back((long int *)testint[0]);
playerIds->push_back((long int *)testint[1]);
playerIds->push_back((long int *)testint[2]);

the parameter provided to playerIds->push_back is not a pointer to a long int.
You have written

(long int *)testint[0]

but in this way the software uses the number 1 (the value previously assigned to testint[0]) as a pointer.
Please try this statements where has been added the & to reference the memory address of testint[0], testint[1], testint[2]

playerIds->push_back((long int *)&testint[0]);
playerIds->push_back((long int *)&testint[1]);
playerIds->push_back((long int *)&testint[2]);

Finally, compiler returns an error on the statement

playerpos->push_back((float *)pos[0])

because the parameter provided to push_back is a float, not a float pointer
Try adding the & char to reference the memory address instead of the float value

playerpos->push_back((float *)&pos[0])
ganquan
Posts: 38
Joined: 17 Apr 2013, 01:42

Re: PutFloatArray problem

Postby ganquan » 22 Apr 2013, 01:43

i try

Code: Select all

ISFSObject * param = new SFSObject( );
   std::string* stdKey = new string("pos");
   vector<float*>* playerpos = new vector<float*>();
   float pos[3];
   pos[0] = 1.0;pos[1] = 2.0;pos[2] = 3.0;
   playerpos->push_back((float *)&pos[0]);
   playerpos->push_back((float *)&pos[1]);
   playerpos->push_back((float *)&pos[2]);
   param->PutFloatArray(stdKey,playerpos);

   long int testint[3];
   testint[0] = 1;testint[1] = 2;testint[2] = 3;
   vector<long int*>* playerIds = new vector<long int*>();
   playerIds->push_back((long int *)&testint[0]);
   playerIds->push_back((long int *)&testint[1]);
   playerIds->push_back((long int *)&testint[2]);
   std::string* stdKeyName = new string("testint");
   param->PutIntArray(stdKeyName,playerIds);


and at server :

Code: Select all

// Get position
      Collection<Float> position = params.getFloatArray("pos");
      Collection<Integer> charactor = params.getIntArray("testint");
      
      trace("position: " + position + "   testint: " + charactor);


and trace print "poisition: [0.0,0.0,0.0] testint: [1765724,1765728,1765732]

it look like testint send the value of address to server, and float still 0
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: PutFloatArray problem

Postby Bax » 22 Apr 2013, 08:21

This was fixed in API v0.9.4 we just published: viewtopic.php?f=34&t=15963
Thank you for reporting.
Paolo Bax
The SmartFoxServer Team

Return to “SFS2X C++ API”

Who is online

Users browsing this forum: No registered users and 15 guests