Exemple #1
0
        static void Main(string[] args)
        {
            SocketClient client = new SocketClient();

            if (client.Connect("127.0.0.1", 5566))
            {
                client.BinaryInput += client_BinaryInput;
                client.StartRead();
                while (true)
                {
                    Console.ReadLine();


                    //for (int i = 0; i < 100000; i++)
                    //{
                    BufferFormat buffer = new BufferFormat(1000);
                    buffer.AddItem(1.ToString());
                    buffer.AddItem(new byte[64]);
                    byte[] data = buffer.Finish();
                    client.Send(data);
                    System.Threading.Thread.Sleep(1);
                    // }
                }
            }
        }
Exemple #2
0
        static void DataOn(byte[] data, UserInfo userinfo)
        {
            //建立一个读取数据包的类 参数是数据包
            //这个类的功能很强大,可以读取数据包的数据,并可以把你发送过来的对象数据,转换对象引用

            ReadBytes read = new ReadBytes(data);

            int lengt; //数据包长度,用于验证数据包的完整性
            int cmd;   //数据包命令类型

            //注意这里一定要这样子写,这样子可以保证所有你要度的数据是完整的,如果读不出来 Raed方法会返回FALSE,从而避免了错误的数据导致崩溃
            if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd))
            {  //read.Read系列函数是不会产生异常的
                //根据命令读取数据包
                switch (cmd)
                {
                case 1000:
                    string msg = read.ReadString();
                    while (true)
                    {
                        BufferFormat buffer = new BufferFormat(1000);
                        buffer.AddItem(msg.ToString());
                        buffer.AddItem(new byte[8096]);
                        byte[] pdata = buffer.Finish();
                        server.Send(userinfo, pdata);
                    }

                    break;
                }
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            SocketClient client = new SocketClient();

            if (client.ConnectionTo("127.0.0.1", 5566))
            {
                client.DataOn += client_BinaryInput;
                user           = new userinfo(client.sock);
                //int i = 1;
                while (true)
                {
                    Console.ReadLine();
                    stop.Reset();
                    stop.Start();
                    count = 0;
                    for (int i = 0; i < 100000; i++)
                    {
                        BufferFormat buffer = new BufferFormat(1000);
                        buffer.AddItem(i.ToString());
                        buffer.AddItem(new byte[64]);
                        byte[] data = buffer.Finish();
                        client.SendTo(user, data);
                        //System.Threading.Thread.Sleep(1);
                    }
                    //i++;
                }
            }
        }
