private EndAccept ( byte &buffer, int &bytesTransferred, IAsyncResult asyncResult ) : Socket | ||
buffer | byte | |
bytesTransferred | int | |
asyncResult | IAsyncResult | |
리턴 | Socket |
internal Socket EndAccept(out byte[] buffer, out int bytesTransferred, IAsyncResult asyncResult)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this, asyncResult);
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
AcceptOverlappedAsyncResult castedAsyncResult = asyncResult as AcceptOverlappedAsyncResult;
if (castedAsyncResult == null || castedAsyncResult.AsyncObject != this)
{
throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult));
}
if (castedAsyncResult.EndCalled)
{
throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndAccept"));
}
Socket socket = (Socket)castedAsyncResult.InternalWaitForCompletion();
bytesTransferred = (int)castedAsyncResult.BytesTransferred;
buffer = castedAsyncResult.Buffer;
castedAsyncResult.EndCalled = true;
if (s_perfCountersEnabled)
{
if (bytesTransferred > 0)
{
SocketPerfCounter.Instance.Increment(SocketPerfCounterName.SocketBytesReceived, bytesTransferred);
}
}
// Throw an appropriate SocketException if the native call failed asynchronously.
if ((SocketError)castedAsyncResult.ErrorCode != SocketError.Success)
{
// Update the internal state of this socket according to the error before throwing.
SocketException socketException = new SocketException(castedAsyncResult.ErrorCode);
UpdateStatusAfterSocketError(socketException);
if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
throw socketException;
}
#if TRACE_VERBOSE
if (NetEventSource.IsEnabled)
{
try
{
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"SRC:{LocalEndPoint} acceptedSocket:{socket} acceptedSocket.SRC:{socket.LocalEndPoint} acceptSocket.DST:{socket.RemoteEndPoint} bytesTransferred:{bytesTransferred}");
}
catch (ObjectDisposedException) { }
}
#endif
if (NetEventSource.IsEnabled)
{
NetEventSource.Accepted(socket, socket.RemoteEndPoint, socket.LocalEndPoint);
NetEventSource.Exit(this, socket);
}
return socket;
}
void OnNewClient(IAsyncResult _asyncResult) { System.Net.Sockets.Socket ClientSocket = ListenSocket.EndAccept(_asyncResult); ListenSocket.BeginAccept(OnNewClient, null); SetKeepAlive(ClientSocket, 1000 * 60, 1000); var NewClient = new TelnetClient { Socket = ClientSocket }; if (Clients.ClientConnected(NewClient) == ClientAcceptanceStatus.Rejected) { NewClient.WasRejected = true; ClientSocket.Close(); } else { // We will handle all the echoing echoing echoing //var echoCommand = new byte[] //{ // (byte)TelnetControlCodes.IAC, // (byte)TelnetControlCodes.Will, // TODO: Handle client response denying this request // (byte)TelnetControlCodes.Echo, // (byte)TelnetControlCodes.IAC, // (byte)TelnetControlCodes.Dont, // (byte)TelnetControlCodes.Echo, //}; //ClientSocket.Send(echoCommand); ClientSocket.BeginReceive(NewClient.Storage, 0, 1024, System.Net.Sockets.SocketFlags.Partial, OnData, NewClient); Console.WriteLine("New telnet client: " + ClientSocket.RemoteEndPoint.ToString()); } }