EldoS | Feel safer!

Software components for data protection, secure storage and transfer

HTTP Based Server failed to repond

Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.
#9911
Posted: 05/05/2009 05:26:14
by xion_more  (Basic support level)
Joined: 05/05/2009
Posts: 19

Hi,
Guys, i m posting my msgconnect server using transport as http.
i have successfully modified the SendNote example to behave as http server..
All you need to do for testing this server, is to build a client code for this.

Finally after lots of testing , i came to conclusion that there is packet loss in msgconnect library and its too slow ..
Can anyone explain me that..

---------------------------------

// w32.cpp : Defines the entry point for the application.
//

#include <winsock2.h>
#include <commctrl.h>
#include "stdafx.h"
#include "resource.h"


#include "MCBase.h"
#include "MCSock.h"
#include "MCSocket.h"
#include "MCHTTP.h"
//#include "XTabCtrl.h"
//#include "MCRouter.h"

using namespace MsgConnect;

#define USE_HTTP
//#define TEST_SOCKS

// Global Variables:
HINSTANCE hInst; // current instance
HWND hWnd;

MCMessage Message;
MCMessenger* mc = NULL;
MCQueue* mq = NULL;
MCMessageHandlers* mhs = NULL;
MCMessageHandler* mh = NULL;

#ifdef USE_HTTP
MCHttpTransport* transport = NULL;
#else
MCSocketTransport* transport = NULL;
//MCSocketTransport* routertransport = NULL;
#endif

//MCRouter* router = NULL;

// Forwar d declarations of functions included in this code module:
BOOL InitInstance(HINSTANCE, int);
//INT_PTR CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void __stdcall MCevent(void* resv, void* Sender, MCMessage& Message, bool& Handled)
{


char MsgSource[256]; int MsgSourceSize = 255;
mc->GetMessageSource(&Message, MsgSource, &MsgSourceSize);
char* s = (char*)malloc(Message.DataSize + 1 + 32);
if (0 != Message.DataSize)
{
char* msgText = (char*)malloc(Message.DataSize + 1);
memcpy(msgText, Message.Data, Message.DataSize);
msgText[Message.DataSize] = 0;
//strcpy(s, "Message from: ");
//strcat(s, msgText);
SendMessage(GetDlgItem( hWnd, IDC_LIST1 ), LB_ADDSTRING, 0, (LPARAM)msgText);
SendMessage(GetDlgItem( hWnd, IDC_LIST1 ), LB_ADDSTRING, 0, (LPARAM) MsgSource);
free(msgText);
/*
int l = (int)strlen(msgBin);
if (l+1 <= Message.DataSize)
{//message part is here
strcpy(s, "Message is: ");
strncat(s, msgBin+l+1, Message.DataSize-l-1);
SendMessage(GetDlgItem( hWnd, IDC_LIST1 ), LB_ADDSTRING, 0, (LPARAM)s);
}
*/
}
free(s);
Handled = true;
}

void __stdcall callback(unsigned long UserData, MCMessage& Message)
{
SendMessage( GetDlgItem( hWnd, IDC_LIST1 ), LB_ADDSTRING, 0, (LPARAM)"Message was sent" );
}

void __stdcall timeoutcallback(unsigned long UserData, MCMessage& Message)
{
SendMessage( GetDlgItem( hWnd, IDC_LIST1 ), LB_ADDSTRING, 0, (LPARAM)"Timeout expired" );
}

void __stdcall errorcallback(unsigned long UserData, MCMessage& Message, long errorcode)
{
SendMessage( GetDlgItem( hWnd, IDC_LIST1 ), LB_ADDSTRING, 0, (LPARAM)"Communication failed" );
}

