Exemple #1
0
        HandleResult OnReceive(uint dwConnID, int iLength)
        {
            // 数据到达了

            // 需要长度
            int required = pkgInfo.Length;

            // 剩余大小
            int remain = iLength;


            while (remain >= required)
            {
                IntPtr bufferPtr = IntPtr.Zero;
                try
                {
                    remain   -= required;
                    bufferPtr = Marshal.AllocHGlobal(required);;
                    if (client.Fetch(dwConnID, bufferPtr, required) == FetchResult.Ok)
                    {
                        if (pkgInfo.IsHeader == true)
                        {
                            PkgHeader header = (PkgHeader)Marshal.PtrToStructure(bufferPtr, typeof(PkgHeader));

                            // 调试信息
                            Console.WriteLine("[Client] head -> Id: {0}, BodySize: {1}\r\n", header.Id, header.BodySize);

                            required = header.BodySize;
                        }
                        else
                        {
                            // 调试信息
                            string recvString = Marshal.PtrToStringAnsi(bufferPtr, required);
                            Console.WriteLine("[Client] body -> text: {0}\r\n", recvString);

                            required = pkgHeaderSize;
                        }

                        AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", dwConnID, pkgInfo.Length));

                        pkgInfo.IsHeader = !pkgInfo.IsHeader;
                        pkgInfo.Length   = required;
                    }
                }
                catch
                {
                    return(HandleResult.Error);
                }
                finally
                {
                    if (bufferPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(bufferPtr);
                        bufferPtr = IntPtr.Zero;
                    }
                }
            }

            return(HandleResult.Ok);
        }
Exemple #2
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            IntPtr bufferPtr = IntPtr.Zero;

            try
            {
                string send = this.txtSend.Text;
                if (send.Length == 0)
                {
                    return;
                }

                // 封包体
                byte[] bodyBytes = Encoding.Default.GetBytes(send);

                // 封包头
                PkgHeader header = new PkgHeader();
                header.Id       = ++id;
                header.BodySize = bodyBytes.Length;
                byte[] headerBytes = StructToBytes(header);


                // 组合最终发送的封包 (封包头+封包体)
                byte[] sendBytes = GetSendBuffer(headerBytes, bodyBytes);

                // 发送
                uint dwConnId = client.GetConnectionId();
                if (client.Send(sendBytes, sendBytes.Length))
                {
                    AddMsg(string.Format("$ ({0}) Send OK --> {1}", dwConnId, send));
                }
                else
                {
                    AddMsg(string.Format("$ ({0}) Send Fail --> {1} ({2})", dwConnId, send, sendBytes.Length));
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                if (bufferPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(bufferPtr);
                }
            }
        }
Exemple #3
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            IntPtr bufferPtr = IntPtr.Zero;
            try
            {
                string send = this.txtSend.Text;
                if (send.Length == 0)
                {
                    return;
                }

                // 封包体
                byte[] bodyBytes = Encoding.Default.GetBytes(send);

                // 封包头
                PkgHeader header = new PkgHeader();
                header.Id = ++id;
                header.BodySize = bodyBytes.Length;
                byte[] headerBytes = StructToBytes(header);

                // 组合最终发送的封包 (封包头+封包体)
                byte[] sendBytes = GetSendBuffer(headerBytes, bodyBytes);

                // 发送
                uint dwConnId = client.GetConnectionId();
                if (client.Send(sendBytes, sendBytes.Length))
                {
                    AddMsg(string.Format("$ ({0}) Send OK --> {1}", dwConnId, send));
                }
                else
                {
                    AddMsg(string.Format("$ ({0}) Send Fail --> {1} ({2})", dwConnId, send, sendBytes.Length));
                }

            }
            catch (Exception)
            {

            }
            finally
            {
                if (bufferPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(bufferPtr);
                }
            }
        }