internal static void InitClient(this Client client, string address, int port)
        {
            if (client.socket == null)
            {
                client.id      = -1;
                client.address = address;
                client.port    = port;

                using (client.mreBeginConnect = new ManualResetEvent(false))
                {
                    client.mreBeginConnect.Reset();
                    client.BeginConnect();
                    client.mreBeginConnect.WaitOne();
                }

                if (!client.closed)
                {
                    client.InitFinal();

                    client.RaiseConnected();
                }
                else
                {
                    client.RaiseDisconnected();
                }
            }
        }
Ejemplo n.º 2
0
        internal static void Disconnect(this Client client)
        {
            try
            {
                if (!client.closed)
                {
                    client.closed = true;

                    client.mreIsConnected.SetIfNotNull();
                    client.mreBeginReceive.SetIfNotNull();
                    client.mreBeginSend.SetIfNotNull();
                    client.mreEndSend.SetIfNotNull();
                    client.mreMonitorDisconnect.SetIfNotNull();

                    if (client.socket != null)
                    {
                        lock (client.socket)
                        {
                            if (client.socket != null)
                            {
                                client.socket.Shutdown(SocketShutdown.Both);
                                BeginDisconnect(client);

                                client.socket.Close();
                                client.socket.Dispose();
                                client.socket = null;

                                client.RaiseDisconnected();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                client.HandleError(e);
            }
        }