Exemple #1
0
        void DoExitReceiveData(ClientSocket _client, List <byte> mBytesBuffer)
        {
            ExchangeObject mObject = new ExchangeObject();

            while (mBytesBuffer.Count >= 12)
            {
                if (mObject.Format(mBytesBuffer))
                {
                    switch ((Common.ECommand)mObject.PackType)
                    {
                    case Common.ECommand.Data:
                        Data data = new Data();
                        if (mObject.GetStruct <Data>(ref data))
                        {
                            AddClientReceivePack(_client, data);
                            //_client.SendData(mObject.ToBuffer<Data>(data, Common.ECommand.Data));
                        }
                        else
                        {
                            Console.WriteLine("解析Data对象错误");
                        }
                        break;
                    }
                }
                else
                {
                    mBytesBuffer.RemoveAt(0);
                }
            }
        }
Exemple #2
0
        void client_OnReceiveEvent(object sender, ReceiveDataEventArgs args)
        {
            this.Invoke((MethodInvoker)(() =>
            {
                ExchangeObject mObject = new ExchangeObject();
                while (args.client.mBuffers.Count >= 12)
                {
                    if (mObject.Format(args.client.mBuffers))
                    {
                        switch ((Common.ECommand)mObject.PackType)
                        {
                        case Common.ECommand.Data:
                            Data data = new Data();
                            if (mObject.GetStruct <Data>(ref data))
                            {
                                toolStripStatusLabel1.Text = "收到";
                                textBox3.Text = Encoding.UTF8.GetString(data.Buffer);
                                ObjectCount++;
                                toolStripStatusLabel3.Text = "来自" + client.cSocket.LocalEndPoint.ToString() + "第" + ObjectCount + "个数据[" + data.DataGuid.ToString("N") + "]";
                            }
                            else
                            {
                                Console.WriteLine("解析Data对象错误");
                            }
                            break;

                        case Common.ECommand.SocketInfo:
                            SocketInformation mInfomation = Common.DeserializeSocketInfo(mObject.mData);
                            CreateSocket(mInfomation);
                            break;
                        }
                    }
                    else
                    {
                        args.client.mBuffers.RemoveAt(0);
                    }
                }
            }));
        }
Exemple #3
0
        void server_OnReceiveClientDataEvent(object sender, ReceiveDataEventArgs args)
        {
            ExchangeObject mObject = new ExchangeObject();

            while (args.client.mBuffers.Count >= 12)
            {
                if (mObject.Format(args.client.mBuffers))
                {
                    switch ((Common.ECommand)mObject.PackType)
                    {
                    case Common.ECommand.Data:
                        Data data = new Data();
                        if (mObject.GetStruct <Data>(ref data))
                        {
                            if (data.Buffer != null)
                            {
                                byte[] dat = mObject.ToBuffer <Data>(data, Common.ECommand.Data);
                                //(sender as SocketServer).SendData(args.client.SocketID, dat);//转发回给源客户端
                                long id = DataManager.AddData(args.client.sRemoteIPAndPort, data.Buffer);
                                if (id == 0)
                                {
                                    Logs.Create("记录中转数据到数据库失败!数据来源[" + args.client.sRemoteIPAndPort + "],数据为[" + Encoding.UTF8.GetString(data.Buffer) + "]");
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("解析Data对象错误");
                            Logs.Create("解析Data对象错误");
                            //(sender as SocketServer).CloseClient(args.client.SocketID);
                        }
                        break;

                    case Common.ECommand.Login:
                        int mProcessId = 0;
                        if (mObject.GetStruct <int>(ref mProcessId))
                        {
                            SetClientProcessId(args.client, mProcessId);
                            //int port = Convert.ToInt32(args.client.sRemoteIPAndPort.Split(':')[1]);
                            foreach (ClientSocket f in portMap.Keys)
                            {
                                foreach (ClientSocket t in portMap[f].Clients)
                                {
                                    ExchangeTransfer(f, t);
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("解析ProcessId对象错误");
                            Logs.Create("解析ProcessId对象错误");
                            //(sender as SocketServer).CloseClient(args.client.SocketID);
                        }
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("对象错误" + args.client.mBuffers.Count.ToString());
                    args.client.mBuffers.RemoveAt(0);
                }
            }
        }