public IProcessedMessage SendBytes(byte[] message) { _SendBytesEvent.Reset(); byte[] response; try { response = Send(message); } catch (Exception ex) { Close(); Logger.GetInstance().Write(ex, "An error happens when connecting to the server" + (IsSslEnabled ? " or An error in SSL connection." : ".")); if (Callback != null) { try { Callback(null); } //Notifies the caller that there is an error but prepare for a new error catch (Exception ex2) { Logger.GetInstance().Write(ex2, "Error in Callback (possibly, cannot process null parameter)."); } } return(null); } if (response == null) { return(null); } IProcessedMessage msg = MessageProcessor.GetInstance().Receive(response, this, this.SendBytesCallback); _SendBytesEvent.WaitOne(); return(msg); }
public void AcceptMessage() { if (_messageListener.SslCertificate == null) { _stream = _client.GetStream(); } else //Use SSL for communication { RemoteCertificateValidationCallback validationCallback = _messageListener.CertificateValidationCallback; if (!_messageListener.IsClientCertificateRequired) { validationCallback = null; //Ensure the callback won't be called } SslStream sslStream = new SslStream(_client.GetStream(), false, validationCallback); sslStream.AuthenticateAsServer(_messageListener.SslCertificate, _messageListener.IsClientCertificateRequired, _messageListener.SslProtocol, _messageListener.IsCertificateRevocationChecked); _stream = sslStream; } _stream.ReadTimeout = _messageListener.ReadTimeout; _stream.WriteTimeout = _messageListener.WriteTimeout; /**** This commented is the buggy code if the client doesn't close the socket. **** We need the length of message actually to determine the end of message ****byte[] bytes = new byte[1024]; ****byte[] b; ****List<byte> data = new List<byte>(); ****int bytesCount; ****while ((bytesCount = _stream.Read(bytes, 0, bytes.Length)) > 0) ****{ **** if (bytesCount != bytes.Length) **** { **** b = new byte[bytesCount]; **** Array.Copy(bytes, 0, b, 0, bytesCount); **** } **** else **** { **** b = bytes; **** } **** data.AddRange(b); **** //if (!_stream.DataAvailable) break; ****} **** ****bytes = data.ToArray();*/ byte[] bytes = null; //Let MessageProcessor get the message bytes. MessageProcessor.GetInstance().Receive(bytes, this); }
public IProcessedMessage SendModel() { return(MessageProcessor.GetInstance().Send(Model, this, Callback)); }
public void Start() { lock (MessageProcessor.GetInstance()) { if (!_isLoaded) { Logger.GetInstance().WriteLine("Loading configuration: " + (String.IsNullOrEmpty(ConfigPath) ? MessageProcessor.defaultConfigPath : ConfigPath)); try { if (_configParser != null) { MessageProcessor.GetInstance().Load(_configParser); } else if (!String.IsNullOrEmpty(ConfigPath)) { CreateXmlConfigParser(); MessageProcessor.GetInstance().Load(_configParser); } else { MessageProcessor.GetInstance().Load(); } } catch (Exception ex) { Logger.GetInstance().Write(ex, "Can't load configuration, application can't continue. "); return; } } _isLoaded = true; } try { System.Net.IPAddress ip = System.Net.IPAddress.Any; if (!String.IsNullOrEmpty(IPAddress)) { ip = Dns.GetHostAddresses(IPAddress)[0]; } _server = new TcpListener(ip, Port); _server.Start((int)SocketOptionName.MaxConnections); Logger.GetInstance().WriteLine("Starts listening port " + Port + (String.IsNullOrEmpty(IPAddress) ? "" : " on address " + IPAddress) + " ..."); EventHandler <ListeningEventArgs> handler = StartListeningEvent; if (handler != null) { handler(this, new ListeningEventArgs(ip, Port, DateTime.Now)); } _stopped = false; DoBeginAcceptTcpClient(); } catch (Exception e) { Logger.GetInstance().Write(e); Stop(); throw e; } }