示例#1
0
        HandleResult OnPointerDataReceive(IServer sender, IntPtr connId, IntPtr pData, int length)
        {
            // 数据到达了
            try
            {
                // 可以通过下面的方法转换到byte[]
                // byte[] bytes = new byte[length];
                // Marshal.Copy(pData, bytes, 0, length);


                // 获取附加数据
                ClientInfo clientInfo = extra.Get(connId);
                if (clientInfo != null)
                {
                    // clientInfo 就是accept里传入的附加数据了
                    AddMsg(string.Format(" > [{0},OnReceive] -> {1}:{2} ({3} bytes)", clientInfo.ConnId, clientInfo.IpAddress, clientInfo.Port, length));
                }
                else
                {
                    AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", connId, length));
                }

                if (server.Send(connId, pData, length))
                {
                    return(HandleResult.Ok);
                }

                return(HandleResult.Error);
            }
            catch (Exception)
            {
                return(HandleResult.Ignore);
            }
        }
示例#2
0
        HandleResult OnReceive(IntPtr connId, byte[] bytes)
        {
            // 数据到达了
            try
            {
                // 获取附加数据
                IntPtr clientPtr = IntPtr.Zero;
                if (server.GetConnectionExtra(connId, ref clientPtr))
                {
                    // ci 就是accept里传入的附加数据了
                    ClientInfo ci = (ClientInfo)Marshal.PtrToStructure(clientPtr, typeof(ClientInfo));
                    AddMsg(string.Format(" > [{0},OnReceive] -> {1}:{2} ({3} bytes)", ci.ConnId, ci.IpAddress, ci.Port, bytes.Length));
                }
                else
                {
                    AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", connId, bytes.Length));
                }

                if (server.Send(connId, bytes, bytes.Length))
                {
                    return(HandleResult.Ok);
                }

                return(HandleResult.Error);
            }
            catch (Exception)
            {
                return(HandleResult.Ignore);
            }
        }
示例#3
0
        private void buttonSend_Click(object sender, EventArgs e)
        {
            string sendContent = textBoxSendMsg.Text;

            if (sendContent.Length < 1)
            {
                return;
            }
            if (checkedListBoxClientList.Items.Count < 1)
            {
                return;
            }

            try
            {
                // byte[] sendBytes = Encoding.Default.GetBytes(sendContent);
                byte[] sendBytes = Encoding.GetEncoding("UTF-8").GetBytes(sendContent);
                for (int i = 0; i < checkedListBoxClientList.Items.Count; i++)
                {
                    IntPtr connId = (IntPtr)Convert.ToInt32(checkedListBoxClientList.Items[i]);
                    if (checkedListBoxClientList.GetItemChecked(i))
                    {
                        server.Send(connId, sendBytes, sendBytes.Length);
                    }
                }

                textBoxSendMsg.Text = string.Empty;
            }
            catch (Exception exc)
            {
                ShowMSG(string.Format("发送失败:{0}", exc.Message));
            }
        }
示例#4
0
        HandleResult OnReceive(IntPtr connId, byte[] bytes)
        {
            // 数据到达了

            Interlocked.Add(ref totalReceived, bytes.Length);

            if (server.Send(connId, bytes, bytes.Length))
            {
                return(HandleResult.Ok);
            }

            return(HandleResult.Error);
        }
示例#5
0
        HandleResult OnPointerDataReceive(IntPtr connId, IntPtr pData, int length)
        {
            // 数据到达了
            try
            {
                // 可以通过下面的方法转换到byte[]
                byte[] bytes = new byte[length];
                Marshal.Copy(pData, bytes, 0, length);
                AddMsg(string.Format(" > [获取的数据] -> {0}", Encoding.Default.GetString(bytes)));

                #region -----获取附加数据-----

                /*
                 * ClientInfo clientInfo = extra.Get(connId);
                 * if (clientInfo != null)
                 * {
                 *  // clientInfo 就是accept里传入的附加数据了
                 *  AddMsg(string.Format(" > [{0},OnPointerDataReceive] -> {1}:{2} ({3} bytes)", clientInfo.ConnId, clientInfo.IpAddress, clientInfo.Port, length));
                 * }
                 * else
                 * {
                 *  AddMsg(string.Format(" > [{0},OnPointerDataReceive] -> ({1} bytes)", connId, length));
                 * }
                 */
                #endregion

                //服务器向客户端发送返回数据
                if (server.Send(connId, pData, length))
                {
                    return(HandleResult.Ok);
                }

                return(HandleResult.Error);
            }
            catch (Exception)
            {
                return(HandleResult.Ignore);
            }
        }
示例#6
0
        /// <summary>
        /// 发送指令
        /// 此处得到的发送指令,一定是可用的且不为End类型的
        /// </summary>
        private void sendPollingCMD()
        {
            BaseCmd baseCmd = ReadCmd();

            if (baseCmd != null)
            {
                currentCmd = baseCmd;
                byte[] tempCmd = baseCmd.GetSendCommand();
                if (server.Send(connId, tempCmd, tempCmd.Length))
                {
                    sendTime = DateTime.Now;
                }
            }
        }
