private void b_send_Click(object sender, EventArgs e) { String _receivedStr = txtTrama.Text + "\n"; if (rb_IP.Checked) { if (Server._connections.Count == 0) { MessageBox.Show("No tiene conexiones activas."); } else { TcpLib.ConnectionState cs = (TcpLib.ConnectionState)Server._connections[Server._connections.Count - 1]; cs.Write(Encoding.UTF8.GetBytes(_receivedStr), 0, _receivedStr.Length); _receivedStr = ""; } } if (rbSerial.Checked) { SerialPort port = new SerialPort("COM5"); port.BaudRate = 1200; port.Parity = Parity.None; port.StopBits = StopBits.One; port.DataBits = 8; port.Handshake = Handshake.None; port.RtsEnable = true; port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); port.Open(); // Write a string port.Write(_receivedStr); // Write a set of bytes //port.Write(new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3); port.Close(); } }
public override void OnReceiveData(ConnectionState state) { var funcName = "OnReceiveData(ConnectionState state)"; StLog.DebugWriteLine ("Daten empfangen", funcName); StLog.DebugWriteLine ("Anzahl der Verbindungen: " + state._server.CurrentConnections, funcName); byte[] buffer = new byte[1024]; while(state.AvailableData > 0) { int readBytes = state.Read(buffer, 0, 1024); if(readBytes > 0) { StLog.DebugWriteLine ("Daten gelesen", funcName); _receivedStr += enc_cp437.GetString (buffer, 0, readBytes); OnNewMessage (_receivedStr); _receivedStr = ""; } //If read fails then close connection } state.EndConnection(); StLog.DebugWriteLine ("Beende Verbindung. Anzahl der Verbindungen: " + state._server.CurrentConnections, funcName); }
/// <SUMMARY> /// Gets executed when the server detects incoming data. /// This method is called only if OnAcceptConnection has already finished. /// </SUMMARY> public abstract void OnReceiveData(ConnectionState state);
/// <SUMMARY> /// Gets executed when the server needs to shutdown the connection. /// </SUMMARY> public abstract void OnDropConnection(ConnectionState state);
/// <SUMMARY> /// Gets executed when the server accepts a new connection. /// </SUMMARY> public abstract void OnAcceptConnection(ConnectionState state);
/// <SUMMARY> /// Callback function: A new connection is waiting. /// </SUMMARY> private void ConnectionReady_Handler(IAsyncResult ar) { lock (this) { if (_listener == null) return; Socket conn = _listener.EndAccept (ar); if (_connections.Count >= _maxConnections) { //Max number of connections reached. string msg = "SE001: Server busy"; conn.Send (Encoding.UTF8.GetBytes (msg), 0, msg.Length, SocketFlags.None); conn.Shutdown (SocketShutdown.Both); conn.Close (); } else { //Start servicing a new connection ConnectionState st = new ConnectionState (); st._conn = conn; st._server = this; st._provider = (TcpServiceProvider)_provider.Clone (); st._buffer = new byte[4]; _connections.Add (st); //Queue the rest of the job to be executed latter ThreadPool.QueueUserWorkItem (AcceptConnection, st); } //Resume the listening callback loop _listener.BeginAccept (ConnectionReady, null); } }
/// <SUMMARY> /// Removes a connection from the list /// </SUMMARY> internal void DropConnection(ConnectionState st) { lock (this) { try { st._conn.Shutdown (SocketShutdown.Both); st._conn.Close (); } catch { // Console.Write (ex.Message); } finally { if (_connections.Contains (st)) _connections.Remove (st); } } }
public override void OnDropConnection(ConnectionState state) { var funcName = "OnDropConnection(ConnectionState state)"; StLog.DebugWriteLine ("Anzahl der Verbindungen: " + state._server.CurrentConnections, funcName); }
public override void OnAcceptConnection(ConnectionState state) { StLog.DebugWriteLine ("New Connection: " + state.RemoteEndPoint.ToString (), "OnAcceptConnection(ConnectionState state)"); }
public override void OnDropConnection(ConnectionState state) { //Nothing to clean here }