Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        void RegConnectTo(string host, int port, long id)
        {
            SocketClient client = new SocketClient();

            int ReCount = 10;

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

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

            try
            {
                client.Sock.Bind(endpoint); //如果无法绑定那么重新选个端口
            }
            catch
            {
                ReCount--;
                if (ReCount > 0)
                {
                    goto Tp;
                }
                else
                {
                    return;
                }
            }

            if (client.Connect(this.RegHost, this.RegPort)) //连接到注册端口
            {
                client.StartRead();
                client.BinaryInput += (data) =>
                {
                    if (data[0] == 1)
                    {
                        BufferFormat tmp2 = new BufferFormat(-1003);
                        tmp2.AddItem(id);
                        MainClient.Send(tmp2.Finish());
                        client.Close();
                        RunConnToMe(host, port, id);
                    }
                };

                BufferFormat tmpX = new BufferFormat(100);
                tmpX.AddItem(this.Id);
                tmpX.AddItem(BindPort);
                client.Send(tmpX.Finish());
            }
            else  //如果无法绑定此端口 那么换个端口
            {
                if (client.socketError == SocketError.AddressAlreadyInUse)
                {
                    ReCount--;
                    if (ReCount > 0)
                    {
                        client = new SocketClient();
                        goto Tp;
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }