Beispiel #1
0
        public byte[] SendTls(byte[] request, string serverName)
        {
            if (!_client.Connected)
            {
                _client.Connect(_hostName, _port);
            }

            SslMllpNetworkSream stream = new SslMllpNetworkSream(
                new NetworkStream(_client.Client),
                false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate),
                null);

            try
            {
                stream.AuthenticateAsClient(serverName);
            }
            catch (AuthenticationException e)
            {
                //Platform.Log(LogLevel.Error, e);
                LogAdapter.Logger.TraceException(e);
                if (e.InnerException != null)
                {
                    LogAdapter.Logger.TraceException(e.InnerException);
                    //Platform.Log(LogLevel.Error, e.InnerException);
                }

                LogAdapter.Logger.Error("Authentication failed - closing the connection");
                //Platform.Log(LogLevel.Error, "Authentication failed - closing the connection");
                _client.Close();
                throw;
            }

            stream.WriteMessage(request, 0, request.Length);
            return(stream.ReadMessage());
        }
Beispiel #2
0
        private void TlsClientHandle(object obj)
        {
            Socket socket = obj as Socket;

            if (socket != null)
            {
                var stream = new SslMllpNetworkSream(new NetworkStream(socket), false);
                try
                {
                    stream.AuthenticateAsServer(_serverCertificate, false, SslProtocols.Default, true);
                }
                catch (AuthenticationException e)
                {
                    LogAdapter.Logger.TraceException(e);
                    //Platform.Log(LogLevel.Error, e);
                    stream.Close();
                    return;
                }
                catch (Exception e)
                {
                    LogAdapter.Logger.TraceException(e);
                    //Platform.Log(LogLevel.Error, e);
                    stream.Close();
                    return;
                }

                try
                {
                    while (true)
                    {
                        byte[] request = stream.ReadMessage();
                        if (HL7Setting.Default.LogRequestMessage)
                        {
                            LogAdapter.Logger.Info(string.Format("Receive an HL7 request message from {0} \n {1}",
                                                                 socket.RemoteEndPoint,
                                                                 Encoding.UTF8.GetString(request)));
                            //Platform.Log(
                            //    LogLevel.Info,
                            //    "Receive an HL7 request message from {0} \n {1}",
                            //    socket.RemoteEndPoint.ToString(),
                            //    Encoding.UTF8.GetString(request));
                        }

                        byte[] response = OnMessage(request);
                        if (HL7Setting.Default.LogResponseMessage)
                        {
                            LogAdapter.Logger.Info(string.Format("Sending HL7 Response message to {0} \n {1}",
                                                                 socket.RemoteEndPoint,
                                                                 Encoding.UTF8.GetString(response)));
                            //Platform.Log(
                            //    LogLevel.Info,
                            //    "Sending HL7 Response message to {0} \n {1}",
                            //    socket.RemoteEndPoint.ToString(),
                            //    Encoding.UTF8.GetString(response));
                        }

                        stream.WriteMessage(response, 0, response.Length);
                    }
                }
                catch (Exception ex)
                {
                    LogAdapter.Logger.TraceException(ex);
                    //Platform.Log(LogLevel.Warn, ex);
                }
                finally
                {
                    stream.Close();
                }
            }
        }