void EnableControls(HWND hWnd)
{
BOOL transportActive = transport->getActive();
EnableWindow(GetDlgItem(hWnd, IDC_RCVPORTEDIT), !transportActive);
EnableWindow(GetDlgItem(hWnd, IDC_RCVPORTSPIN), !transportActive);

ShowWindow(GetDlgItem(hWnd, IDC_ACTIVE), transportActive?SW_SHOW:SW_HIDE);

EnableWindow(GetDlgItem(hWnd, IDC_TEXTEDIT), transportActive);
EnableWindow(GetDlgItem(hWnd, IDC_SNDADREDIT), transportActive);
EnableWindow(GetDlgItem(hWnd, IDC_SNDPORTEDIT), transportActive);
EnableWindow(GetDlgItem(hWnd, IDC_SNDPORTSPIN), transportActive);
EnableWindow(GetDlgItem(hWnd, IDC_SENDBUTTON), transportActive);
}

void mc_init(void)
{
MCBaseInitialization();
mc = new MCMessenger();
mq = new MCQueue();
mhs = mq->getHandlers();
mh = mhs->Add();
//#ifdef USE_HTTP
transport = new MCHttpTransport();
//#else
// transport = new MCSocketTransport();
//#endif
transport->setMessenger(mc);
transport->setAttemptsToConnect(1);
transport->setFailOnInactive(true);
transport->setMaxTimeout(900000l);
transport->setMessengerAddress("127.0.0.1");
//transport->setMessengerAddress("192.168.1.2");
transport->setMessengerPort(80); // 14583
transport->setTransportMode(stmServer); // stmP2P
#ifdef TEST_SOCKS
transport->setUseSocks(true);
transport->setSocksServer("192.168.114.1");
#endif

/* routertransport = new MCSocketTransport();
routertransport->setMessenger(mc);
routertransport->setAttemptsToConnect(1);
routertransport->setFailOnInactive(true);
routertransport->setMaxTimeout(900000l);
routertransport->setMessengerAddress("0.0.0.0");
routertransport->setMessengerPort(14584);
routertransport->setTransportMode(stmP2P);
routertransport->setName("sckt");
routertransport->setActive(true);
*/
mc->setMaxTimeout(ULONG_MAX);
try
{
transport->setActive(true);
}
catch(EMCError& e)
{
char msg[256];
sprintf(msg, "Cannot start transport. Error code is %d.", e.ErrorCode());
MessageBox(NULL, msg, "Error", MB_OK);
}
mh->setMsgCodeLow(1);
mh->setMsgCodeHigh(1);
mh->setOnMessage(MCevent);
mh->setEnabled(true);
mq->setQueueName("StressTest");

mq->setMessenger(mc);

/*
router = new MCRouter();
router->setMessenger(mc);
router->SetRoutingRule("socket:127.0.0.1:14583", "sckt:127.0.0.1:14584");
*/
}

void mc_free(void)
{
TRY_BLOCK
{
transport->setActive(false);
mq->setMessenger(NULL);
transport->setMessenger(NULL);
delete transport; transport = NULL;
delete mq; mq = NULL;
//delete mhs; mhs = NULL;
delete mc; mc = NULL;
}
CATCH_EVERY
{
}
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
mc_init();
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

//return (int) msg.wParam;

return 0;
}

void mc_send_message(void)
{
char data[1024] = "C++ demo -> ";
char dest_addr[1024];
char dest_port[1024];
int l = (int)strlen(data);
GetWindowText( GetDlgItem(hWnd, IDC_TEXTEDIT), &data[l], 1023-l-1);

MCMessenger::CreateMessageFromText(/*MsgCode*/ 1, /*Param1*/ 0, /*Param2*/ 0, data, false, Message);

/*
//int l2 = (int)strlen(&data[l+1]);
l = (int)strlen(data) + 1;
Message.Data = MCMemAlloc(l);
memcpy(Message.Data, &data, l);
Message.DataSize = l;

Message.MsgCode = 1;
Message.Param1 = 0;
Message.Param2 = 0;
Message.DataType = bdtConst;

*/
//create destination address
char dest[256];
#ifdef USE_HTTP
strcpy(dest, "HTTP:"); //transport name
#else
strcpy(dest, "SOCKET:");
#endif
GetDlgItemText(hWnd, IDC_SNDADREDIT, (LPTSTR)dest_addr, 1023);
GetDlgItemText(hWnd, IDC_SNDPORTEDIT, (LPTSTR)dest_port, 1023);

strcat(dest, dest_addr);
strcat(dest, ":");
strcat(dest, dest_port);
strcat(dest, "|SendNote");
mc->setCleanupInterval(1000);
mc->SendMessageTimeoutCallback(dest, &Message, 1000, callback, timeoutcallback, errorcallback, 0, NULL);
MCMemFree(Message.Data);
Message.Data = NULL;
}

