Server can now receive messages from client.
Client can now send messages to server.
Tested IMON init call.
1.1 --- a/CSNamedPipe/CSNamedPipe/NamedPipeServer.cs Thu Feb 07 15:56:06 2013 +0100
1.2 +++ b/CSNamedPipe/CSNamedPipe/NamedPipeServer.cs Thu Feb 07 20:11:58 2013 +0100
1.3 @@ -33,6 +33,10 @@
1.4 public const uint FILE_FLAG_OVERLAPPED = (0x40000000);
1.5 public const uint PIPE_ACCESS_OUTBOUND = (0x00000002);
1.6 public const uint PIPE_ACCESS_INBOUND = (0x00000001);
1.7 + public const uint PIPE_TYPE_BYTE = (0x00000000);
1.8 + public const uint PIPE_UNLIMITED_INSTANCES = 255;
1.9 +
1.10 +
1.11
1.12
1.13 public const int BUFFER_SIZE = 256;
1.14 @@ -48,13 +52,11 @@
1.15 SafeFileHandle iPipeInbound;
1.16 public FileStream iStreamInbound;
1.17
1.18 - public int ClientType;
1.19
1.20 - public NamedPipeServer(string PName,int Mode)
1.21 + public NamedPipeServer(string aPipeNameOutbound, string aPipeNameInbound)
1.22 {
1.23 - iPipeNameOutbound = PName;
1.24 - ClientType = Mode;//0 Reading Pipe, 1 Writing Pipe
1.25 -
1.26 + iPipeNameOutbound = aPipeNameOutbound;
1.27 + iPipeNameInbound = aPipeNameInbound;
1.28 }
1.29
1.30 /**
1.31 @@ -66,8 +68,8 @@
1.32 this.iThreadOutbound = new Thread(new ThreadStart(ThreadOutbound));
1.33 this.iThreadOutbound.Start();
1.34 //Start inbound thread to receive messages
1.35 - //this.iThreadInbound = new Thread(new ThreadStart(ThreadInbound));
1.36 - //this.iThreadInbound.Start();
1.37 + this.iThreadInbound = new Thread(new ThreadStart(ThreadInbound));
1.38 + this.iThreadInbound.Start();
1.39 }
1.40
1.41 /**
1.42 @@ -76,23 +78,28 @@
1.43 private void ThreadOutbound()
1.44 {
1.45 //TODO: Check that 255
1.46 - iPipeOutbound = CreateNamedPipe(this.iPipeNameOutbound, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, 0, 255, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero);
1.47 + iPipeOutbound = CreateNamedPipe(this.iPipeNameOutbound, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero);
1.48
1.49 - while (true)
1.50 + //could not create named pipe
1.51 + if (iPipeOutbound.IsInvalid)
1.52 {
1.53 - //could not create named pipe
1.54 - if (iPipeOutbound.IsInvalid)
1.55 - return;
1.56 + //TODO: error handling
1.57 + return;
1.58 + }
1.59
1.60 - //Will complete once a client connects
1.61 - int success = ConnectNamedPipe(iPipeOutbound, IntPtr.Zero);
1.62 + //Will complete once our client connects
1.63 + int success = ConnectNamedPipe(iPipeOutbound, IntPtr.Zero);
1.64
1.65 - //could not connect client
1.66 - if (success == 0)
1.67 - return;
1.68 + //could not connect client
1.69 + if (success == 0)
1.70 + {
1.71 + //TODO: error handling
1.72 + return;
1.73 + }
1.74
1.75 - iStreamOutbound = new FileStream(iPipeOutbound, FileAccess.Write, BUFFER_SIZE, false);
1.76 - }
1.77 + //Client now connected create our stream
1.78 + iStreamOutbound = new FileStream(iPipeOutbound, FileAccess.Write, BUFFER_SIZE, false);
1.79 +
1.80 }
1.81
1.82 /**
1.83 @@ -102,6 +109,23 @@
1.84 {
1.85 //Client client = (Client)clientObj;
1.86 //clientse.stream = new FileStream(clientse.handle, FileAccess.ReadWrite, BUFFER_SIZE, true);
1.87 +
1.88 + iPipeInbound = CreateNamedPipe(this.iPipeNameInbound, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero);
1.89 +
1.90 + //could not create named pipe
1.91 + if (iPipeInbound.IsInvalid)
1.92 + return;
1.93 +
1.94 + //Will complete once a client connects
1.95 + int success = ConnectNamedPipe(iPipeInbound, IntPtr.Zero);
1.96 +
1.97 + //could not connect client
1.98 + if (success == 0)
1.99 + return;
1.100 +
1.101 + iStreamInbound = new FileStream(iPipeInbound, FileAccess.Read, BUFFER_SIZE, false);
1.102 +
1.103 +
1.104 byte[] buffer = null;
1.105 ASCIIEncoding encoder = new ASCIIEncoding();
1.106
1.107 @@ -131,7 +155,8 @@
1.108 int ReadLength = 0;
1.109 for (int i = 0; i < BUFFER_SIZE; i++)
1.110 {
1.111 - if (buffer[i].ToString("x2") != "cc")
1.112 + //if (buffer[i].ToString("x2") != "cc")
1.113 + if (buffer[i] != 0)
1.114 {
1.115 ReadLength++;
1.116 }
2.1 --- a/CSNamedPipe/CSNamedPipe/Program.cs Thu Feb 07 15:56:06 2013 +0100
2.2 +++ b/CSNamedPipe/CSNamedPipe/Program.cs Thu Feb 07 20:11:58 2013 +0100
2.3 @@ -10,7 +10,7 @@
2.4 {
2.5 //NamedPipeServer PServer1 = new NamedPipeServer(@"\\.\pipe\myNamedPipe1",0);
2.6 //NamedPipeServer PServer2 = new NamedPipeServer(@"\\.\pipe\myNamedPipe2",1);
2.7 - NamedPipeServer server = new NamedPipeServer(@"\\.\pipe\sga-receiver", 1);
2.8 + NamedPipeServer server = new NamedPipeServer(@"\\.\pipe\sga-receiver", @"\\.\pipe\sga-sender");
2.9
2.10
2.11 //PServer1.Start();
3.1 --- a/SoundGraphAccess.cpp Thu Feb 07 15:56:06 2013 +0100
3.2 +++ b/SoundGraphAccess.cpp Thu Feb 07 20:11:58 2013 +0100
3.3 @@ -5,7 +5,9 @@
3.4 #include "SoundGraphAccess.h"
3.5 #include "iMONDisplayAPI.h"
3.6
3.7 -#define WM_DSP_PLUGIN_NOTIFY WM_APP + 1121
3.8 +#define WM_DSP_PLUGIN_NOTIFY WM_APP + 1100
3.9 +#define WM_IMON_INIT WM_APP + 1101
3.10 +#define WM_IMON_UNINIT WM_APP + 1102
3.11 #define MAX_LOADSTRING 100
3.12
3.13
3.14 @@ -25,6 +27,8 @@
3.15 char gBufferReceiver[256];
3.16 char gBufferSender[256];
3.17
3.18 +//
3.19 +HWND gWnd;
3.20
3.21 //
3.22 BOOL m_bVfdConnected;
3.23 @@ -37,6 +41,59 @@
3.24 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
3.25
3.26
3.27 +const char KMsgDone[]="done";
3.28 +const char KMsgError[]="error";
3.29 +
3.30 +
3.31 +
3.32 +/**
3.33 +Send a message to our server.
3.34 +*/
3.35 +void SendMessageToServer(const char* aResponse)
3.36 + {
3.37 + DWORD cbWritten=0;
3.38 + WriteFile(gPipeSender, aResponse, strlen(aResponse), &cbWritten, NULL);
3.39 + }
3.40 +
3.41 +
3.42 +/**
3.43 +Handle messages from our server.
3.44 +*/
3.45 +void MessageReceived(const char* aMsg)
3.46 + {
3.47 + //
3.48 +
3.49 + if (strcmp(aMsg,"handshake")==0)
3.50 + {
3.51 + const char response[]="ready";
3.52 + DWORD cbWritten=0;
3.53 + SendMessageToServer(response);
3.54 + }
3.55 + else if (strcmp(aMsg,"init")==0)
3.56 + {
3.57 + /*
3.58 + DSPResult res=IMON_Display_Uninit();
3.59 + res=IMON_Display_Init(gWnd, WM_DSP_PLUGIN_NOTIFY);
3.60 + if (res!=DSP_SUCCEEDED)
3.61 + {
3.62 + SendMessage(error);
3.63 + }
3.64 + */
3.65 + //IMON API call need to be done from window thread for some reason
3.66 + SendMessage(gWnd,WM_IMON_INIT,0,0);
3.67 + }
3.68 + else if (strcmp(aMsg,"uninit")==0)
3.69 + {
3.70 + //IMON API call need to be done from window thread for some reason
3.71 + SendMessage(gWnd,WM_IMON_UNINIT,0,0);
3.72 + }
3.73 + else
3.74 + {
3.75 + const char response[]="unknown";
3.76 + SendMessageToServer(response);
3.77 + }
3.78 + }
3.79 +
3.80
3.81
3.82 /**
3.83 @@ -55,6 +112,7 @@
3.84 {
3.85 gBufferReceiver[cbRead]='\0';
3.86 OutputDebugStringA(gBufferReceiver);
3.87 + MessageReceived(gBufferReceiver);
3.88 }
3.89 if (!success && GetLastError() != ERROR_MORE_DATA)
3.90 {
3.91 @@ -79,14 +137,17 @@
3.92 */
3.93 DWORD WINAPI ThreadSender( LPVOID lpParam )
3.94 {
3.95 +
3.96 gPipeSender=CreateFile(gPipeNameSender, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
3.97
3.98 -
3.99 +
3.100 + /*
3.101 if (gPipeSender!=0)
3.102 {
3.103 CloseHandle(gPipeSender);
3.104 gPipeSender=0;
3.105 }
3.106 + */
3.107
3.108 return 0;
3.109 }
3.110 @@ -135,34 +196,36 @@
3.111
3.112 }
3.113
3.114 -
3.115 +/**
3.116 +*/
3.117 void DisplayPluginMessage(UINT uErrCode, BOOL bError)
3.118 {
3.119 - LPCTSTR strErrMsg = _T("");
3.120 + char* strErrMsg = "";
3.121
3.122 if(bError)
3.123 {
3.124 switch(uErrCode)
3.125 {
3.126 - case DSPN_ERR_IN_USED: strErrMsg = _T("Display Plug-in is Already Used by Other Application."); break;
3.127 - case DSPN_ERR_HW_DISCONNECTED: strErrMsg = _T("iMON HW is Not Connected."); break;
3.128 - case DSPN_ERR_NOT_SUPPORTED_HW: strErrMsg = _T("The Connected iMON HW doesn't Support Display Plug-in."); break;
3.129 - case DSPN_ERR_PLUGIN_DISABLED: strErrMsg = _T("Display Plug-in Mode Option is Disabled."); break;
3.130 - case DSPN_ERR_IMON_NO_REPLY: strErrMsg = _T("The Latest iMON is Not Installed or iMON Not Running."); break;
3.131 - case DSPN_ERR_UNKNOWN: strErrMsg = _T("Unknown Failure."); break;
3.132 + case DSPN_ERR_IN_USED: strErrMsg = ("Display Plug-in is Already Used by Other Application."); break;
3.133 + case DSPN_ERR_HW_DISCONNECTED: strErrMsg = ("iMON HW is Not Connected."); break;
3.134 + case DSPN_ERR_NOT_SUPPORTED_HW: strErrMsg = ("The Connected iMON HW doesn't Support Display Plug-in."); break;
3.135 + case DSPN_ERR_PLUGIN_DISABLED: strErrMsg = ("Display Plug-in Mode Option is Disabled."); break;
3.136 + case DSPN_ERR_IMON_NO_REPLY: strErrMsg = ("The Latest iMON is Not Installed or iMON Not Running."); break;
3.137 + case DSPN_ERR_UNKNOWN: strErrMsg = ("Unknown Failure."); break;
3.138 }
3.139 }
3.140 else
3.141 {
3.142 switch(uErrCode)
3.143 {
3.144 - case DSPNM_PLUGIN_SUCCEED: strErrMsg = _T("Plug-in Mode Inited Successfully."); break;
3.145 - case DSPNM_IMON_RESTARTED: strErrMsg = _T("iMON Started and Plug-in Mode Inited."); break;
3.146 - case DSPNM_HW_CONNECTED: strErrMsg = _T("iMON HW Connected and Plug-in Mode Inited."); break;
3.147 + case DSPNM_PLUGIN_SUCCEED: strErrMsg = ("Plug-in Mode Inited Successfully."); break;
3.148 + case DSPNM_IMON_RESTARTED: strErrMsg = ("iMON Started and Plug-in Mode Inited."); break;
3.149 + case DSPNM_HW_CONNECTED: strErrMsg = ("iMON HW Connected and Plug-in Mode Inited."); break;
3.150 }
3.151 }
3.152 //
3.153 - OutputDebugString((LPCTSTR)strErrMsg);
3.154 + OutputDebugStringA(strErrMsg);
3.155 + SendMessageToServer(strErrMsg);
3.156 }
3.157
3.158
3.159 @@ -213,20 +276,20 @@
3.160 //
3.161 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
3.162 {
3.163 - HWND hWnd;
3.164 +
3.165
3.166 hInst = hInstance; // Store instance handle in our global variable
3.167
3.168 - hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
3.169 + gWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
3.170 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
3.171
3.172 - if (!hWnd)
3.173 + if (!gWnd)
3.174 {
3.175 return FALSE;
3.176 }
3.177
3.178 - ShowWindow(hWnd, nCmdShow);
3.179 - UpdateWindow(hWnd);
3.180 + ShowWindow(gWnd, nCmdShow);
3.181 + UpdateWindow(gWnd);
3.182
3.183 return TRUE;
3.184 }
3.185 @@ -297,6 +360,17 @@
3.186 //IMON_Display_Uninit();
3.187 PostQuitMessage(0);
3.188 break;
3.189 +
3.190 + case WM_IMON_UNINIT:
3.191 + IMON_Display_Uninit();
3.192 + SendMessageToServer(KMsgDone);
3.193 + break;
3.194 +
3.195 + case WM_IMON_INIT:
3.196 + IMON_Display_Uninit();
3.197 + IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
3.198 + break;
3.199 +
3.200 case WM_DSP_PLUGIN_NOTIFY:
3.201 switch(wParam)
3.202 {