/// <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> /// Callback function: A new connection is waiting. /// </SUMMARY> private void ConnectionReady_Handler(IAsyncResult ar) { lock (this) { if (mListener == null) { return; } Socket conn = mListener.EndAccept(ar); if (mConnections.Count >= mMaxConnections) { //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)mProvider.Clone(); st._buffer = new byte[4]; mConnections.Add(st); //Queue the rest of the job to be executed latter ThreadPool.QueueUserWorkItem(AcceptConnection, st); } //Resume the listening callback loop mListener.BeginAccept(ConnectionReady, null); } }
/// <SUMMARY> /// Gets executed when the server accepts a new connection. /// </SUMMARY> public abstract void OnAcceptConnection(ConnectionState state);
/// <SUMMARY> /// Removes a connection from the list /// </SUMMARY> internal void DropConnection(ConnectionState st) { lock (this) { if (mConnections.Contains(st)) { mConnections.Remove(st); } st._provider.OnDropConnection(st); try { st._conn.Shutdown(SocketShutdown.Both); st._conn.Close(); } catch (System.Exception e) { //Ignore object disposed exceptions if (!(e is ObjectDisposedException)) { Program.Logger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Network, string.Format("Caught un-expected network exception: {0}", e)); } } } }