Exemple #4
0
        void RunQueueList()
        {
            try
            {
                if (UserMaskList.Count > 0) //如果列队数量大于0
                {
Re:
                    string userkey;

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

                        SocketClient client = new SocketClient();                        //建立一个 SOCKET客户端
Pt:
                        IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定当前端口

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

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

                        if (client.Connect(Host, RegIpPort)) //连接注册服务器端口
                        {
                            BufferFormat tmp = new BufferFormat(100);
                            tmp.AddItem(Key);
                            tmp.AddItem(BindPort);
                            client.Send(tmp.Finish());

                            System.Threading.Thread.Sleep(50); //等待 50毫秒

                            BufferFormatV2 tmp2 = new BufferFormatV2((int)PCMD.CONN);
                            tmp2.AddItem(userkey);
                            Mainclient.Send(tmp2.Finish());
                            client.Close();//关闭客户端
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogOut.LogIn(e.ToString(), ActionType.Error);
            }
        }
Exemple #5
0
        static void Main(string[] args)
        {
            client.DataOn += new DataOn(client_DataOn);                               //数据包进入事件

            client.Disconnection += new ExceptionDisconnection(client_Disconnection); //数据包断开事件

            if (client.ConnectionTo("127.0.0.1", 9982))                               //使用同步连接到服务器,一步就用Begin开头的那个
            {
                while (true)
                {
                    Console.ReadLine();



                    testClass.PPo temp = new testClass.PPo();
                    temp.Id      = 1;
                    temp.Message = "通过对象通讯";
                    temp.guid    = new List <Guid>();

                    for (int i = 0; i < 100; i++)
                    {
                        temp.guid.Add(Guid.NewGuid());
                    }
                    client.SendTo(BufferFormat.FormatFCA(temp));  //讲一个PPO对象发送出去


                    // Console.ReadLine();

                    BufferFormat buffmat = new BufferFormat(1001);
                    buffmat.AddItem(2);
                    buffmat.AddItem("通过组合数据包通讯,GUID is object");
                    buffmat.AddItem(Guid.NewGuid());

                    client.SendTo(buffmat.Finish()); //用组合数据包模拟PPO对象

                    // Console.ReadLine();

                    BufferFormat buffmat2 = new BufferFormat(1002);
                    buffmat2.AddItem(3);
                    buffmat2.AddItem("通过组合数据包通讯 all buff");
                    buffmat2.AddItem(Guid.NewGuid().ToString());
                    client.SendTo(buffmat2.Finish()); //用组合数据包模拟PPO对象 但GUID 是字符串类型
                }
            }
            else
            {
                Console.WriteLine("无法连接服务器");
            }

            Console.ReadLine();
        }
Exemple #6
0
        static void Main(string[] args)
        {
            #region DES

            BufferFormat fan = new BufferFormat(1000, new FDataExtraHandle((o) =>
            {
                return(DES.EncryptDES(o, DESkeys, "hello word"));
            }));

            fan.AddItem(true);
            fan.AddItem("abc");
            fan.AddItem(123);

            byte[] data = fan.Finish();


            ReadBytes read = new ReadBytes(data, 4, -1, new RDataExtraHandle((o) =>
            {
                return(DES.DecryptDES(o, DESkeys, "hello word"));
            }));

            int    lengt;
            int    cmd;
            bool   var1;
            string var2;
            int    var3;

            if (read.IsDataExtraSuccess &&
                read.ReadInt32(out lengt) &&
                lengt == read.Length &&
                read.ReadInt32(out cmd) &&
                read.ReadBoolean(out var1) &&
                read.ReadString(out var2) &&
                read.ReadInt32(out var3))
            {
                Console.WriteLine("This DES-> Length:{0} Cmd:{1} var1:{2} var2:{3} var3:{4}", lengt, cmd, var1, var2, var3);
            }
            #endregion

            //AES测试
            AEStest();

            //数据压缩
            Deflatetest();



            Console.ReadLine();
        }
Exemple #7
0
        private void DataOn(byte[] data, SocketAsyncEventArgs socketAsync, ZYNetRingBufferPool stream)
        {
            ReadBytes read = new ReadBytes(data);

            if (read.Length >= 4)
            {
                if (read.ReadInt32(out int lengt) && lengt == read.Length)
                {
                    if (read.ReadByte() == 0xED &&
                        read.ReadByte() == 0xCE &&
                        read.ReadByte() == 0xFE &&
                        read.ReadByte() == 0x10)
                    {
                        long sessionId = read.ReadInt64();

                        if (sessionId == 0)
                        {
                            var token = MakeNewToken(socketAsync, stream, ref sessionId);
                            if (token != null)
                            {
                                BufferFormat session = new BufferFormat(0x10FECEED);
                                session.AddItem(sessionId);
                                Send(token.Sendobj, session.Finish());
                            }
                        }
                        else
                        {
                            if (TokenList.TryGetValue(sessionId, out ASyncToken token))
                            {
                                token.SetSocketEventAsync(socketAsync);
                                socketAsync.UserToken = token;
                                Log.Debug($"ReUse Token {token.SessionKey}");
                            }
                            else
                            {
                                var _token = MakeNewToken(socketAsync, stream, ref sessionId);
                                if (_token != null)
                                {
                                    BufferFormat session = new BufferFormat(0x10FECEED);
                                    session.AddItem(sessionId);
                                    Send(_token.Sendobj, session.Finish());
                                }
                            }
                        }
                    }
                    else
                    {
                        Server.Disconnect(socketAsync.AcceptSocket);
                    }
                }
                else
                {
                    Server.Disconnect(socketAsync.AcceptSocket);
                }
            }
Exemple #8
0
        static void Deflatetest()
        {
            BufferFormat fan = new BufferFormat(1000, new FDataExtraHandle((o) =>
            {
                return(Deflate.Compress(o));
            }));

            fan.AddItem(true);
            fan.AddItem("abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc");
            fan.AddItem(123);

            byte[] data = fan.Finish();


            ReadBytes read = new ReadBytes(data, 4, -1, new RDataExtraHandle((o) =>
            {
                return(Deflate.Decompress(o));
            }));

            int    lengt;
            int    cmd;
            bool   var1;
            string var2;
            int    var3;

            if (read.IsDataExtraSuccess &&
                read.ReadInt32(out lengt) &&
                lengt == read.Length &&
                read.ReadInt32(out cmd) &&
                read.ReadBoolean(out var1) &&
                read.ReadString(out var2) &&
                read.ReadInt32(out var3))
            {
                Console.WriteLine("压缩前长度:{0}", read.Data.Length);
                Console.WriteLine("压缩后长度:{0}", read.Length);
                Console.WriteLine("This Deflate-> Length:{0} Cmd:{1} var1:{2} var2:{3} var3:{4}", lengt, cmd, var1, var2, var3);
            }
        }
Exemple #9
0
        static void AEStest()
        {
            BufferFormat fan = new BufferFormat(1000, new FDataExtraHandle((o) =>
            {
                return(AES.AESEncrypt(o, AESkeys, "hello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello word"));
            }));

            fan.AddItem(true);
            fan.AddItem("abc");
            fan.AddItem(123);

            byte[] data = fan.Finish();


            ReadBytes read = new ReadBytes(data, 4, -1, new RDataExtraHandle((o) =>
            {
                return(AES.AESDecrypt(o, AESkeys, "hello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello wordhello word"));
            }));

            int    lengt;
            int    cmd;
            bool   var1;
            string var2;
            int    var3;

            if (read.IsDataExtraSuccess &&
                read.ReadInt32(out lengt) &&
                lengt == read.Length &&
                read.ReadInt32(out cmd) &&
                read.ReadBoolean(out var1) &&
                read.ReadString(out var2) &&
                read.ReadInt32(out var3))
            {
                Console.WriteLine("This AES-> Length:{0} Cmd:{1} var1:{2} var2:{3} var3:{4}", lengt, cmd, var1, var2, var3);
            }
        }
Exemple #10
0
        private bool CheckSockConnect()
        {
            if (IsClose)
                throw new ObjectDisposedException("ConnectionManager is Close");

            lock (lockObj)
            {
                if (socketClient == null || socketClient.IsConnect == false)
                {
                    socketClient?.Close();
                    socketClient = new SocketClient(SendBufferLenght);

                    if (socketClient.Connect(Host, Port))
                    {

                        RingBuffer.Flush();
                        socketClient.BinaryInput += SocketClient_BinaryInput; 
                        socketClient.Disconnect += SocketClient_Disconnect;                      

                        BufferFormat buffer = new BufferFormat(0x10FECEED);
                        buffer.AddItem(SessionRW.GetSession());
                        byte[] data = buffer.Finish();
                        socketClient.Send(data);
                     

                        return true;

                    }
                    else
                        return false;
                }
                else
                {
                    return true;
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// 数据包处理
        /// </summary>
        /// <param name="data"></param>
        private void BufferIn(byte[] data)
        {
            ReadBytesV2 read = new ReadBytesV2(data);
            int         length;

            if (read.ReadInt32(out length) && length == read.Length)
            {
                int cmd;

                if (read.ReadInt32(out cmd))
                {
                    PCMD pcmd = (PCMD)cmd;


                    switch (pcmd)
                    {
                    case PCMD.SET:     //准备就绪

                        BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLMASK);
                        Mainclient.Send(tmp.Finish());
                        break;

                    case PCMD.ALLUSER:     //获取全部用户列表
                        try
                        {
                            int count;
                            if (read.ReadInt32(out count))
                            {
                                for (int i = 0; i < count; i++)
                                {
                                    string usermask;

                                    if (read.ReadString(out usermask))
                                    {
                                        UserMaskList.Enqueue(usermask);
                                    }
                                }


                                RunQueueList();
                            }
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                        }
                        break;

                    case PCMD.NOWCONN:      //立刻连接到指定IP端口
                        string host;
                        string key;

                        if (read.ReadString(out host) && read.ReadString(out key))
                        {
                            host = host + ":" + key;

                            SocketClient client = new SocketClient();
Tp:
                            IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++);     //绑定端口
                            if (BindPort >= 60000)
                            {
                                BindPort = 1000;
                            }

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

                            if (client.Connect(this.Host, RegIpPort))     //连接到注册端口
                            {
                                BufferFormat tmpX = new BufferFormat(100);
                                tmpX.AddItem(Key);
                                tmpX.AddItem(BindPort);
                                client.Send(tmpX.Finish());

                                System.Threading.Thread.Sleep(50);


                                BufferFormatV2 tmpX2 = new BufferFormatV2((int)PCMD.LEFTCONN);
                                tmpX2.AddItem(key);
                                Mainclient.Send(tmpX2.Finish());

                                client.Close();

                                System.Threading.Thread.Sleep(50);

                                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host);
                            }
                        }
                        break;

                    case PCMD.LEFTCONN:
                        string host2;
                        string key2;
                        if (read.ReadString(out host2) && read.ReadString(out key2))
                        {
                            host2 = host2 + ":" + key2;
                            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host2);
                        }
                        break;

                    case PCMD.GETALLUSER:
                    {
                        int count;

                        if (read.ReadInt32(out count))
                        {
                            AllUser = new List <string>();

                            for (int i = 0; i < count; i++)
                            {
                                string var;
                                if (read.ReadString(out var))
                                {
                                    AllUser.Add(var);
                                }
                                else
                                {
                                    break;
                                }
                            }

                            if (GetAllUserList != null)
                            {
                                GetAllUserList(AllUser);
                            }
                        }
                    }
                    break;

                    case PCMD.ProxyData:
                    {
                        string keys;
                        byte[] buff;

                        if (read.ReadString(out keys) && read.ReadByteArray(out buff))
                        {
                            if (ProxyList.ContainsKey(keys))
                            {
                                client_DataOutPut(keys, ProxyList[keys], buff);
                            }
                            else
                            {
                                ConClient client = new ConClient(keys);

                                if (ProxyList.TryAdd(client.Key, client))
                                {
                                    client_DataOutPut(keys, client, buff);
                                }
                            }
                        }
                    }
                    break;
                    }
                }
            }
        }
Exemple #12
0
        static void DataOn(byte[] data, SocketAsyncEventArgs e)
        {
            Console.WriteLine("ThreadId:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
            try
            {
                //建立一个读取数据包的类 参数是数据包
                //这个类的功能很强大,可以读取数据包的数据,并可以把你发送过来的对象数据,转换对象引用

                ReadBytes read = new ReadBytes(data);

                int lengt; //数据包长度,用于验证数据包的完整性
                int cmd;   //数据包命令类型

                //注意这里一定要这样子写,这样子可以保证所有你要度的数据是完整的,如果读不出来 Raed方法会返回FALSE,从而避免了错误的数据导致崩溃
                if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd))
                {  //read.Read系列函数是不会产生异常的
                    //根据命令读取数据包
                    switch (cmd)
                    {
                    case 1000:
                    {
                        int    version = read.ReadInt32();
                        long   data1   = read.ReadInt64();
                        float  data2   = read.ReadFloat();
                        double db1     = read.ReadDouble();
                        bool   b1      = read.ReadBoolean();
                        bool   b2      = read.ReadBoolean();
                        short  s1      = read.ReadInt16();
                        byte   sb1     = read.ReadByte();
                        string str1    = read.ReadString();
                        string str2    = read.ReadString();
                        byte[] datax   = read.ReadByteArray();

                        TestData tm2 = read.ReadObject <TestData>();

                        Console.WriteLine("int:" + version);
                        Console.WriteLine("long:" + data1);
                        Console.WriteLine("float:" + data2);
                        Console.WriteLine("double:" + db1);
                        Console.WriteLine("bool TRUE:" + b1);
                        Console.WriteLine("bool False:" + b2);
                        Console.WriteLine("short:" + s1);
                        Console.WriteLine("byte:" + sb1);
                        Console.WriteLine("string:" + str1);
                        Console.WriteLine("string:" + str2);
                        Console.WriteLine("bytes lengt:" + datax.Length);

                        BufferFormat buffer = new BufferFormat(1000);
                        buffer.AddItem(version);
                        buffer.AddItem(data1);
                        buffer.AddItem(data2);
                        buffer.AddItem(db1);
                        buffer.AddItem(b1);
                        buffer.AddItem(b2);
                        buffer.AddItem(s1);
                        buffer.AddItem(sb1);
                        buffer.AddItem(str1);
                        buffer.AddItem(str2);
                        buffer.AddItem(datax);


                        TestData tmx = new TestData()
                        {
                            Id   = 1,
                            Data = new List <string>()
                            {
                                "123123", "32123123"
                            },
                            Data2 = new List <Test2>()
                            {
                                new Test2 {
                                    A = 1, B = 2
                                }, new Test2 {
                                    A = 3, B = 4
                                }
                            }
                        };

                        buffer.AddItem(tmx);

                        server.SendData(e.AcceptSocket, buffer.Finish());
                    }
                    break;
                    }
                }
            }
            catch (Exception er)
            {
                Console.WriteLine(er.ToString());
            }
        }