Exemple #1
0
        void RunQueueList()
        {
            try
            {
                if (UserMaskList.Count > 0) //如果列队数量大于0
                {
Re:
                    long userkey;

                    if (UserMaskList.TryDequeue(out userkey)) //挤出一个用户ID
                    {
                        if (userkey == this.Id)
                        {
                            goto Re;
                        }

                        SocketClient client = new SocketClient(); //建立一个 SOCKET客户端

                        int Re = 10;

Pt:
                        IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定当前端口

                        if (BindPort >= 60000)
                        {
                            BindPort = 1000;
                        }

                        try
                        {
                            client.Sock.Bind(endpoint); //如果无法绑定此端口 那么换个端口
                        }
                        catch
                        {
                            Re--;

                            if (Re > 0)
                            {
                                goto Pt;
                            }
                            else
                            {
                                return;
                            }
                        }

                        if (client.Connect(RegHost, RegPort)) //连接注册服务器端口
                        {
                            client.StartRead();

                            client.BinaryInput += (data) =>
                            {
                                if (data[0] == 1)
                                {
                                    BufferFormat tmp2 = new BufferFormat(-1002);
                                    tmp2.AddItem(userkey);
                                    MainClient.Send(tmp2.Finish());
                                    client.Close();//关闭客户端
                                }
                            };

                            BufferFormat tmp = new BufferFormat(100);
                            tmp.AddItem(Id);
                            tmp.AddItem(BindPort);
                            client.Send(tmp.Finish());
                        }
                        else//如果无法绑定此端口 那么换个端口
                        {
                            if (client.socketError == SocketError.AddressAlreadyInUse)
                            {
                                client = new SocketClient();

                                Re--;

                                if (Re > 0)
                                {
                                    client = new SocketClient();
                                    goto Pt;
                                }
                                else
                                {
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LLOG(e.ToString(), ActionType.Error);
            }
        }
Exemple #2
0
        /// <summary>
        /// 数据包处理
        /// </summary>
        /// <param name="data"></param>
        private void BufferIn(byte[] data)
        {
            ReadBytes read = new ReadBytes(data);

            int length;
            int cmd;

            if (read.ReadInt32(out length) && length == read.Length && read.ReadInt32(out cmd))
            {
                switch (cmd)
                {
                case -1000:
                {
                    RegSession session;

                    if (read.ReadObject <RegSession>(out session))
                    {
                        if (session.IsSuccess)
                        {
                            this.Id      = session.Id;
                            this.RegPort = session.Port;
                            LLOG("MY ID:" + this.Id, ActionType.Message);
                            GetAllUserSession();
                        }
                        else
                        {
                            LLOG(session.Msg, ActionType.Error);
                            MainClient.Close();
                        }
                    }
                }
                break;

                case -1001:
                {
                    GetAllSession allsession;

                    if (read.ReadObject <GetAllSession>(out allsession))
                    {
                        if (allsession.IsSuccess && allsession.UserIds != null)
                        {
                            CheckDroupConnect(allsession);

                            foreach (var Id in allsession.UserIds)
                            {
                                if (!ConnUserList.ContainsKey(Id))
                                {
                                    SessionObj tmp = new SessionObj()
                                    {
                                        Id = Id
                                    };

                                    ConnUserList.AddOrUpdate(Id, tmp, (a, b) => tmp);

                                    UserMaskList.Enqueue(Id);
                                }
                            }

                            RunQueueList();
                        }
                    }
                }
                break;

                case -1002:
                {
                    ConnectTo connto;

                    if (read.ReadObject <ConnectTo>(out connto))
                    {
                        if (connto.IsSuccess)
                        {
                            RegConnectTo(connto.Host, connto.Port, connto.Id);
                        }
                    }
                }
                break;

                case -1003:
                {
                    LEFTConnect leftconnto;

                    if (read.ReadObject <LEFTConnect>(out leftconnto))
                    {
                        if (leftconnto.IsSuccess)
                        {
                            RunConnToMe(leftconnto.Host, leftconnto.Port, leftconnto.Id);
                        }
                    }
                }
                break;

                case -1004:
                {
                    AddSession addsession;

                    if (read.ReadObject <AddSession>(out addsession))
                    {
                        if (addsession.Id != this.Id)
                        {
                            if (!ConnUserList.ContainsKey(addsession.Id))
                            {
                                SessionObj tmp = new SessionObj()
                                {
                                    Id = addsession.Id
                                };

                                ConnUserList.AddOrUpdate(addsession.Id, tmp, (a, b) => tmp);

                                LLOG("Add Session:" + addsession.Id, ActionType.Message);
                            }
                        }
                    }
                }
                break;

                case -1005:
                {
                    RemoveSession remove;
                    if (read.ReadObject <RemoveSession>(out remove))
                    {
                        if (ConnUserList.ContainsKey(remove.Id))
                        {
                            DropConnUser(remove.Id);

                            LLOG("Remove Session:" + remove.Id, ActionType.Message);
                        }
                    }
                }
                break;

                case -2000:
                {
                    ProxyData proxydata;

                    if (read.ReadObject <ProxyData>(out proxydata))
                    {
                        if (DataInput != null)
                        {
                            DataInput(proxydata.Source, proxydata.Data);
                        }
                    }
                }
                break;
                }
            }
        }