示例#1
0
        public void ReadCallback(IAsyncResult ar)
        {
            String content = String.Empty;
            string Name    = string.Empty;
            string pasw    = string.Empty;

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            Usuario     Usu     = (Usuario)ar.AsyncState;
            StateObject state   = Usu.VSc;
            Socket      handler = state.workSocket;

            state.sb.Clear();
            // Read data from the client socket.
            try
            {
                int bytesRead = handler.EndReceive(ar);

                if (bytesRead > 0)
                {
                    // There  might be more data, so store the data received so far.
                    state.sb.Append(Encoding.GetEncoding(437).GetString(
                                        state.buffer, 0, bytesRead));

                    // Check for end-of-file tag. If it is not there, read
                    // more data.
                    content = state.sb.ToString();

                    if (content.IndexOf(/*(char)22 + "$\r"*/ "#\r") > -1)
                    {
                        // All the data has been read from the
                        // client. Display it on the console.
                        switch (Funciones.Login(Clientes, BD, content, ref Name, ref pasw))
                        {
                        case 0:
                        {
                            Send(handler, ("/*0*/#\r"), false);
                        } break;

                        case 1:
                        {
                            Send(handler, ("/*1*/#\r"), false);
                        } break;

                        case 2:
                        {
                            Clientes.Agregar(Name, state);
                            AList(Clientes.VNombres);
                            Send(handler, ("/*2" + version + "*/#\r"), true);
                            InTR(Name + " se ha unido a la charla");
                            InLC((LUsuarios.Items.Count - 1).ToString());
                            if (BDesactiva.Text == "Activa Chat")
                            {
                                Send(handler, ("$" + (char)18 + "\r"), false);
                                Thread.Sleep(50);
                            }
                            if (PlanTrabajo.Length > 5 || DGNot.Rows.Count > 0)
                            {
                                Send(handler, (PlanTrabajo + (char)7 + Noticias() + (char)11 + "&\r"), false);
                            }
                            state.sb.Clear();
                            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                                 new AsyncCallback(ReadCallback), Clientes.ItemArray(Clientes.Length - 1));
                        } break;

                        case 4:
                        {
                            Send(handler, ("/*4*/#\r"), false);
                        } break;
                        }
                        return;
                    }
                    else
                    {
                        if (content.IndexOf(/*(char)22 + "$\r"*/ "&\r") > -1)
                        {
                            if (BDesactiva.Text == "Desactiva Chat")
                            {
                                InTR(Usu.VNombre + " dice:" + (char)31 + "\n" + content.Substring(0, content.Length - 2));
                                Send(handler, (Usu.VNombre + " dice:" + (char)31 + "\n" + content.Substring(0, content.Length - 2) + "&\r"), true);
                            }
                            else
                            {
                                Send(handler, ("$" + (char)18 + "\r"), false);
                            }
                            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                                 new AsyncCallback(ReadCallback), Usu);
                        }
                        else
                        {
                            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                                 new AsyncCallback(ReadCallback), Usu);
                        }
                    }
                }
                else
                {
                    Usu.VSc.workSocket.Shutdown(SocketShutdown.Both);
                    Usu.VSc.workSocket.Disconnect(true);
                    if (Usu.VNombre != "")
                    {
                        InTR("");
                        Clientes.Eliminar(Usu);
                        AList(Clientes.VNombres);
                        Send(handler, (" &\r"), true);
                        Send(handler, ("/*2" + Clientes.VPNombres + "*/#\r"), true);
                        LCantidad.Text = (LUsuarios.Items.Count - 1).ToString();
                    }
                }
            }
            catch (Exception es)
            {
                try
                {
                    Send(handler, ("/*3*/\r"), false);
                }
                catch (Exception ex)
                {
                    Funciones.Log(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), ex.Message + ex.StackTrace.Substring(ex.StackTrace.Length - 11));
                }
            }
        }