Exemple #1
0
            /// <summary>
            /// handle new connections to a server asynchronously (high performance)
            /// </summary>
            /// <param name="ar"></param>
            private void OnServerAccepted(IAsyncResult ar)
            {
                TcpClient theClient = ar.AsyncState as TcpClient;

                if (theClient == null)
                {
                    return;
                }


                try
                {
                    theClient.EndConnect(ar);
                    TCP_ClientContext context = new TCP_ClientContext(theClient, LogTrace, bVerboseData, logMsgs, sslSettings);

                    if (NewClientCallback != null)
                    {
                        NewClientCallback(context, true, null);
                    }
                }
                catch (Exception ex)
                {
                    NewClientCallback(null, false, ex);
                    LogTrace.TraceEvent(TraceEventType.Error, AcceptConnLogID, "Connection failed with exception: " + ex.ToString());
                    //Console.WriteLine(ex);
                }
            }
Exemple #2
0
            /// <summary>
            /// Blocking connect function. Calling function must handle exceptions.
            /// </summary>
            /// <param name="hostname"></param>
            /// <param name="port"></param>
            /// <param name="log">Log function to be used for communication protocol</param>
            /// <returns></returns>
            public ClientContext ConnectToServer(string hostname, Int32 port)
            {
                TcpClient theClient = new TcpClient();

                theClient.Connect(hostname, port);
                TCP_ClientContext context = new TCP_ClientContext(theClient, LogTrace, bVerboseData, logMsgs, sslSettings);

                return(context);
            }
Exemple #3
0
            private delegate void makeOnClientAsync(); //IAsyncResult ar, TcpListener listener);

            //handle new connections asynchronously (high performance)
            private void OnClientAccepted(IAsyncResult ar)
            {
                TcpListener listener = ar.AsyncState as TcpListener;

                if (listener == null)
                {
                    return;
                }

                makeOnClientAsync caller = delegate() //IAsyncResult ar, TcpListener listener)
                {
                    try
                    {
                        TCP_ClientContext context = new TCP_ClientContext(listener.EndAcceptTcpClient(ar), LogTrace, bVerboseData, logMsgs, sslSettings);

                        if (NewClientCallback != null)
                        {
                            NewClientCallback(context, true, null);
                        }
                    }
                    catch (Exception ex)
                    {
                        NewClientCallback(null, false, ex);
                        LogTrace.TraceEvent(TraceEventType.Error, AcceptConnLogID, "Caught exception when accepting a socket: " + ex
                                            + "\r\n\r\n Will continue accepting sockets");
                    }
                };

                try
                {
                    caller.BeginInvoke(delegate(IAsyncResult arr) { caller.EndInvoke(arr); }, null);
                }
                catch (Exception ex)
                {
                    NewClientCallback(null, false, ex);
                    LogTrace.TraceEvent(TraceEventType.Error, AcceptConnLogID, "Caught exception when trying to asynchronously connect a socket: " + ex
                                        + "\r\n\r\n Will continue accepting sockets");
                }
                finally
                {
                    listener.BeginAcceptTcpClient(OnClientAccepted, listener);
                }
            }