Beispiel #1
0
        private void ListenerServer()
        {
            var bytes = new byte[1024];

            while (true)
            {
                if (socket.Available == 0)
                {
                    Thread.Sleep(100);

                    continue;
                }

                socket.Receive(bytes, 0, 3, SocketFlags.None);

                var actionId = bytes[0];

                var length = (short)(bytes[1] << 8 | bytes[2]);

                using (var stream = new MemoryStream())
                {
                    stream.Write(bytes, 0, 3);

                    for (var i = 3; i < length; i += bytes.Length)
                    {
                        var readLength = length - i > bytes.Length ? bytes.Length : length - i;

                        var len = 0;

                        while (len != readLength)
                        {
                            len += socket.Receive(bytes, len, readLength - len, SocketFlags.None);
                        }

                        stream.Write(bytes, 0, readLength);
                    }

                    var streamBytes = stream.ToArray();

                    if (actionId == 0x01)
                    {
                        var waitCount = new WaitMessagePackage().DeSerialize(streamBytes).WaitCount;

                        ReferenceServerStatus(waitCount);

                        ReferenceRank();

                        continue;
                    }

                    if (actionId == 0x02)
                    {
                        var model = new CheckCodePackage().DeSerialize(streamBytes);

                        GetCheckCode(new ZhaopinCheckCode
                        {
                            Id      = model.CheckCodeId,
                            Account = Encoding.UTF8.GetString(model.Account),
                            Status  = model.Status,
                            Cookie  = Encoding.UTF8.GetString(model.Cookie),
                            Type    = model.Type
                        });

                        continue;
                    }

                    if (actionId == 0x06)
                    {
                        this.RunInMainthread(() =>
                        {
                            this.lbl_Tip.Text = $"提示:{DateTime.Now:HH:mm:ss} 没有验证码待输入!";

                            this.lbl_WaitCount.Text = "待处理:0";

                            isGetCheckCode = false;
                        });

                        continue;
                    }

                    if (actionId == 0x0c)
                    {
                        var model = new LoginCheckResultPackage().DeSerialize(streamBytes);

                        isChecked = true;

                        if (model.Status == 1)
                        {
                            this.RunInMainthread(() =>
                            {
                                this.lbl_Tip.Text = $"提示:{DateTime.Now:HH:mm:ss} 验证成功!";

                                endTime = null;

                                this.btn_GetCheckCode.Enabled = true;

                                this.btn_ReferenceCheckCode.Enabled = false;

                                this.btn_Checking.Enabled = false;

                                this.pic_Body.Image = null;

                                this.pic_Header.Image = null;
                            });
                        }
                        else
                        {
                            endTime = DateTime.Now.AddMinutes(1);

                            var dataResult = ReferenceCheckCode();

                            if (!dataResult.IsSuccess)
                            {
                                endTime = null;
                            }
                        }
                    }
                }
            }
        }
        private void StartSocketServer()
        {
            var tcpListener = new TcpListener(IPAddress.Any, 12580);

            tcpListener.Start();

            this.RunInMainthread(() =>
            {
                this.btn_Start.Text = "已启动";

                this.btn_Start.Enabled = false;

                this.tir_Refrence.Start();
            });

            while (true)
            {
                if (!tcpListener.Pending())
                {
                    Thread.Sleep(100);

                    continue;
                }

                Task.Run(() =>
                {
                    var account = string.Empty;

                    using (var socket = tcpListener.AcceptSocket())
                    {
                        var lastTime = DateTime.Now.AddMinutes(1);

                        var bytes = new byte[1024];

                        while (true)
                        {
                            if (lastTime < DateTime.Now)
                            {
                                if (string.IsNullOrEmpty(cleaningDictionary.FirstOrDefault(f => f.Value == socket).Key))
                                {
                                    break;
                                }

                                LogFactory.Debug($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 关闭连接 程序ID=> {cleaningDictionary.FirstOrDefault(f => f.Value == socket).Key} account:{account} lastTime =>{lastTime:yyyy-MM-dd HH:mm:ss}");

                                Socket tempSockte;

                                if (cleaningDictionary.TryRemove(account, out tempSockte))
                                {
                                    business.CleaningEnd(account);
                                }

                                break;
                            }

                            if (socket.Available == 0)
                            {
                                Thread.Sleep(100);

                                continue;
                            }

                            lastTime = DateTime.Now.AddMinutes(1);

                            socket.Receive(bytes, 0, 3, SocketFlags.None);

                            var actionId = bytes[0];

                            if (cleaningDictionary.Any(f => f.Value == socket))
                            {
                                LogFactory.Debug($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 收到心跳 程序ID=> {cleaningDictionary.FirstOrDefault(f => f.Value == socket).Key} lastTime =>{lastTime:yyyy-MM-dd HH:mm:ss}, Dic=>{JsonConvert.SerializeObject(cleaningDictionary.Select(s=>s.Key))}");
                            }



                            var length = (short)(bytes[1] << 8 | bytes[2]);

                            using (var stream = new MemoryStream())
                            {
                                stream.Write(bytes, 0, 3);

                                for (var i = 3; i < length; i += bytes.Length)
                                {
                                    var readLength = length - i > bytes.Length ? bytes.Length : length - i;

                                    var len = 0;

                                    while (len != readLength)
                                    {
                                        len += socket.Receive(bytes, len, readLength - len, SocketFlags.None);
                                    }

                                    stream.Write(bytes, 0, readLength);
                                }

                                var streamBytes = stream.ToArray();

                                if (actionId == 0x01)
                                {
                                    var waitCount = business.GetWaitCount();

                                    socket.Send(new WaitMessagePackage
                                    {
                                        Id        = 0x01,
                                        Length    = 7,
                                        WaitCount = waitCount
                                    }.Serialize());

                                    continue;
                                }

                                if (actionId == 0x02)
                                {
                                    var checeCodeModel = business.GetCheckCode();

                                    if (checeCodeModel == null)
                                    {
                                        socket.Send(new Package {
                                            Id = 0x06, Length = 3
                                        }.Serialize());

                                        continue;
                                    }

                                    var accountBytes = Encoding.UTF8.GetBytes(checeCodeModel.Account);

                                    var cookieBytes = Encoding.UTF8.GetBytes(checeCodeModel.Cookie);

                                    socket.Send(new CheckCodePackage
                                    {
                                        Id          = 0x02,
                                        Length      = (short)(cookieBytes.Length + accountBytes.Length + 15),
                                        CheckCodeId = checeCodeModel.Id,
                                        Cookie      = cookieBytes,
                                        Status      = checeCodeModel.Status,
                                        Type        = checeCodeModel.Type,
                                        Account     = accountBytes
                                    }.Serialize());

                                    clientDictionary.AddOrUpdate(checeCodeModel.Id, k => socket, (k, v) => socket);

                                    continue;
                                }

                                if (actionId == 0x03)
                                {
                                    var package = new CheckedResultPackage().DeSerialize(streamBytes);

                                    var accounts = business.CheckResult(package.CheckCodeId, package.Status, Encoding.UTF8.GetString(package.Account), Encoding.UTF8.GetString(package.HandleUser));

                                    if (package.Status == 1)
                                    {
                                        foreach (var item in accounts)
                                        {
                                            Socket socketClient;

                                            if (!cleaningDictionary.TryGetValue(item, out socketClient))
                                            {
                                                continue;
                                            }

                                            socketClient.Send(streamBytes);
                                        }
                                    }

                                    continue;
                                }


                                if (actionId == 0x04)
                                {
                                    var package = new CleaningProcedurePackage().DeSerialize(streamBytes);

                                    account = Encoding.UTF8.GetString(package.Account);

                                    business.CleaningStart(new ZhaopinCleaningProcedure
                                    {
                                        Id       = package.CleaningId,
                                        Account  = Encoding.UTF8.GetString(package.Account),
                                        Password = Encoding.UTF8.GetString(package.Password)
                                    });

                                    cleaningDictionary.AddOrUpdate(Encoding.UTF8.GetString(package.Account), k => socket, (k, v) => socket);

                                    continue;
                                }

                                if (actionId == 0x05)
                                {
                                    var package = new CheckCodePackage().DeSerialize(streamBytes);

                                    business.InserCheckCode(new ZhaopinCheckCode
                                    {
                                        Account    = Encoding.UTF8.GetString(package.Account),
                                        Cookie     = Encoding.UTF8.GetString(package.Cookie),
                                        CreateTime = DateTime.Now,
                                        Status     = 0,
                                        Type       = package.Type
                                    });
                                }
                                if (actionId == 0x09)
                                {
                                    var package = new CheckCoordinatPackage().DeSerialize(streamBytes);

                                    Socket socketClient;

                                    if (!cleaningDictionary.TryGetValue(Encoding.UTF8.GetString(package.Account), out socketClient))
                                    {
                                        continue;
                                    }

                                    socketClient.Send(streamBytes);
                                }
                                if (actionId == 0x0a)
                                {
                                    var package = new ReferenceCheckCodePackage().DeSerialize(streamBytes);

                                    Socket socketClient;

                                    if (!cleaningDictionary.TryGetValue(Encoding.UTF8.GetString(package.Account), out socketClient))
                                    {
                                        continue;
                                    }

                                    socketClient.Send(streamBytes);
                                }

                                //if (actionId == 0x0c)
                                //{
                                //    var package = new LoginCheckResultPackage().DeSerialize(streamBytes);

                                //    business.LoginCheckResult(package.CheckCodeId, package.Status, package.CleaningId);

                                //    Socket socketClient;

                                //    if (!clientDictionary.TryGetValue(package.CheckCodeId, out socketClient)) continue;

                                //    socketClient.Send(streamBytes);

                                //    if (package.Status == 1)
                                //    {
                                //        clientDictionary.TryRemove(package.CheckCodeId, out socketClient);
                                //    }
                                //}
                            }
                        }
                    }
                });
            }
        }