public void SendMessage() { string clearXmlRequest = XmlRequest; int serverConnectTimeout = 5000; // 5 Seconds //DevelopmentLicence myForm = new DevelopmentLicence(); // myForm.ShowDialog(); // used new methods // string base64String = Convert.ToBase64String(Encoding.Default.GetBytes(XmlRequest)); //XmlRequest = base64String; _myStatusForm = new StatusForm(); _myStatusForm.Show(); _myStatusForm.BringToFront(); _myStatusForm.Refresh(); try { string[] serveripandport; serveripandport = ServerUrl.Split(':'); string ip = serveripandport[0]; int port = System.Convert.ToInt32(serveripandport[1]); //TcpClient client = new TcpClient(ip, port); connectDone.Reset(); TcpClient client = new TcpClient(); try { OnStatusMessage(STR_Connecting); client.BeginConnect(ip, port, new AsyncCallback(ConnectCallback), client); // here the clients starts to connect asynchronously (because it is asynchronous it doesn't have a timeout only events are triggered) // immediately after calling beginconnect the control is returned to the method, now we need to wait for the event or connection timeout connectDone.WaitOne(serverConnectTimeout, false); // now application waits here for either an connection event (fi. connection accepted) or the timeout specified by connectionTimeout (in milliseconds) if (!client.Connected) { throw new SocketException(10061); } /*10061 = Could not connect to server */ client.SendTimeout = timeout; client.ReceiveTimeout = timeout; // client.NoDelay = true; client.ReceiveBufferSize = 32768; // First 20 bytes = length in Ascii. //string messageSize = "00000000000000000000" + XmlRequest.Length; //messageSize = messageSize.Substring(messageSize.Length - 20); //XmlRequest = messageSize + XmlRequest; XmlRequest = encodeXmlMessage(XmlRequest, true); OnStatusMessage(STR_Sending); Byte[] request = Encoding.Default.GetBytes(XmlRequest); client.GetStream().Write(request, 0, request.Length); client.GetStream().Flush(); byte[] xmlLength; string messageLength; int lenbytesRead; bool messageLoop = false; bool statusLoop = true; do { // Big Status Loop. do { // Inner Ack Loop, maybe we can get rid off. xmlLength = new byte[20]; messageLength = ""; lenbytesRead = client.GetStream().Read(xmlLength, 0, 20);//s.Receive(incomingBuffer); messageLength = Encoding.Default.GetString(xmlLength); // Log("Message size is " + messageLength); switch (messageLength) { case STR_ACK00000000000000000: OnStatusMessage(STR_AckReceived); messageLoop = true; break; default: OnStatusMessage(STR_Reading); messageLoop = false; break; } } while (messageLoop && client.Connected); int xmlMessageSize = Convert.ToInt32(messageLength); List <byte> dataBuffer = new List <byte>(); int bytesReadSoFar = 0; do { Byte[] response = new Byte[client.ReceiveBufferSize]; int bytesRead = client.GetStream().Read(response, 0, client.ReceiveBufferSize); bytesReadSoFar += bytesRead; byte[] resultArray = new byte[bytesRead]; Array.Copy(response, resultArray, bytesRead); dataBuffer.AddRange(resultArray); } while (bytesReadSoFar < xmlMessageSize); XmlResponse = Encoding.Default.GetString(dataBuffer.ToArray(), 0, dataBuffer.Count).Trim(); //Log("Received response: " + XmlResponse); if (XmlResponse.IndexOf('<') < 0) { // Log("Message was encrypted"); XmlResponse = XmlResponse.Replace("\0", ""); XmlResponse = decodeXmlMessage(XmlResponse, true); } if (XmlResponse.IndexOf("<StatusResponse") > 0) { //Do something with the status message.... try { XmlSerializer serializer = new XmlSerializer(typeof(HicapsConnectControl.StatusResponse)); MemoryStream myResultMS = new MemoryStream(Encoding.Default.GetBytes(XmlResponse)); HicapsConnectControl.StatusResponse myResult = (HicapsConnectControl.StatusResponse)serializer.Deserialize(myResultMS); OnStatusMessage(myResult.ResponseText); statusLoop = true; } catch (Exception ex) { statusLoop = false; } } else { statusLoop = false; } } while (client.Connected && statusLoop); client.Close(); } catch (SocketException) { string className = HicapsConnectControl.BaseMessage.extractClassName(clearXmlRequest); XmlResponse = returnNetworkErrorXml("ED", String.Format(STR_DestinationErrorCouldNotConnectToServer, ServerUrl), className); } catch (TimeoutException) { string className = HicapsConnectControl.BaseMessage.extractClassName(clearXmlRequest); XmlResponse = returnNetworkErrorXml("EN", String.Format(STR_NetworkRequestTimedOut, ServerUrl), className); } catch (Exception) { string className = HicapsConnectControl.BaseMessage.extractClassName(clearXmlRequest); XmlResponse = returnNetworkErrorXml("EN", STR_NetworkError, className); } client.Close(); //Console.ReadLine(); //client.Close(); } catch (Exception ex) { //lock (ignoreServerList) //{ // if (!ignoreServerList.Contains(server)) // { // ignoreServerList.Add(server); // } //} } if (XmlResponse.Length == 0 || XmlResponse == "Que ? Go away") { string className = HicapsConnectControl.BaseMessage.extractClassName(clearXmlRequest); XmlResponse = returnNetworkErrorXml("EN", "Network Error", className); } OnStatusMessage("Waiting"); OnCompletedMessage(); return; }
private string SendMessageLocal(string XmlRequest, int timeout) { string XmlResponse = ""; string clearXmlRequest = XmlRequest; int serverConnectTimeout = 5000; //DevelopmentLicence myForm = new DevelopmentLicence(); // myForm.ShowDialog(); try { NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "HicapsConnectPipe", PipeDirection.InOut); // Connect to the pipe or wait until the pipe is available. setStatus(STR_Connecting); pipeClient.Connect(serverConnectTimeout); XmlRequest = encodeXmlMessage(XmlRequest, true); setStatus(STR_Sending); Byte[] request = Encoding.Default.GetBytes(XmlRequest); pipeClient.Write(request, 0, request.Length); pipeClient.Flush(); byte[] xmlLength; string messageLength; int lenbytesRead; bool messageLoop = false; bool statusLoop = true; do { // Big Status Loop. do { // Inner Ack Loop, maybe we can get rid off. xmlLength = new byte[20]; messageLength = ""; lenbytesRead = pipeClient.Read(xmlLength, 0, 20);//s.Receive(incomingBuffer); messageLength = Encoding.Default.GetString(xmlLength); // Log("Message size is " + messageLength); switch (messageLength) { case STR_ACK00000000000000000: setStatus(STR_AckReceived); messageLoop = true; break; default: setStatus(STR_Reading); messageLoop = false; break; } } while (messageLoop && pipeClient.IsConnected); int xmlMessageSize = Convert.ToInt32(messageLength); //Byte[] response = new Byte[xmlMessageSize]; // int bytesRead = client.GetStream().Read(response, 0, xmlMessageSize); //byte[] resultArray = new byte[bytesRead]; // Array.Copy(response, resultArray, bytesRead); //XmlResponse = Encoding.Default.GetString(response).Trim(); List <byte> dataBuffer = new List <byte>(); int bytesReadSoFar = 0; do { Byte[] response = new Byte[8192]; int bytesRead = pipeClient.Read(response, 0, response.Length); bytesReadSoFar += bytesRead; byte[] resultArray = new byte[bytesRead]; Array.Copy(response, resultArray, bytesRead); dataBuffer.AddRange(resultArray); } while (bytesReadSoFar < xmlMessageSize); XmlResponse = Encoding.Default.GetString(dataBuffer.ToArray(), 0, dataBuffer.Count).Trim(); // Log("Received response: " + XmlResponse); if (XmlResponse.IndexOf('<') < 0) { // Log("Message was encrypted"); XmlResponse = XmlResponse.Replace("\0", ""); XmlResponse = decodeXmlMessage(XmlResponse, true); } if (XmlResponse.IndexOf("<StatusResponse") > 0) { //Do something with the status message.... try { XmlSerializer serializer = new XmlSerializer(typeof(HicapsConnectControl.StatusResponse)); MemoryStream myResultMS = new MemoryStream(Encoding.Default.GetBytes(XmlResponse)); HicapsConnectControl.StatusResponse myResult = (HicapsConnectControl.StatusResponse)serializer.Deserialize(myResultMS); setStatus(myResult.ResponseText); statusLoop = true; } catch (Exception ex) { statusLoop = false; } } else { statusLoop = false; } } while (pipeClient.IsConnected && statusLoop); pipeClient.Close(); } catch (TimeoutException te) { string className = HicapsConnectControl.BaseMessage.extractClassName(clearXmlRequest); XmlResponse = returnNetworkErrorXml("EN", String.Format(STR_StandAloneRequestTimedOutCheckServiceIsRunning), className); return(XmlResponse); } catch (Exception ex) { } return(XmlResponse); }