示例#7
0
        HandleResult OnReceive(uint dwConnID, IntPtr pData, int iLength)
        {
            // 数据到达了

            Interlocked.Add(ref totalReceived, iLength);

            if (server.Send(dwConnID, pData, iLength))
            {
                return(HandleResult.Ok);
            }

            return(HandleResult.Error);

            /*try
             * {*/
            /*
             * // 从pData中获取字符串
             * // string str = Marshal.PtrToStringAnsi(pData, iLength);
             *
             * // intptr转byte[]
             * // byte[] bytes = new byte[iLength];
             * // Marshal.Copy(pData, bytes, 0, iLength);
             *
             *
             * // 获取附加数据
             * IntPtr clientPtr = IntPtr.Zero;
             * if (server.GetConnectionExtra(dwConnID, ref clientPtr))
             * {
             *  // ci 就是accept里传入的附加数据了
             *  ClientInfo ci = (ClientInfo)Marshal.PtrToStructure(clientPtr, typeof(ClientInfo));
             *  AddMsg(string.Format(" > [{0},OnReceive] -> {1}:{2} ({3} bytes)", ci.ConnId, ci.IpAddress, ci.Port, iLength));
             * }
             * else
             * {
             *  AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", dwConnID, iLength));
             * }
             *
             * if (server.Send(dwConnID, pData, iLength))
             * {
             *  return HandleResult.Ok;
             * }
             *
             * return HandleResult.Error;*/
            /*}
             * catch (Exception)
             * {
             *  return HandleResult.IGNORE;
             * }*/
        }
示例#8
0
        HandleResult OnReceive(IntPtr connId, IntPtr pData, int length)
        {
            // 数据到达了
            try
            {
                // 从pData中获取字符串
                // string str = Marshal.PtrToStringAnsi(pData, length);

                // intptr转byte[]
                // byte[] bytes = new byte[length];
                // Marshal.Copy(pData, bytes, 0, length);


                // 获取附加数据
                IntPtr clientPtr = IntPtr.Zero;
                if (server.GetConnectionExtra(connId, ref clientPtr))
                {
                    // ci 就是accept里传入的附加数据了
                    ClientInfo ci = (ClientInfo)Marshal.PtrToStructure(clientPtr, typeof(ClientInfo));
                    AddMsg(string.Format(" > [{0},OnReceive] -> {1}:{2} ({3} bytes)", ci.ConnId, ci.IpAddress, ci.Port, length));
                }
                else
                {
                    AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", connId, length));
                }

                if (server.Send(connId, pData, length))
                {
                    return(HandleResult.Ok);
                }

                return(HandleResult.Error);
            }
            catch (Exception)
            {
                return(HandleResult.Ignore);
            }
        }
示例#9
0
        /// <summary>
        /// 处理单个数据
        /// 这个数据可以使任何的,比如登陆的,心跳的 ,还是正常的数据包都行
        /// </summary>
        /// <param name="data">要求为 ClientInfo</param>
        public void ProcessSingleData(object data)
        {
            ClientInfo clientInfo = data as ClientInfo;

            HPSocketCS.TcpServer server = clientInfo.server;
            byte[] bytes  = clientInfo.Data;
            IntPtr connId = clientInfo.ConnId;

            if (clientInfo != null)
            {
                if (bytes.Length == 14 && bytes[0] == 170)
                {
                    //14位长,并且第一位是170,表示是登陆指令,解析登陆id
                    String logid = BaseDTU.ParseLogin(bytes);
                    if (logid != "")
                    {
                        try
                        {
                            //合法的id,首先发送心跳包
                            if (server.Send(clientInfo.ConnId, heartbeat, heartbeat.Length))
                            {
                                //发送心跳包成功,根据过滤的设置显示发送成功的消息
                                if (clientInfo.ConnId.ToInt32() % showFilterNumber == 0)
                                {
                                    AddMsg(string.Format(" 发送了心跳包  {0}", clientInfo.ConnId));
                                }
                                //判断此server的dtu字典中是否存在这个logid
                                if (clientInfo.serverManager.dtuDic.ContainsKey(logid))
                                {
                                    //已存在key,说明已经初始化了
                                    try
                                    {
                                        //心跳完成,准备轮训
                                        clientInfo.serverManager.dtuDic[logid].server = server;
                                        clientInfo.serverManager.dtuDic[logid].connId = connId;
                                        basetimermgr.AddDTU(clientInfo.serverManager.dtuDic[logid]);
                                        if (clientInfo.ConnId.ToInt32() % showFilterNumber == 0)
                                        {
                                            AddMsg(string.Format(clientInfo.serverManager.desc + "    > [客户端] -> {0}开始轮询", (int)connId));
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        MessageBox.Show(e.Message);
                                    }
                                }
                                else
                                {
                                    //不存在key 未被初始化
                                    if (server.Disconnect(connId, true))
                                    {
                                        //正常断开非法登录客户端
                                    }
                                    AddMsg(string.Format(" > [客户端] -> {0}断开 id : {1}", (int)connId, logid));
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("发送出错" + e.Message);
                        }
                    }
                    else
                    {
                        //非法id,断开
                        if (server.Disconnect(connId, true))
                        {
                            //正常断开非法登录客户端
                        }
                    }
                }
                else
                {
                    //不是登录包
                    if (bytes == heartbeat)
                    {
                        //收到的是心跳包
                    }
                    else
                    {
                        try
                        {
                            //收到的是正常任务包
                            BaseDTU basedtu = clientInfo.serverManager.dtuDic.Values.Where(item => item.connId == connId).FirstOrDefault();
                            if (basedtu != null)
                            {
                                //把正常数据包丢给dtu处理
                                basedtu.ReceivedCmd(bytes);
                            }
                        }
                        catch (Exception e)
                        {
                            server.Disconnect(connId);
                        }
                    }
                }
            }
            else
            {
                AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", connId, bytes.Length));
            }
        }