Beispiel #1
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);
            }
        }
Beispiel #2
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);
            }
        }