void mc_startstop(HWND hWnd)
{
bool transportActive;
WORD port;
port = (WORD)SendDlgItemMessage(hWnd, IDC_RCVPORTSPIN, UDM_GETPOS, 0, 0);
transportActive = transport->getActive();
if (!transportActive)
transport->setMessengerPort(port);
transport->setActive(!transportActive);
EnableControls(hWnd);
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
BOOL CALLBACK MWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//MCMessage msg;
//bool msgArrived = false;

switch (message)
{
case WM_TIMER:
SetTimer(hWnd, 256, 100, NULL);
/*msgArrived = mc->PeekMessage(&msg);
if (msgArrived)
{
mc->GetMessage(&msg);
mc->MessageProcessed(&msg);
} */
mc->DispatchMessages();
return TRUE;

case WM_INITDIALOG:
SetTimer(hWnd, 256, 100, NULL);
//EnableControls(hWnd);
return TRUE;
case WM_COMMAND:
switch ( LOWORD ( (DWORD)wParam ) )
{
case IDC_SENDBUTTON:
//mc_send_message();
return TRUE;
case IDC_STARTSTOPBUTTON:
mc_startstop(hWnd);
return TRUE;
}
break;
case WM_DESTROY:
KillTimer(hWnd, 256);
PostQuitMessage(0);
mc_free();
return TRUE;
case WM_CLOSE:
DestroyWindow(hWnd);
return TRUE;
}
return FALSE;
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
InitCommonControls();
hInst = hInstance; // Store instance handle in our global variable

/** New variable decleration for tab control dialog box **/
// CImageList m_imagelistTab;

hWnd = CreateDialog( hInstance, MAKEINTRESOURCE( IDD_MAIN ), 0,MWindowProc );
// m_imagelisttab.

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
/* SetDlgItemText(hWnd, IDC_TEXTEDIT, TEXT("Test Message"));
SetDlgItemText(hWnd, IDC_SNDADREDIT, TEXT("127.0.0.1"));
SendDlgItemMessage(hWnd, IDC_SNDPORTSPIN, UDM_SETRANGE, 0,
MAKELONG(0x7FFF, 0));
SendDlgItemMessage(hWnd, IDC_SNDPORTSPIN, UDM_SETPOS, 0,
MAKELONG(14583, 0));

SendDlgItemMessage(hWnd, IDC_RCVPORTSPIN, UDM_SETRANGE, 0,
MAKELONG(0x7FFF, 0));
SendDlgItemMessage(hWnd, IDC_RCVPORTSPIN, UDM_SETPOS, 0,
MAKELONG(14583, 0)); */
return TRUE;
}


-----------
Just replace the w32.cpp file of SendNote demo code with my above code..
tell me if you get some problem while compiling the code.
Thanks
#9913
Posted: 05/05/2009 07:29:23
by Eugene Mayevski (EldoS Corp.)

Evaluation version is limited to 2 messages per second. If you send too many messages, they just expire while being in the send queue.


Sincerely yours
Eugene Mayevski
#9914
Posted: 05/05/2009 07:47:40
by xion_more  (Basic support level)
Joined: 05/05/2009
Posts: 19

Goodness ,
I need full version of Msgconnect to test ..
How can i get it?
After testing i will buy it ..

Reply

Statistics

Topic viewed 2697 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!