/// <summary> /// Install a callback handler that will be invoked if the connection is closed. /// </summary> /// <description>The handler is called when the connection is closed no matter if the connection was closed /// by the client or by the server. Any new call to this function will replace the callback handler installed /// by a prior function call.</description> /// <param name="handler">The user provided callback handler</param> /// <exception cref="IedConnectionException">This exception is thrown if there is a connection or service error</exception> public void InstallConnectionClosedHandler(ConnectionClosedHandler handler) { connectionClosedHandler = new InternalConnectionClosedHandler(MyConnectionClosedHandler); userProvidedHandler = handler; IedConnection_installConnectionClosedHandler(connection, connectionClosedHandler, connection); }
/// <summary> /// Close the socket /// </summary> public void Close() { if (m_closed) { return; } // Send the close frame SendFrame(CloseFrame, null, 0, 0); m_closed = true; // Notify any listeners ConnectionClosedHandler handler = ConnectionClosed; if (handler != null) { handler(this); } }
public Client(TcpClient socket, MessageRecievedHandler mHandler, ConnectionClosedHandler cHandler) { this.socket = socket; this.mHandler = mHandler; this.cHandler = cHandler; clientIn = new StreamReader(socket.GetStream()); clientOut = new StreamWriter(socket.GetStream()); clientOut.AutoFlush = true; //exchange port numbers with remote hosts; clientOut.WriteLine(Program.myPort); RemotePort = int.Parse(clientIn.ReadLine()); Console.WriteLine("Verbonden: " + Program.ConvertToPort(RemotePort)); //start the message handling thread Thread t = new Thread(ReadMessages); t.Start(); }
/// <summary>Constructs a new instance.</summary> /// <param name="socketNumber">The socket that this module is plugged in to.</param> public WiFiRN171(int socketNumber) { Socket socket = Socket.GetSocket(socketNumber, true, this, null); var useFlowControl = socket.SupportsType('K'); this.LocalIP = "0.0.0.0"; this.LocalListenPort = "0"; this.DebugPort = null; this.Ready = false; this.commandModeResponse = ""; this.serialBuffer = ""; this.timeout = 10000; this.useDhcp = false; this.inCommandMode = false; this.updateComplete = new AutoResetEvent(false); this.commandResponseComplete = new AutoResetEvent(false); this.awaitingPostData = false; this.httpEnabled = false; this.httpBuffer = ""; this.currentRequest = null; this.onHttpRequestReceived = this.OnHttpRequestReceived; this.onDataReceived = this.OnDataReceived; this.onLineReceived = this.OnLineReceived; this.onConnectionEstablished = this.OnConnectionEstablished; this.onConnectionClosed = this.OnConnectionClosed; if (!useFlowControl) { socket.EnsureTypeIsSupported('U', this); } this.reset = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Three, true, this); this.rts = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Six, false, this); this.SerialPort = GTI.SerialFactory.Create(socket, 115200, GTI.SerialParity.None, GTI.SerialStopBits.One, 8, useFlowControl ? GTI.HardwareFlowControl.Required : GTI.HardwareFlowControl.NotRequired, this); this.SerialPort.DataReceived += this.OnSerialDataReceived; this.SerialPort.Open(); this.Reset(); var end = DateTime.Now.AddMilliseconds(this.timeout); while (!this.Ready) { if (end <= DateTime.Now && this.timeout > 0) { throw new TimeoutException(); } Thread.Sleep(1); } this.EnterCommandMode(); this.ExecuteCommand("set sys printlvl 0"); this.ExecuteCommand("set comm remote 0"); if (useFlowControl) { this.ExecuteCommand("set uart flow 1"); } this.ExecuteCommand("set uart tx 1"); this.ExitCommandMode(); }
/// <summary> /// Install a callback handler that will be invoked if the connection is closed. /// </summary> /// <description>The handler is called when the connection is closed no matter if the connection was closed /// by the client or by the server. Any new call to this function will replace the callback handler installed /// by a prior function call.</description> /// <param name="handler">The user provided callback handler</param> /// <exception cref="IedConnectionException">This exception is thrown if there is a connection or service error</exception> public void InstallConnectionClosedHandler(ConnectionClosedHandler handler) { connectionClosedHandler = new InternalConnectionClosedHandler (MyConnectionClosedHandler); userProvidedHandler = handler; IedConnection_installConnectionClosedHandler (connection, connectionClosedHandler, connection); }