123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635 |
- using System;
- using System.Collections.Concurrent;
- using System.Diagnostics;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- namespace NetCoreServer
- {
-
-
-
-
- public class SslServer : IDisposable
- {
-
-
-
-
-
-
- public SslServer(SslContext context, IPAddress address, int port) : this(context, new IPEndPoint(address, port)) {}
-
-
-
-
-
-
- public SslServer(SslContext context, string address, int port) : this(context, new IPEndPoint(IPAddress.Parse(address), port)) {}
-
-
-
-
-
- public SslServer(SslContext context, DnsEndPoint endpoint) : this(context, endpoint as EndPoint, endpoint.Host, endpoint.Port) {}
-
-
-
-
-
- public SslServer(SslContext context, IPEndPoint endpoint) : this(context, endpoint as EndPoint, endpoint.Address.ToString(), endpoint.Port) {}
-
-
-
-
-
-
-
- private SslServer(SslContext context, EndPoint endpoint, string address, int port)
- {
- Id = Guid.NewGuid();
- Address = address;
- Port = port;
- Context = context;
- Endpoint = endpoint;
- }
-
-
-
- public Guid Id { get; }
-
-
-
- public string Address { get; }
-
-
-
- public int Port { get; }
-
-
-
- public SslContext Context { get; }
-
-
-
- public EndPoint Endpoint { get; private set; }
-
-
-
- public long ConnectedSessions { get { return Sessions.Count; } }
-
-
-
- public long BytesPending { get { return _bytesPending; } }
-
-
-
- public long BytesSent { get { return _bytesSent; } }
-
-
-
- public long BytesReceived { get { return _bytesReceived; } }
-
-
-
-
-
-
- public int OptionAcceptorBacklog { get; set; } = 1024;
-
-
-
-
-
-
-
- public bool OptionDualMode { get; set; }
-
-
-
-
-
-
- public bool OptionKeepAlive { get; set; }
-
-
-
-
-
-
- public int OptionTcpKeepAliveTime { get; set; } = -1;
-
-
-
-
-
-
- public int OptionTcpKeepAliveInterval { get; set; } = -1;
-
-
-
-
-
-
- public int OptionTcpKeepAliveRetryCount { get; set; } = -1;
-
-
-
-
-
-
- public bool OptionNoDelay { get; set; }
-
-
-
-
-
-
- public bool OptionReuseAddress { get; set; }
-
-
-
-
-
-
- public bool OptionExclusiveAddressUse { get; set; }
-
-
-
- public int OptionReceiveBufferSize { get; set; } = 8192;
-
-
-
- public int OptionSendBufferSize { get; set; } = 8192;
- #region Start/Stop server
-
- private Socket _acceptorSocket;
- private SocketAsyncEventArgs _acceptorEventArg;
-
- internal long _bytesPending;
- internal long _bytesSent;
- internal long _bytesReceived;
-
-
-
- public bool IsStarted { get; private set; }
-
-
-
- public bool IsAccepting { get; private set; }
-
-
-
-
-
-
-
- protected virtual Socket CreateSocket()
- {
- return new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- }
-
-
-
-
- public virtual bool Start()
- {
- Debug.Assert(!IsStarted, "SSL server is already started!");
- if (IsStarted)
- return false;
-
- _acceptorEventArg = new SocketAsyncEventArgs();
- _acceptorEventArg.Completed += OnAsyncCompleted;
-
- _acceptorSocket = CreateSocket();
-
- IsSocketDisposed = false;
-
- _acceptorSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, OptionReuseAddress);
-
- _acceptorSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, OptionExclusiveAddressUse);
-
- if (_acceptorSocket.AddressFamily == AddressFamily.InterNetworkV6)
- _acceptorSocket.DualMode = OptionDualMode;
-
- _acceptorSocket.Bind(Endpoint);
-
- Endpoint = _acceptorSocket.LocalEndPoint;
-
- OnStarting();
-
- _acceptorSocket.Listen(OptionAcceptorBacklog);
-
- _bytesPending = 0;
- _bytesSent = 0;
- _bytesReceived = 0;
-
- IsStarted = true;
-
- OnStarted();
-
- IsAccepting = true;
- StartAccept(_acceptorEventArg);
- return true;
- }
-
-
-
-
- public virtual bool Stop()
- {
- Debug.Assert(IsStarted, "SSL server is not started!");
- if (!IsStarted)
- return false;
-
- IsAccepting = false;
-
- _acceptorEventArg.Completed -= OnAsyncCompleted;
-
- OnStopping();
- try
- {
-
- _acceptorSocket.Close();
-
- _acceptorSocket.Dispose();
-
- _acceptorEventArg.Dispose();
-
- IsSocketDisposed = true;
- }
- catch (ObjectDisposedException) {}
-
- DisconnectAll();
-
- IsStarted = false;
-
- OnStopped();
- return true;
- }
-
-
-
-
- public virtual bool Restart()
- {
- if (!Stop())
- return false;
- while (IsStarted)
- Thread.Yield();
- return Start();
- }
- #endregion
- #region Accepting clients
-
-
-
- private void StartAccept(SocketAsyncEventArgs e)
- {
-
- e.AcceptSocket = null;
-
- if (!_acceptorSocket.AcceptAsync(e))
- ProcessAccept(e);
- }
-
-
-
- private void ProcessAccept(SocketAsyncEventArgs e)
- {
- if (e.SocketError == SocketError.Success)
- {
-
- var session = CreateSession();
-
- RegisterSession(session);
-
- session.Connect(e.AcceptSocket);
- }
- else
- SendError(e.SocketError);
-
- if (IsAccepting)
- StartAccept(e);
- }
-
-
-
-
- private void OnAsyncCompleted(object sender, SocketAsyncEventArgs e)
- {
- if (IsSocketDisposed)
- return;
- ProcessAccept(e);
- }
- #endregion
- #region Session factory
-
-
-
-
- protected virtual SslSession CreateSession() { return new SslSession(this); }
- #endregion
- #region Session management
-
-
-
- protected readonly ConcurrentDictionary<Guid, SslSession> Sessions = new ConcurrentDictionary<Guid, SslSession>();
-
-
-
-
- public virtual bool DisconnectAll()
- {
- if (!IsStarted)
- return false;
-
- foreach (var session in Sessions.Values)
- session.Disconnect();
- return true;
- }
-
-
-
-
-
- public SslSession FindSession(Guid id)
- {
-
- return Sessions.TryGetValue(id, out SslSession result) ? result : null;
- }
-
-
-
-
- internal void RegisterSession(SslSession session)
- {
-
- Sessions.TryAdd(session.Id, session);
- }
-
-
-
-
- internal void UnregisterSession(Guid id)
- {
-
- Sessions.TryRemove(id, out SslSession _);
- }
- #endregion
- #region Multicasting
-
-
-
-
-
- public virtual bool Multicast(byte[] buffer) => Multicast(buffer.AsSpan());
-
-
-
-
-
-
-
- public virtual bool Multicast(byte[] buffer, long offset, long size) => Multicast(buffer.AsSpan((int)offset, (int)size));
-
-
-
-
-
- public virtual bool Multicast(ReadOnlySpan<byte> buffer)
- {
- if (!IsStarted)
- return false;
- if (buffer.IsEmpty)
- return true;
-
- foreach (var session in Sessions.Values)
- session.SendAsync(buffer);
- return true;
- }
-
-
-
-
-
- public virtual bool Multicast(string text) => Multicast(Encoding.UTF8.GetBytes(text));
-
-
-
-
-
- public virtual bool Multicast(ReadOnlySpan<char> text) => Multicast(Encoding.UTF8.GetBytes(text.ToArray()));
- #endregion
- #region Server handlers
-
-
-
- protected virtual void OnStarting() {}
-
-
-
- protected virtual void OnStarted() {}
-
-
-
- protected virtual void OnStopping() {}
-
-
-
- protected virtual void OnStopped() {}
-
-
-
-
- protected virtual void OnConnecting(SslSession session) {}
-
-
-
-
- protected virtual void OnConnected(SslSession session) {}
-
-
-
-
- protected virtual void OnHandshaking(SslSession session) {}
-
-
-
-
- protected virtual void OnHandshaked(SslSession session) {}
-
-
-
-
- protected virtual void OnDisconnecting(SslSession session) {}
-
-
-
-
- protected virtual void OnDisconnected(SslSession session) {}
-
-
-
-
- protected virtual void OnError(SocketError error) {}
- internal void OnConnectingInternal(SslSession session) { OnConnecting(session); }
- internal void OnConnectedInternal(SslSession session) { OnConnected(session); }
- internal void OnHandshakingInternal(SslSession session) { OnHandshaking(session); }
- internal void OnHandshakedInternal(SslSession session) { OnHandshaked(session); }
- internal void OnDisconnectingInternal(SslSession session) { OnDisconnecting(session); }
- internal void OnDisconnectedInternal(SslSession session) { OnDisconnected(session); }
- #endregion
- #region Error handling
-
-
-
-
- private void SendError(SocketError error)
- {
-
- if ((error == SocketError.ConnectionAborted) ||
- (error == SocketError.ConnectionRefused) ||
- (error == SocketError.ConnectionReset) ||
- (error == SocketError.OperationAborted) ||
- (error == SocketError.Shutdown))
- return;
- OnError(error);
- }
- #endregion
- #region IDisposable implementation
-
-
-
- public bool IsDisposed { get; private set; }
-
-
-
- public bool IsSocketDisposed { get; private set; } = true;
-
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposingManagedResources)
- {
-
-
-
-
-
-
-
-
-
-
-
- if (!IsDisposed)
- {
- if (disposingManagedResources)
- {
-
- Stop();
- }
-
-
-
- IsDisposed = true;
- }
- }
- #endregion
- }
- }
|