Archived
This forum has been archived. Please start a new discussion on GitHub.
About return struct
slice code:
client code:
module HX { struct HXMessage { int messageid; string userid; string password; }; struct HXCallMessage { int messageid; string Hintstr; }; interface HXSocket { ["ami"] HXCallMessage GetLogin(HXMessage Message); }; };
client code:
#include <Ice/Ice.h> #include <IceUtil/IceUtil.h> #include "IceMessages.h" #include "HX.h" #include "windows.h" using namespace std; using namespace HX; using namespace IceUtil; //////////////////////全局变量区////////////// COnLogin OnLogin=NULL; //////////////////////全局变量区////////////// class CIceCallback:virtual public HX::AMI_HXSocket_GetLogin { public: virtual void ice_response( const HXCallMessage& result ) { cout<<"ok"<<endl; if(OnLogin!=NULL) { CIceCallMessage IceCallMessage; memcpy(IceCallMessage.Hintstr,result.Hintstr.c_str(),strlen(result.Hintstr.c_str())); IceCallMessage.messageid=result.messageid; } return; }; virtual void ice_exception(const Ice::Exception&) { return; }; }; class IceClient { private: Ice::CommunicatorPtr ic; struct Ice::InitializationData initdata; HXSocketPrx SocketClient; AMI_HXSocket_GetLoginPtr IceCallback; struct HXMessage HXMsg; public: IceClient() { }; IceClient(char *config); int send(CIceMessage IceMessage); ~IceClient() { if(ic) { ic->shutdown(); ic->destroy(); } }; }; IceClient::IceClient(char *config) { Ice::PropertiesPtr props; props=Ice::createProperties(); props->load(config); initdata.properties=props; ic=Ice::initialize(initdata); Ice::ObjectPrx base=ic->stringToProxy("HX:default -p 10000"); SocketClient=HXSocketPrx::checkedCast(base); if(!SocketClient) throw "Invalid proxy"; IceCallback=new CIceCallback; }; int IceClient::send(CIceMessage IceMessage) { switch(IceMessage.messageid) { case HX_LOGIN:{ HXMsg.userid.append(IceMessage.userid,strlen(IceMessage.userid));//允许用户输入14位 HXMsg.password.append(IceMessage.password,strlen(IceMessage.password));//允许用户输入14位 SocketClient->GetLogin_async(IceCallback,HXMsg); break; } }; return 1; }; ////////////////////////////全局区域//////////////////////////////////// IceClient *Client; int _stdcall loadice(char *config) { Client=new IceClient(config); return 1; }; void _stdcall unloadice() { if(Client!=NULL) delete Client; }; int _stdcall sendmessage(CIceMessage IceMessage) { int result; if(Client!=NULL) result=Client->send(IceMessage); return result; }; int main() { loadice("Config.cfg"); CIceMessage IceMessage; IceMessage.callback=NULL; IceMessage.messageid=HX_LOGIN; IceMessage.userid="AAA"; IceMessage.password="fff"; sendmessage(IceMessage); delete Client; }; ////////////////////////////全局区域////////////////////////////////////
0
Comments
-
Serve Code:
// ********************************************************************** // // Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved. // // This copy of Ice is licensed to you under the terms described in the // ICE_LICENSE file included in this distribution. // // ********************************************************************** #include <Ice/Ice.h> #include "HX.h" #include "IceMessages.h" using namespace std; using namespace HX; class SocketServer : virtual public HXSocket { public: virtual ::HX::HXCallMessage GetLogin(const ::HX::HXMessage& HXMsg, const ::Ice::Current&) { struct HXCallMessage CallbackMessage; cout<<"=>>=>>=>>=>>=>>=>>=>>=>>=>>=>>"<<endl; cout<<"登陆用户:"<<HXMsg.userid<<endl; cout<<"登陆密码:"<<HXMsg.password<<endl; cout<<"=>>=>>=>>=>>=>>=>>=>>=>>=>>=>>"<<endl; CallbackMessage.messageid=HX_RETURN_LOGIN; CallbackMessage.Hintstr="到了十级使用祝福宝石,可以获得双倍经验"; cout<<CallbackMessage.messageid<<"::"<<CallbackMessage.Hintstr<<endl; return CallbackMessage; }; }; int main(int argc, char* argv[]) { int status = 0; Ice::CommunicatorPtr ic; try { ic = Ice::initialize(argc, argv); Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints( "HX", "default -p 10000"); Ice::ObjectPtr object = new SocketServer; adapter->add(object, ic->stringToIdentity("HX")); adapter->activate(); ic->waitForShutdown(); } catch (const Ice::Exception & e) { cerr << e << endl; status = 1; } catch (const char * msg) { cerr << msg << endl; status = 1; } if (ic) { try { ic->destroy(); } catch (const Ice::Exception & e) { cerr << e << endl; status = 1; } } return status; }
When I runs,ice_response can't be called.0 -
runs exception:
[ Network: trying to establish tcp connection to 192.168.0.34:10000 ]
[ Network: tcp connection established
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
[ Network: received 14 of 14 bytes via tcp
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
[ Protocol: received validate connection
message type = 3 (validate connection)
compression status = 0 (not compressed; do not compress response, if any)
message size = 14 ]
[ Protocol: sending request
message type = 0 (request)
compression status = 0 (not compressed; do not compress response, if any)
message size = 54
request id = 1
identity = HX
facet =
operation = ice_isA
mode = 1 (nonmutating)
context = ]
[ Network: received 14 of 14 bytes via tcp
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
[ Network: sent 54 of 54 bytes via tcp
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
[ Network: received 12 of 12 bytes via tcp
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
[ Protocol: received reply
message type = 2 (reply)
compression status = 0 (not compressed; do not compress response, if any)
message size = 26
request id = 1
reply status = 0 (ok) ]
[ Protocol: sending asynchronous request
message type = 0 (request)
compression status = 0 (not compressed; do not compress response, if any)
message size = 52
request id = 2
identity = HX
facet =
operation = GetLogin
mode = 0 (normal)
context = ]
[ Network: sent 52 of 52 bytes via tcp
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
[ Protocol: sending close connection
message type = 4 (close connection)
compression status = 1 (not compressed; compress response, if any)
message size = 14 ]
[ Network: sent 14 of 14 bytes via tcp
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
[ Network: received 14 of 14 bytes via tcp
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
[ Network: received 54 of 54 bytes via tcp
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
[ Protocol: received reply
message type = 2 (reply)
compression status = 0 (not compressed; do not compress response, if any)
message size = 68
request id = 2
reply status = 0 (ok) ]
[ Network: shutting down tcp connection for writing
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
warning: unknown exception raised by AMI callback
[ Network: closing tcp connection
local address = 192.168.0.34:4876
remote address = 192.168.0.34:10000 ]
Press any key to continue0 -
The warning indicates that a non-Ice exception is raised by your code in ice_response(). I don't know why, because I don't know the code for CIceCallMessage, but I suspect it is some kind of memory management error, which causes a Microsoft exception to be raised. Remove the memcpy() and check if you still get an exception.0
-
My struct define:
struct CIceMessage { int messageid; char *userid; char *password; void *callback; }; struct CIceCallMessage { int messageid; char *Hintstr; }; const HX_LOGIN=0; const HX_ICE_CALLBACK=1; const HX_RETURN_LOGIN=2;
Now I change the "ice_response" codevirtual void ice_response( const HXCallMessage& result ) { cout<<"ok"<<endl; };
the exception messages still exist.0 -
Posting small snippets of code like this isn't really helpful. There isn't enough information for us to diagnose your problem. All this snippet of code tells me is that there was some previous memory error in your application -- perhaps you allocated the AMI callback on the stack or something similar. I recommend tracing through the issue with a debugger.
If you want further assistance with this problem you should package up a complete, small, compilable example that demonstrates the issue and we'd be happy to look further.0 -
-
You are linking with "stlport_vc6.lib stlport_vc6_stldebug.lib". You should not do this. These libraries are linked automatically by the compiler. In addition, you must enable RTTI when compiling. In addition the call to waitForShutdown in the client is unecessary because there is no server side to shutdown.0