public override void OnAcceptConnection(ConnectionState state)
        {
            log("Connection from {0}", state.RemoteEndPoint.ToString());

            WriteLine(state, OHAI);
            if (DataSource != null)
            {
                foreach (var s in DataSource.GetData())
                {
                    WriteLine(state, s);
                }
            }
            WriteLine(state, KTHXBAI);

            log("Data Sent to {0}", state.RemoteEndPoint.ToString());

            state.EndConnection();
        }
Beispiel #2
0
 /// <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);
Beispiel #3
0
 /// <SUMMARY>
 /// Gets executed when the server needs to shutdown the connection.
 /// </SUMMARY>
 public abstract void OnDropConnection(ConnectionState state);
Beispiel #4
0
 /// <SUMMARY>
 /// Gets executed when the server accepts a new connection.
 /// </SUMMARY>
 public abstract void OnAcceptConnection(ConnectionState state);
Beispiel #5
0
        /// <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);
            }
        }
Beispiel #6
0
        /// <SUMMARY>
        /// Removes a connection from the list
        /// </SUMMARY>
        internal void DropConnection(ConnectionState st)
        {
            lock (this)
            {

                try
                {
                    st._provider.OnDropConnection(st);
                }
                catch
                { }

                try
                {
                    if (st._conn != null && st._conn.Connected)
                    {
                        st._conn.Shutdown(SocketShutdown.Both);
                        st._conn.Close();
                    }
                }
                catch
                { }

                if (_connections.Contains(st))
                    _connections.Remove(st);
            }
        }
 public override void OnReceiveData(ConnectionState state)
 {
     //Do nothing.
 }
 public override void OnDropConnection(ConnectionState state)
 {
     log("Connnection closed {0}", state.RemoteEndPoint.ToString());
 }
 protected bool WriteLine(ConnectionState state, string msg)
 {
     byte[] msgBytes = Encoding.ASCII.GetBytes(String.Format("{0}{1}", msg, Environment.NewLine));
     return state.Write(msgBytes, 0, msgBytes.Length);
 }