# HG changeset patch # User lenclud@WUE-9BW295J.ad.garmin.com # Date 1360264318 -3600 # Node ID ebd2fb40b03324c5e7293f4024f531ede0359fba # Parent f9c5cad93786b92ee5cdf833bcec0496171c2d7d Server can now receive messages from client. Client can now send messages to server. Tested IMON init call. diff -r f9c5cad93786 -r ebd2fb40b033 CSNamedPipe/CSNamedPipe/NamedPipeServer.cs --- a/CSNamedPipe/CSNamedPipe/NamedPipeServer.cs Thu Feb 07 15:56:06 2013 +0100 +++ b/CSNamedPipe/CSNamedPipe/NamedPipeServer.cs Thu Feb 07 20:11:58 2013 +0100 @@ -33,6 +33,10 @@ public const uint FILE_FLAG_OVERLAPPED = (0x40000000); public const uint PIPE_ACCESS_OUTBOUND = (0x00000002); public const uint PIPE_ACCESS_INBOUND = (0x00000001); + public const uint PIPE_TYPE_BYTE = (0x00000000); + public const uint PIPE_UNLIMITED_INSTANCES = 255; + + public const int BUFFER_SIZE = 256; @@ -48,13 +52,11 @@ SafeFileHandle iPipeInbound; public FileStream iStreamInbound; - public int ClientType; - public NamedPipeServer(string PName,int Mode) + public NamedPipeServer(string aPipeNameOutbound, string aPipeNameInbound) { - iPipeNameOutbound = PName; - ClientType = Mode;//0 Reading Pipe, 1 Writing Pipe - + iPipeNameOutbound = aPipeNameOutbound; + iPipeNameInbound = aPipeNameInbound; } /** @@ -66,8 +68,8 @@ this.iThreadOutbound = new Thread(new ThreadStart(ThreadOutbound)); this.iThreadOutbound.Start(); //Start inbound thread to receive messages - //this.iThreadInbound = new Thread(new ThreadStart(ThreadInbound)); - //this.iThreadInbound.Start(); + this.iThreadInbound = new Thread(new ThreadStart(ThreadInbound)); + this.iThreadInbound.Start(); } /** @@ -76,23 +78,28 @@ private void ThreadOutbound() { //TODO: Check that 255 - iPipeOutbound = CreateNamedPipe(this.iPipeNameOutbound, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, 0, 255, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero); + iPipeOutbound = CreateNamedPipe(this.iPipeNameOutbound, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero); - while (true) + //could not create named pipe + if (iPipeOutbound.IsInvalid) { - //could not create named pipe - if (iPipeOutbound.IsInvalid) - return; + //TODO: error handling + return; + } - //Will complete once a client connects - int success = ConnectNamedPipe(iPipeOutbound, IntPtr.Zero); + //Will complete once our client connects + int success = ConnectNamedPipe(iPipeOutbound, IntPtr.Zero); - //could not connect client - if (success == 0) - return; + //could not connect client + if (success == 0) + { + //TODO: error handling + return; + } - iStreamOutbound = new FileStream(iPipeOutbound, FileAccess.Write, BUFFER_SIZE, false); - } + //Client now connected create our stream + iStreamOutbound = new FileStream(iPipeOutbound, FileAccess.Write, BUFFER_SIZE, false); + } /** @@ -102,6 +109,23 @@ { //Client client = (Client)clientObj; //clientse.stream = new FileStream(clientse.handle, FileAccess.ReadWrite, BUFFER_SIZE, true); + + iPipeInbound = CreateNamedPipe(this.iPipeNameInbound, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero); + + //could not create named pipe + if (iPipeInbound.IsInvalid) + return; + + //Will complete once a client connects + int success = ConnectNamedPipe(iPipeInbound, IntPtr.Zero); + + //could not connect client + if (success == 0) + return; + + iStreamInbound = new FileStream(iPipeInbound, FileAccess.Read, BUFFER_SIZE, false); + + byte[] buffer = null; ASCIIEncoding encoder = new ASCIIEncoding(); @@ -131,7 +155,8 @@ int ReadLength = 0; for (int i = 0; i < BUFFER_SIZE; i++) { - if (buffer[i].ToString("x2") != "cc") + //if (buffer[i].ToString("x2") != "cc") + if (buffer[i] != 0) { ReadLength++; } diff -r f9c5cad93786 -r ebd2fb40b033 CSNamedPipe/CSNamedPipe/Program.cs --- a/CSNamedPipe/CSNamedPipe/Program.cs Thu Feb 07 15:56:06 2013 +0100 +++ b/CSNamedPipe/CSNamedPipe/Program.cs Thu Feb 07 20:11:58 2013 +0100 @@ -10,7 +10,7 @@ { //NamedPipeServer PServer1 = new NamedPipeServer(@"\\.\pipe\myNamedPipe1",0); //NamedPipeServer PServer2 = new NamedPipeServer(@"\\.\pipe\myNamedPipe2",1); - NamedPipeServer server = new NamedPipeServer(@"\\.\pipe\sga-receiver", 1); + NamedPipeServer server = new NamedPipeServer(@"\\.\pipe\sga-receiver", @"\\.\pipe\sga-sender"); //PServer1.Start(); diff -r f9c5cad93786 -r ebd2fb40b033 SoundGraphAccess.cpp --- a/SoundGraphAccess.cpp Thu Feb 07 15:56:06 2013 +0100 +++ b/SoundGraphAccess.cpp Thu Feb 07 20:11:58 2013 +0100 @@ -5,7 +5,9 @@ #include "SoundGraphAccess.h" #include "iMONDisplayAPI.h" -#define WM_DSP_PLUGIN_NOTIFY WM_APP + 1121 +#define WM_DSP_PLUGIN_NOTIFY WM_APP + 1100 +#define WM_IMON_INIT WM_APP + 1101 +#define WM_IMON_UNINIT WM_APP + 1102 #define MAX_LOADSTRING 100 @@ -25,6 +27,8 @@ char gBufferReceiver[256]; char gBufferSender[256]; +// +HWND gWnd; // BOOL m_bVfdConnected; @@ -37,6 +41,59 @@ INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); +const char KMsgDone[]="done"; +const char KMsgError[]="error"; + + + +/** +Send a message to our server. +*/ +void SendMessageToServer(const char* aResponse) + { + DWORD cbWritten=0; + WriteFile(gPipeSender, aResponse, strlen(aResponse), &cbWritten, NULL); + } + + +/** +Handle messages from our server. +*/ +void MessageReceived(const char* aMsg) + { + // + + if (strcmp(aMsg,"handshake")==0) + { + const char response[]="ready"; + DWORD cbWritten=0; + SendMessageToServer(response); + } + else if (strcmp(aMsg,"init")==0) + { + /* + DSPResult res=IMON_Display_Uninit(); + res=IMON_Display_Init(gWnd, WM_DSP_PLUGIN_NOTIFY); + if (res!=DSP_SUCCEEDED) + { + SendMessage(error); + } + */ + //IMON API call need to be done from window thread for some reason + SendMessage(gWnd,WM_IMON_INIT,0,0); + } + else if (strcmp(aMsg,"uninit")==0) + { + //IMON API call need to be done from window thread for some reason + SendMessage(gWnd,WM_IMON_UNINIT,0,0); + } + else + { + const char response[]="unknown"; + SendMessageToServer(response); + } + } + /** @@ -55,6 +112,7 @@ { gBufferReceiver[cbRead]='\0'; OutputDebugStringA(gBufferReceiver); + MessageReceived(gBufferReceiver); } if (!success && GetLastError() != ERROR_MORE_DATA) { @@ -79,14 +137,17 @@ */ DWORD WINAPI ThreadSender( LPVOID lpParam ) { + gPipeSender=CreateFile(gPipeNameSender, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); - + + /* if (gPipeSender!=0) { CloseHandle(gPipeSender); gPipeSender=0; } + */ return 0; } @@ -135,34 +196,36 @@ } - +/** +*/ void DisplayPluginMessage(UINT uErrCode, BOOL bError) { - LPCTSTR strErrMsg = _T(""); + char* strErrMsg = ""; if(bError) { switch(uErrCode) { - case DSPN_ERR_IN_USED: strErrMsg = _T("Display Plug-in is Already Used by Other Application."); break; - case DSPN_ERR_HW_DISCONNECTED: strErrMsg = _T("iMON HW is Not Connected."); break; - case DSPN_ERR_NOT_SUPPORTED_HW: strErrMsg = _T("The Connected iMON HW doesn't Support Display Plug-in."); break; - case DSPN_ERR_PLUGIN_DISABLED: strErrMsg = _T("Display Plug-in Mode Option is Disabled."); break; - case DSPN_ERR_IMON_NO_REPLY: strErrMsg = _T("The Latest iMON is Not Installed or iMON Not Running."); break; - case DSPN_ERR_UNKNOWN: strErrMsg = _T("Unknown Failure."); break; + case DSPN_ERR_IN_USED: strErrMsg = ("Display Plug-in is Already Used by Other Application."); break; + case DSPN_ERR_HW_DISCONNECTED: strErrMsg = ("iMON HW is Not Connected."); break; + case DSPN_ERR_NOT_SUPPORTED_HW: strErrMsg = ("The Connected iMON HW doesn't Support Display Plug-in."); break; + case DSPN_ERR_PLUGIN_DISABLED: strErrMsg = ("Display Plug-in Mode Option is Disabled."); break; + case DSPN_ERR_IMON_NO_REPLY: strErrMsg = ("The Latest iMON is Not Installed or iMON Not Running."); break; + case DSPN_ERR_UNKNOWN: strErrMsg = ("Unknown Failure."); break; } } else { switch(uErrCode) { - case DSPNM_PLUGIN_SUCCEED: strErrMsg = _T("Plug-in Mode Inited Successfully."); break; - case DSPNM_IMON_RESTARTED: strErrMsg = _T("iMON Started and Plug-in Mode Inited."); break; - case DSPNM_HW_CONNECTED: strErrMsg = _T("iMON HW Connected and Plug-in Mode Inited."); break; + case DSPNM_PLUGIN_SUCCEED: strErrMsg = ("Plug-in Mode Inited Successfully."); break; + case DSPNM_IMON_RESTARTED: strErrMsg = ("iMON Started and Plug-in Mode Inited."); break; + case DSPNM_HW_CONNECTED: strErrMsg = ("iMON HW Connected and Plug-in Mode Inited."); break; } } // - OutputDebugString((LPCTSTR)strErrMsg); + OutputDebugStringA(strErrMsg); + SendMessageToServer(strErrMsg); } @@ -213,20 +276,20 @@ // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { - HWND hWnd; + hInst = hInstance; // Store instance handle in our global variable - hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, + gWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); - if (!hWnd) + if (!gWnd) { return FALSE; } - ShowWindow(hWnd, nCmdShow); - UpdateWindow(hWnd); + ShowWindow(gWnd, nCmdShow); + UpdateWindow(gWnd); return TRUE; } @@ -297,6 +360,17 @@ //IMON_Display_Uninit(); PostQuitMessage(0); break; + + case WM_IMON_UNINIT: + IMON_Display_Uninit(); + SendMessageToServer(KMsgDone); + break; + + case WM_IMON_INIT: + IMON_Display_Uninit(); + IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY); + break; + case WM_DSP_PLUGIN_NOTIFY: switch(wParam) {