1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Text;
|
---|
4 | using System.IO;
|
---|
5 | using System.Net.Sockets;
|
---|
6 | using System.Net;
|
---|
7 |
|
---|
8 |
|
---|
9 | namespace IndianHealthService.BMXNet
|
---|
10 | {
|
---|
11 | /// <summary>
|
---|
12 | /// BMXNetLib implements low-level socket connectivity to RPMS databases.
|
---|
13 | /// The VA RPC Broker must be running on the RPMS server in order for
|
---|
14 | /// BMXNetLib to connect.
|
---|
15 | /// </summary>
|
---|
16 | public class BMXNetLib
|
---|
17 | {
|
---|
18 | public BMXNetLib()
|
---|
19 | {
|
---|
20 | m_sWKID = "XWB";
|
---|
21 | m_sWINH = "";
|
---|
22 | m_sPRCH = "";
|
---|
23 | m_sWISH = "";
|
---|
24 | m_cHDR = ADEBHDR(m_sWKID,m_sWINH,m_sPRCH,m_sWISH);
|
---|
25 |
|
---|
26 | }
|
---|
27 |
|
---|
28 | #region Piece Functions
|
---|
29 |
|
---|
30 | /// <summary>
|
---|
31 | /// Corresponds to M's $L(STRING,DELIMITER)
|
---|
32 | /// </summary>
|
---|
33 | /// <param name="sInput"></param>
|
---|
34 | /// <param name="sDelim"></param>
|
---|
35 | /// <returns></returns>
|
---|
36 | public static int PieceLength(string sInput, string sDelim)
|
---|
37 | {
|
---|
38 | char[] cDelim = sDelim.ToCharArray();
|
---|
39 | string [] cSplit = sInput.Split(cDelim);
|
---|
40 | return cSplit.GetLength(0);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /// <summary>
|
---|
44 | /// Corresponds to M's $$Piece function
|
---|
45 | /// </summary>
|
---|
46 | /// <param name="sInput"></param>
|
---|
47 | /// <param name="sDelim"></param>
|
---|
48 | /// <param name="nNumber"></param>
|
---|
49 | /// <returns></returns>
|
---|
50 | public static string Piece(string sInput, string sDelim, int nNumber)
|
---|
51 | {
|
---|
52 | char[] cDelim = sDelim.ToCharArray();
|
---|
53 | string [] cSplit = sInput.Split(cDelim);
|
---|
54 | int nLength = cSplit.GetLength(0);
|
---|
55 | if ((nLength < nNumber)||(nNumber < 1))
|
---|
56 | return "";
|
---|
57 | return cSplit[nNumber-1];
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | // public static string Piece(string[] sInput, string sDelim, int nNumber)
|
---|
62 | // {
|
---|
63 | // char[] cDelim = sDelim.ToCharArray();
|
---|
64 | // int nLength = sInput.GetLength(0);
|
---|
65 | // if ((nLength < nNumber)||(nNumber < 1))
|
---|
66 | // return "";
|
---|
67 | //
|
---|
68 | // return sInput[nNumber-1];
|
---|
69 | //
|
---|
70 | // }
|
---|
71 |
|
---|
72 | public static string Piece(string sInput, string sDelim, int nNumber, int nEnd)
|
---|
73 | {
|
---|
74 | try
|
---|
75 | {
|
---|
76 | if (nNumber < 0)
|
---|
77 | nNumber = 1;
|
---|
78 |
|
---|
79 | if (nEnd < nNumber)
|
---|
80 | return "";
|
---|
81 |
|
---|
82 | if (nEnd == nNumber)
|
---|
83 | return Piece(sInput, sDelim, nNumber);
|
---|
84 |
|
---|
85 | char[] cDelim = sDelim.ToCharArray();
|
---|
86 | string [] cSplit = sInput.Split(cDelim);
|
---|
87 | int nLength = cSplit.GetLength(0);
|
---|
88 | if ((nLength < nNumber)||(nNumber < 1))
|
---|
89 | return "";
|
---|
90 |
|
---|
91 | //nNumber = 1-based index of the starting element to return
|
---|
92 | //nLength = count of elements
|
---|
93 | //nEnd = 1-based index of last element to return
|
---|
94 | //nCount = number of elements past nNumber to return
|
---|
95 |
|
---|
96 | //convert nNumber to 0-based index:
|
---|
97 | nNumber--;
|
---|
98 |
|
---|
99 | //convert nEnd to 0-based index;
|
---|
100 | nEnd--;
|
---|
101 |
|
---|
102 | //Calculate nCount;
|
---|
103 | int nCount = nEnd - nNumber + 1;
|
---|
104 |
|
---|
105 | //Adjust nCount for number of elements
|
---|
106 | if (nCount + nNumber >= nLength)
|
---|
107 | {
|
---|
108 | nCount = nLength - nNumber;
|
---|
109 | }
|
---|
110 |
|
---|
111 | string sRet = string.Join(sDelim, cSplit, nNumber, nCount );
|
---|
112 | return sRet;
|
---|
113 | }
|
---|
114 | catch (Exception bmxEx)
|
---|
115 | {
|
---|
116 | string sMessage = bmxEx.Message + bmxEx.StackTrace;
|
---|
117 | throw new BMXNetException(sMessage);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | // public static string Piece(string[] sInput, string sDelim, int nNumber, int nEnd)
|
---|
122 | // {
|
---|
123 | // if (nEnd < nNumber)
|
---|
124 | // return "";
|
---|
125 | //
|
---|
126 | //
|
---|
127 | // if (nEnd == nNumber)
|
---|
128 | // return Piece(sInput, sDelim, nNumber);
|
---|
129 | //
|
---|
130 | // char[] cDelim = sDelim.ToCharArray();
|
---|
131 | // int nLength = sInput.GetLength(0);
|
---|
132 | //
|
---|
133 | // if ((nLength < nNumber)||(nNumber < 1))
|
---|
134 | // return "";
|
---|
135 | //
|
---|
136 | // if (nEnd > nLength)
|
---|
137 | // nEnd = nLength;
|
---|
138 | //
|
---|
139 | // return string.Join(sDelim, sInput, nNumber - 1, nEnd - 1);
|
---|
140 | // }
|
---|
141 |
|
---|
142 | #endregion Piece Functions
|
---|
143 |
|
---|
144 | #region RPX Fields
|
---|
145 |
|
---|
146 | private string m_sWKID;
|
---|
147 | private string m_sWISH;
|
---|
148 | private string m_sPRCH;
|
---|
149 | private string m_sWINH;
|
---|
150 | private string m_cHDR;
|
---|
151 | private string m_cVerify;
|
---|
152 | private string m_cAccess;
|
---|
153 | private string m_cDUZ;
|
---|
154 | private string m_cAuthentication;
|
---|
155 | private string m_cAppContext;
|
---|
156 | private bool m_bConnected;
|
---|
157 | private int m_nMServerPort;
|
---|
158 | private string m_cServerAddress;
|
---|
159 | private string m_cDUZ2;
|
---|
160 | private string m_cLoginFacility;
|
---|
161 |
|
---|
162 | #endregion RPX Fields
|
---|
163 |
|
---|
164 | #region Encryption Keys
|
---|
165 | private string[] m_sKey = new string[]
|
---|
166 | {
|
---|
167 | @"wkEo-ZJt!dG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1p9=w}FAI&j/keD0c?)LN6OHV]lGy'$*>nd[(tb!#",
|
---|
187 | };
|
---|
188 | #endregion Encryption Keys
|
---|
189 |
|
---|
190 |
|
---|
191 | #region RPX Functions
|
---|
192 |
|
---|
193 | /// <summary>
|
---|
194 | /// Given strInput = "13" builds "013" if nLength = 3. Default for nLength is 3.
|
---|
195 | /// </summary>
|
---|
196 | /// <param name="strInput"></param>
|
---|
197 | /// <returns></returns>
|
---|
198 | private string ADEBLDPadString(string strInput)
|
---|
199 | {
|
---|
200 | return ADEBLDPadString(strInput, 3);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /// <summary>
|
---|
204 | /// Given strInput = "13" builds "013" if nLength = 3 Default for nLength is 3.
|
---|
205 | /// </summary>
|
---|
206 | /// <param name="strInput"></param>
|
---|
207 | /// <param name="nLength">Default = 3</param>
|
---|
208 | /// <returns></returns>
|
---|
209 | private string ADEBLDPadString(string strInput, int nLength /*=3*/)
|
---|
210 | {
|
---|
211 | return strInput.PadLeft(nLength, '0');
|
---|
212 | }
|
---|
213 |
|
---|
214 | /// <summary>
|
---|
215 | /// Concatenates zero-padded length of sInput to sInput
|
---|
216 | /// Given "Hello" returns "004Hello"
|
---|
217 | /// If nSize = 5, returns "00004Hello"
|
---|
218 | /// Default for nSize is 3.
|
---|
219 | /// </summary>
|
---|
220 | /// <param name="sInput"></param>
|
---|
221 | /// <param name="nSize"></param>
|
---|
222 | /// <returns></returns>
|
---|
223 | private string ADEBLDB(string sInput)
|
---|
224 | {
|
---|
225 | return ADEBLDB(sInput, 3);
|
---|
226 | }
|
---|
227 |
|
---|
228 | /// <summary>
|
---|
229 | /// Concatenates zero-padded length of sInput to sInput
|
---|
230 | /// Given "Hello" returns "004Hello"
|
---|
231 | /// If nSize = 5, returns "00004Hello"
|
---|
232 | /// Default for nSize is 3.
|
---|
233 | /// </summary>
|
---|
234 | /// <param name="sInput"></param>
|
---|
235 | /// <param name="nSize"></param>
|
---|
236 | /// <returns></returns>
|
---|
237 | private string ADEBLDB(string sInput, int nSize /*=3*/)
|
---|
238 | {
|
---|
239 | int nLen = sInput.Length;
|
---|
240 | string sLen = this.ADEBLDPadString(nLen.ToString(), nSize);
|
---|
241 | return sLen + sInput;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /// <summary>
|
---|
245 | /// Build protocol header
|
---|
246 | /// </summary>
|
---|
247 | /// <param name="sWKID"></param>
|
---|
248 | /// <param name="sWINH"></param>
|
---|
249 | /// <param name="sPRCH"></param>
|
---|
250 | /// <param name="sWISH"></param>
|
---|
251 | /// <returns></returns>
|
---|
252 | private string ADEBHDR(string sWKID, string sWINH, string sPRCH, string sWISH)
|
---|
253 | {
|
---|
254 | string strResult;
|
---|
255 | strResult = sWKID+";"+sWINH+";"+sPRCH+";"+sWISH+";";
|
---|
256 | strResult = ADEBLDB(strResult);
|
---|
257 | return strResult;
|
---|
258 | }
|
---|
259 |
|
---|
260 | private string ADEBLDMsg(string cHDR, string cRPC, string cParam, string cMult)
|
---|
261 | {
|
---|
262 | //Builds RPC message
|
---|
263 | //Automatically splits parameters longer than 200 into subscripted array
|
---|
264 | string cMSG;
|
---|
265 | string sBuild = "";
|
---|
266 | string sPiece = "";
|
---|
267 | string sBig = "";
|
---|
268 | int l;
|
---|
269 | int nLength;
|
---|
270 |
|
---|
271 | if (cParam == "")
|
---|
272 | {
|
---|
273 | cMSG = "0" + cRPC ;
|
---|
274 | }
|
---|
275 | else
|
---|
276 | {
|
---|
277 | l = PieceLength(cParam, "^");
|
---|
278 | for (int j=1; j <= l; j++)
|
---|
279 | {
|
---|
280 | sPiece = Piece(cParam, "^", j);
|
---|
281 | if ((j == l) && (sPiece.Length > 200))
|
---|
282 | {
|
---|
283 | //Split up long param into array pieces
|
---|
284 | sBig = sPiece;
|
---|
285 | sPiece = ".x";
|
---|
286 | nLength = sPiece.Length + 1;
|
---|
287 | sPiece = ADEBLDPadString(nLength.ToString()) + "2" + sPiece;
|
---|
288 | sBuild = sBuild + sPiece;
|
---|
289 | int nSubscript = 1;
|
---|
290 | string sSubscript ="";
|
---|
291 | int nSubLen = 0;
|
---|
292 | string sSubLen ="";
|
---|
293 | int nBigLen = sBig.Length;
|
---|
294 | string sHunk ="";
|
---|
295 | int nHunkLen = 0;
|
---|
296 | string sHunkLen ="";
|
---|
297 | do
|
---|
298 | {
|
---|
299 | sHunk = sBig.Substring(0,200);
|
---|
300 | sBig = sBig.Substring(201, sBig.Length + 1);
|
---|
301 | nBigLen = sBig.Length;
|
---|
302 | sSubscript = nSubscript.ToString();
|
---|
303 | nSubLen = sSubscript.Length;
|
---|
304 | sSubLen = nSubLen.ToString();
|
---|
305 | sSubLen = ADEBLDPadString(sSubLen);
|
---|
306 | nHunkLen = sHunk.Length;
|
---|
307 | sHunkLen = nHunkLen.ToString();
|
---|
308 | sHunkLen = ADEBLDPadString(sHunkLen);
|
---|
309 | cMult = cMult + sSubLen + sSubscript + sHunkLen + sHunk;
|
---|
310 | nSubscript++;
|
---|
311 | } while (nBigLen > 0);
|
---|
312 | }
|
---|
313 | else
|
---|
314 | {
|
---|
315 | nLength = sPiece.Length +1;
|
---|
316 | sPiece = ADEBLDPadString(nLength.ToString()) + "0" + sPiece;
|
---|
317 | sBuild = sBuild + sPiece;
|
---|
318 | }
|
---|
319 | }
|
---|
320 | nLength = sBuild.Length;
|
---|
321 | string sTotLen = ADEBLDPadString(nLength.ToString(),5);
|
---|
322 | if (cMult.Length > 0)
|
---|
323 | {
|
---|
324 | cMSG = "1"+ cRPC + "^" +sTotLen + sBuild;
|
---|
325 | }
|
---|
326 | else
|
---|
327 | {
|
---|
328 | cMSG = "0"+ cRPC + "^" +sTotLen + sBuild;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | cMSG = ADEBLDB(cMSG, 5);
|
---|
332 | cMSG = cHDR + cMSG;
|
---|
333 | return cMSG;
|
---|
334 | }
|
---|
335 |
|
---|
336 | private string ADEEncryp(string sInput)
|
---|
337 | {
|
---|
338 | //Encrypt a string
|
---|
339 | string strResult;
|
---|
340 | string strPercent;
|
---|
341 | string strAssoc;
|
---|
342 | string strIdix;
|
---|
343 | int nPercent;
|
---|
344 | int nAssocix;
|
---|
345 | int nIdix;
|
---|
346 | Debug.Assert(sInput != "");
|
---|
347 | System.Random rRand = new Random(DateTime.Now.Second);
|
---|
348 |
|
---|
349 | nPercent = rRand.Next(0,10000);
|
---|
350 | nPercent += 72439;
|
---|
351 | nAssocix = nPercent % 20;
|
---|
352 | nAssocix++;
|
---|
353 | Debug.Assert(nAssocix < 21);
|
---|
354 | strPercent = nPercent.ToString();
|
---|
355 | strPercent = strPercent.Substring(1,2);
|
---|
356 | nIdix = Convert.ToInt32(strPercent);
|
---|
357 | nIdix = nIdix % 20;
|
---|
358 | nIdix++;
|
---|
359 | Debug.Assert(nIdix < 21);
|
---|
360 |
|
---|
361 | const int nEncryptBase = 101;
|
---|
362 | strAssoc = LoadKey(nEncryptBase + nAssocix);
|
---|
363 | Debug.Assert(strAssoc.Length == 94);
|
---|
364 | strIdix = LoadKey(nEncryptBase + nIdix);
|
---|
365 | Debug.Assert(strIdix.Length == 94);
|
---|
366 | string sEncrypted = "";
|
---|
367 |
|
---|
368 | foreach (char c in sInput)
|
---|
369 | {
|
---|
370 | string d = c.ToString();
|
---|
371 | int nFindChar = FindChar(strIdix, c);
|
---|
372 | if (nFindChar > -1)
|
---|
373 | {
|
---|
374 | d = strAssoc.Substring(nFindChar,1);
|
---|
375 | }
|
---|
376 | sEncrypted += d;
|
---|
377 | }
|
---|
378 |
|
---|
379 | strResult = (char) (nIdix + 31) + sEncrypted + (char) (nAssocix + 31);
|
---|
380 |
|
---|
381 | return strResult;
|
---|
382 | }
|
---|
383 |
|
---|
384 | private int FindChar(byte[] c, char y)
|
---|
385 | {
|
---|
386 | int n = 0;
|
---|
387 | int nRet = -1;
|
---|
388 | for (n=0; n < c.Length; n++)
|
---|
389 | {
|
---|
390 | if (y == (char) c[n])
|
---|
391 | {
|
---|
392 | nRet = n;
|
---|
393 | break;
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | return nRet;
|
---|
398 | }
|
---|
399 |
|
---|
400 | private int FindChar(string s, char y)
|
---|
401 | {
|
---|
402 | int n = 0;
|
---|
403 | int nRet = -1;
|
---|
404 | foreach (char c in s)
|
---|
405 | {
|
---|
406 | if (y == c)
|
---|
407 | {
|
---|
408 | nRet = n;
|
---|
409 | break;
|
---|
410 | }
|
---|
411 | n++;
|
---|
412 | }
|
---|
413 | return nRet;
|
---|
414 | }
|
---|
415 |
|
---|
416 | private string LoadKey(int nID)
|
---|
417 | {
|
---|
418 | nID -= 102;
|
---|
419 | Debug.Assert( nID < 20);
|
---|
420 | return m_sKey[nID];
|
---|
421 | }
|
---|
422 |
|
---|
423 | // private string GetLocalAddress()
|
---|
424 | // {
|
---|
425 | // return "";
|
---|
426 | // }
|
---|
427 |
|
---|
428 | public bool OpenConnection(string sServerAddress, string sAccess, string sVerify)
|
---|
429 | {
|
---|
430 | try
|
---|
431 | {
|
---|
432 | m_cServerAddress = sServerAddress;
|
---|
433 | m_cAccess = sAccess;
|
---|
434 | m_cVerify = sVerify;
|
---|
435 |
|
---|
436 | //Get the local host address and available port;
|
---|
437 | IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
|
---|
438 | if (ipHostInfo.AddressList.Length < 1)
|
---|
439 | {
|
---|
440 | throw new BMXNetException("BMXNetLib.OpenConnection unable to find IP Address.");
|
---|
441 | }
|
---|
442 |
|
---|
443 | //Start the listener
|
---|
444 | IPAddress ipAddress = ipHostInfo.AddressList[0];
|
---|
445 | IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 0);
|
---|
446 | TcpListener listener = new TcpListener(localEndPoint);
|
---|
447 | listener.Start();
|
---|
448 | IPEndPoint ipEp = (IPEndPoint) listener.LocalEndpoint;
|
---|
449 | int nLocalPort = ipEp.Port;
|
---|
450 | string sLocalAddress = ipAddress.ToString();
|
---|
451 |
|
---|
452 | //Connect with the server
|
---|
453 | TcpClient connector = new TcpClient();
|
---|
454 | try
|
---|
455 | {
|
---|
456 | connector.Connect(m_cServerAddress, m_nMServerPort);
|
---|
457 | }
|
---|
458 | catch (SocketException exSocket)
|
---|
459 | {
|
---|
460 | string s = exSocket.Message + exSocket.StackTrace;
|
---|
461 | throw new BMXNetException(s);
|
---|
462 | }
|
---|
463 |
|
---|
464 | //Prepare & send the connect message
|
---|
465 | string cSend = "TCPconnect^" + sLocalAddress + "^" + nLocalPort.ToString() + "^";
|
---|
466 | int nLen = cSend.Length;
|
---|
467 | string sLen = nLen.ToString();
|
---|
468 | sLen = sLen.PadLeft(5, '0');
|
---|
469 | cSend = "{XWB}" + sLen + cSend;
|
---|
470 |
|
---|
471 | NetworkStream ns = connector.GetStream();
|
---|
472 | byte[] sendBytes = Encoding.ASCII.GetBytes(cSend);
|
---|
473 | ns.Write(sendBytes,0,sendBytes.Length);
|
---|
474 |
|
---|
475 | //Accept connection from server
|
---|
476 | DateTime dStartTime = DateTime.Now;
|
---|
477 | DateTime dEndTime;
|
---|
478 | bool bPending = false;
|
---|
479 | while (bPending == false)
|
---|
480 | {
|
---|
481 | if (listener.Pending() == true)
|
---|
482 | {
|
---|
483 | m_pCommSocket = listener.AcceptTcpClient();
|
---|
484 | bPending = true;
|
---|
485 | this.m_bConnected = true;
|
---|
486 | break;
|
---|
487 | }
|
---|
488 | dEndTime = DateTime.Now;
|
---|
489 | TimeSpan ds = dEndTime - dStartTime;
|
---|
490 | if (ds.TotalSeconds > 10) //TODO: Parameterize this timeout value
|
---|
491 | {
|
---|
492 | throw new Exception("AcceptTcpClient failed.");
|
---|
493 | }
|
---|
494 |
|
---|
495 | }
|
---|
496 |
|
---|
497 | try
|
---|
498 | {
|
---|
499 | bool bSecurity = SendSecurityRequest();
|
---|
500 | }
|
---|
501 | catch (Exception ex)
|
---|
502 | {
|
---|
503 | //Close the connection
|
---|
504 | SendString(m_pCommSocket, "#BYE#");
|
---|
505 | m_pCommSocket.Close();
|
---|
506 | m_bConnected = false;
|
---|
507 | m_cServerAddress = "";
|
---|
508 | throw ex;
|
---|
509 | }
|
---|
510 |
|
---|
511 | m_bConnected = true;
|
---|
512 |
|
---|
513 | // if ( bSecurity != true)
|
---|
514 | // {
|
---|
515 | // //Close the connection
|
---|
516 | // SendString(m_pCommSocket, "#BYE#");
|
---|
517 | // m_pCommSocket.Close();
|
---|
518 | // m_bConnected = false;
|
---|
519 | // m_cServerAddress = "";
|
---|
520 | // }
|
---|
521 | // else
|
---|
522 | // {
|
---|
523 | // m_bConnected = true;
|
---|
524 | // }
|
---|
525 | return m_bConnected;
|
---|
526 | }
|
---|
527 | catch (BMXNetException bmxEx)
|
---|
528 | {
|
---|
529 | throw bmxEx;
|
---|
530 | }
|
---|
531 | catch (Exception ex)
|
---|
532 | {
|
---|
533 | string s = ex.Message + ex.StackTrace;
|
---|
534 | throw new BMXNetException(s);
|
---|
535 | }
|
---|
536 |
|
---|
537 | }
|
---|
538 |
|
---|
539 | private void SendString(TcpClient tcpClient, string cSendString)
|
---|
540 | {
|
---|
541 | string sMult = "";
|
---|
542 | SendString(tcpClient, cSendString, sMult);
|
---|
543 | }
|
---|
544 |
|
---|
545 | private void SendString(TcpClient tcpClient, string cSendString, string cMult)
|
---|
546 | {
|
---|
547 | int nLen = cSendString.Length;
|
---|
548 | string sLen = nLen.ToString();
|
---|
549 | sLen = sLen.PadLeft(5, '0');
|
---|
550 | cSendString = sLen + cSendString;
|
---|
551 |
|
---|
552 | nLen += 15;
|
---|
553 | sLen = nLen.ToString();
|
---|
554 | sLen = sLen.PadLeft(5, '0');
|
---|
555 |
|
---|
556 | cSendString = "{XWB}" + sLen + cSendString;
|
---|
557 | cSendString = cSendString + cMult;
|
---|
558 |
|
---|
559 | NetworkStream ns = tcpClient.GetStream();
|
---|
560 | byte[] sendBytes = Encoding.ASCII.GetBytes(cSendString);
|
---|
561 | ns.Write(sendBytes,0,sendBytes.Length);
|
---|
562 |
|
---|
563 | return;
|
---|
564 | }
|
---|
565 |
|
---|
566 | private string ReceiveString(TcpClient tcpClient)
|
---|
567 | {
|
---|
568 | NetworkStream ns = tcpClient.GetStream();
|
---|
569 | do
|
---|
570 | {
|
---|
571 |
|
---|
572 | }while (ns.DataAvailable == false);
|
---|
573 |
|
---|
574 | byte[] bReadBuffer = new byte[1024];
|
---|
575 | string sReadBuffer = "";
|
---|
576 | StringBuilder sbAll = new StringBuilder("", 1024);
|
---|
577 | int numberOfBytesRead = 0;
|
---|
578 |
|
---|
579 | // Incoming message may be larger than the buffer size.
|
---|
580 |
|
---|
581 | numberOfBytesRead = ns.Read(bReadBuffer, 0, 2); //first two bytes are 0
|
---|
582 | bool bFinished = false;
|
---|
583 | int nFind = -1;
|
---|
584 | do
|
---|
585 | {
|
---|
586 | numberOfBytesRead = ns.Read(bReadBuffer, 0, bReadBuffer.Length);
|
---|
587 | nFind = FindChar(bReadBuffer, (char) 4);
|
---|
588 | if (nFind > -1)
|
---|
589 | bFinished = true;
|
---|
590 |
|
---|
591 | sReadBuffer = Encoding.ASCII.GetString(bReadBuffer, 0, numberOfBytesRead);
|
---|
592 | if (nFind > -1)
|
---|
593 | {
|
---|
594 | sbAll.Append(sReadBuffer, 0, numberOfBytesRead -1);
|
---|
595 | }
|
---|
596 | else
|
---|
597 | {
|
---|
598 | sbAll.Append(sReadBuffer);
|
---|
599 | }
|
---|
600 | }
|
---|
601 | while(bFinished == false);
|
---|
602 | return sbAll.ToString();
|
---|
603 |
|
---|
604 | }
|
---|
605 |
|
---|
606 | private bool SendSecurityRequest()
|
---|
607 | {
|
---|
608 | string strReceive = "";
|
---|
609 | string cAccess;
|
---|
610 | string cVerify;
|
---|
611 |
|
---|
612 | //Setup Signon Environment
|
---|
613 | string cMSG = this.ADEBLDB("0XUS SIGNON SETUP^00000", 5);
|
---|
614 | cMSG = m_cHDR + cMSG;
|
---|
615 | SendString(m_pCommSocket, cMSG);
|
---|
616 | strReceive = ReceiveString(m_pCommSocket);
|
---|
617 |
|
---|
618 | cAccess = m_cAccess.ToUpper();
|
---|
619 | cVerify = m_cVerify.ToUpper();
|
---|
620 |
|
---|
621 | //Build AV Call
|
---|
622 | string strAV = cAccess + ";" + cVerify;
|
---|
623 | strAV = ADEEncryp(strAV);
|
---|
624 | cMSG = ADEBLDMsg(m_cHDR, "XUS AV CODE", strAV,"");
|
---|
625 | SendString(m_pCommSocket, cMSG);
|
---|
626 |
|
---|
627 | strReceive = ReceiveString(m_pCommSocket);
|
---|
628 |
|
---|
629 | char[] cDelim = {(char) 13,(char) 10,(char) 0};
|
---|
630 | m_cAuthentication = strReceive;
|
---|
631 | string sDelim = new string(cDelim);
|
---|
632 | int nPiece = 1;
|
---|
633 | m_cDUZ = Piece(m_cAuthentication, sDelim , nPiece);
|
---|
634 | if ((m_cDUZ == "0")||(m_cDUZ == ""))
|
---|
635 | {
|
---|
636 | m_cAccess = "";
|
---|
637 | m_cVerify = "";
|
---|
638 | string sReason = Piece(m_cAuthentication, sDelim, 7);
|
---|
639 | throw new Exception(sReason);
|
---|
640 | }
|
---|
641 | m_cAccess = cAccess;
|
---|
642 | m_cVerify = cVerify;
|
---|
643 |
|
---|
644 | //Set up context
|
---|
645 | if (m_cAppContext == null)
|
---|
646 | m_cAppContext = "XUS SIGNON";
|
---|
647 |
|
---|
648 | SendString(m_pCommSocket, ADEBLDMsg(m_cHDR, "XWB CREATE CONTEXT", ADEEncryp(m_cAppContext), ""));
|
---|
649 | m_cAuthentication = ReceiveString(m_pCommSocket);
|
---|
650 |
|
---|
651 | return true;
|
---|
652 | }
|
---|
653 |
|
---|
654 | public void CloseConnection()
|
---|
655 | {
|
---|
656 | if (!m_bConnected)
|
---|
657 | {
|
---|
658 | return;
|
---|
659 | }
|
---|
660 | SendString(m_pCommSocket, "#BYE#");
|
---|
661 | m_pCommSocket.Close();
|
---|
662 | m_bConnected = false;
|
---|
663 | m_cServerAddress = "";
|
---|
664 | m_cAuthentication = "";
|
---|
665 | m_cDUZ2 = "";
|
---|
666 | m_cAccess = "";
|
---|
667 | m_cVerify = "";
|
---|
668 | m_cDUZ = "";
|
---|
669 | }
|
---|
670 |
|
---|
671 | public string TransmitRPC(string sRPC, string sParam)
|
---|
672 | {
|
---|
673 | if (m_bConnected == false)
|
---|
674 | {
|
---|
675 | //Raise Exception
|
---|
676 | throw new BMXNetException("BMXNetLib.TransmitRPC failed because BMXNetLib is not connected to RPMS.");
|
---|
677 | }
|
---|
678 | Debug.Assert(m_cDUZ != "");
|
---|
679 | Debug.Assert(m_pCommSocket != null);
|
---|
680 |
|
---|
681 | string sMult = "";
|
---|
682 | string sSend = ADEBLDMsg(m_cHDR, sRPC, sParam, sMult);
|
---|
683 | SendString(m_pCommSocket, sSend, sMult);
|
---|
684 | string strResult = ReceiveString(m_pCommSocket);
|
---|
685 | return strResult;
|
---|
686 | }
|
---|
687 |
|
---|
688 | public string GetLoginFacility()
|
---|
689 | {
|
---|
690 | try
|
---|
691 | {
|
---|
692 | if (m_bConnected == false)
|
---|
693 | {
|
---|
694 | throw new BMXNetException("BMXNetLib is not connected to RPMS");
|
---|
695 | }
|
---|
696 |
|
---|
697 | if (m_cLoginFacility != "")
|
---|
698 | {
|
---|
699 | return m_cLoginFacility;
|
---|
700 | }
|
---|
701 |
|
---|
702 | Debug.Assert(m_pCommSocket != null);
|
---|
703 | Debug.Assert(m_cDUZ != "");
|
---|
704 | SendString(m_pCommSocket, ADEBLDMsg(m_cHDR, "BMXGetFac", m_cDUZ, ""));
|
---|
705 | string sFac = ReceiveString(m_pCommSocket);
|
---|
706 | m_cLoginFacility = sFac;
|
---|
707 | return sFac;
|
---|
708 | }
|
---|
709 | catch (BMXNetException bmxEx)
|
---|
710 | {
|
---|
711 | string sMessage = bmxEx.Message + bmxEx.StackTrace;
|
---|
712 | throw new BMXNetException(sMessage);
|
---|
713 | }
|
---|
714 | catch (Exception ex)
|
---|
715 | {
|
---|
716 | throw ex;
|
---|
717 | }
|
---|
718 | }
|
---|
719 |
|
---|
720 | TcpClient m_pCommSocket;
|
---|
721 |
|
---|
722 | #endregion RPX Functions
|
---|
723 |
|
---|
724 | #region RPX Properties
|
---|
725 |
|
---|
726 | public string WKID
|
---|
727 | {
|
---|
728 | get
|
---|
729 | {
|
---|
730 | return m_sWKID;
|
---|
731 | }
|
---|
732 | set
|
---|
733 | {
|
---|
734 | m_sWKID = value;
|
---|
735 | }
|
---|
736 | }
|
---|
737 |
|
---|
738 | public string PRCH
|
---|
739 | {
|
---|
740 | get
|
---|
741 | {
|
---|
742 | return m_sPRCH;
|
---|
743 | }
|
---|
744 | set
|
---|
745 | {
|
---|
746 | m_sPRCH = value;
|
---|
747 | }
|
---|
748 | }
|
---|
749 |
|
---|
750 | public string WINH
|
---|
751 | {
|
---|
752 | get
|
---|
753 | {
|
---|
754 | return m_sWINH;
|
---|
755 | }
|
---|
756 | set
|
---|
757 | {
|
---|
758 | m_sWINH = value;
|
---|
759 | }
|
---|
760 | }
|
---|
761 |
|
---|
762 | public string WISH
|
---|
763 | {
|
---|
764 | get
|
---|
765 | {
|
---|
766 | return m_sWISH;
|
---|
767 | }
|
---|
768 | set
|
---|
769 | {
|
---|
770 | m_sWISH = value;
|
---|
771 | }
|
---|
772 | }
|
---|
773 |
|
---|
774 | public string AppContext
|
---|
775 | {
|
---|
776 | get
|
---|
777 | {
|
---|
778 | return m_cAppContext;
|
---|
779 | }
|
---|
780 | set
|
---|
781 | {
|
---|
782 | m_cAppContext = value;
|
---|
783 | //TODO: send the changed context to RPMS
|
---|
784 | }
|
---|
785 | }
|
---|
786 |
|
---|
787 | public bool Connected
|
---|
788 | {
|
---|
789 | get
|
---|
790 | {
|
---|
791 | return m_bConnected;
|
---|
792 | }
|
---|
793 | }
|
---|
794 |
|
---|
795 | public string DUZ
|
---|
796 | {
|
---|
797 | get
|
---|
798 | {
|
---|
799 | return m_cDUZ;
|
---|
800 | }
|
---|
801 | }
|
---|
802 |
|
---|
803 | // public string Error
|
---|
804 | // {
|
---|
805 | // get
|
---|
806 | // {
|
---|
807 | // return "";
|
---|
808 | // }
|
---|
809 | // }
|
---|
810 |
|
---|
811 | public string DUZ2
|
---|
812 | {
|
---|
813 | get
|
---|
814 | {
|
---|
815 | return m_cDUZ2;
|
---|
816 | }
|
---|
817 | set
|
---|
818 | {
|
---|
819 | m_cDUZ2 = value;
|
---|
820 | //TODO: send the changed context to RPMS
|
---|
821 | }
|
---|
822 | }
|
---|
823 |
|
---|
824 | public string MServerAddress
|
---|
825 | {
|
---|
826 | get
|
---|
827 | {
|
---|
828 | return m_cServerAddress;
|
---|
829 | }
|
---|
830 | }
|
---|
831 |
|
---|
832 | public int MServerPort
|
---|
833 | {
|
---|
834 | get
|
---|
835 | {
|
---|
836 | return m_nMServerPort;
|
---|
837 | }
|
---|
838 | set
|
---|
839 | {
|
---|
840 | m_nMServerPort = value;
|
---|
841 | }
|
---|
842 | }
|
---|
843 |
|
---|
844 | #endregion RPX Properties
|
---|
845 |
|
---|
846 |
|
---|
847 |
|
---|
848 |
|
---|
849 |
|
---|
850 |
|
---|
851 | }
|
---|
852 | }
|
---|