Exemple #1
0
        HandleResult OnReceive(TcpClient sender, IntPtr pData, int length)
        {
            // 数据到达了
            if (isSendFile == true)
            {
                // 如果发送了文件,并接收到了返回数据
                isSendFile = false;
                MyFileInfo myFile = (MyFileInfo)Marshal.PtrToStructure(pData, typeof(MyFileInfo));
                int objSize = Marshal.SizeOf(myFile);
                // 因为没有附加尾数据,所以大小可以用length - objSize
                byte[] bytes = new byte[length - objSize];
                Marshal.Copy(pData + objSize, bytes, 0, length - objSize);

                string txt = Encoding.Default.GetString(bytes);
                string msg = string.Empty;
                if (txt.Length > 100)
                {
                    msg = txt.Substring(0, 100) + "......";
                }
                else
                {
                    msg = txt;
                }

                AddMsg(string.Format(" > [{0},OnReceive] -> FileInfo(Path:\"{1}\",Size:{2})", sender.ConnectionId, myFile.FilePath, myFile.FileSize));
                AddMsg(string.Format(" > [{0},OnReceive] -> FileContent(\"{1}\")", sender.ConnectionId, msg));
            }
            else if (studentType != StudentType.None)
            {
                byte[] bytes = new byte[length];
                Marshal.Copy(pData, bytes, 0, length);

                switch (studentType)
                {
                    case StudentType.Array:
                        Student[] students = sender.BytesToObject(bytes) as Student[];
                        foreach (var stu in students)
                        {
                            AddMsg(string.Format(" > [{0},OnReceive] -> Student({1},{2},{3})", sender.ConnectionId, stu.Id, stu.Name, stu.GetSexString()));
                        }
                        break;
                    case StudentType.List:
                        List<Student> stuList = sender.BytesToObject(bytes) as List<Student>;
                        foreach (var stu in stuList)
                        {
                            AddMsg(string.Format(" > [{0},OnReceive] -> Student({1},{2},{3})", sender.ConnectionId, stu.Id, stu.Name, stu.GetSexString()));
                        }
                        break;
                    case StudentType.Single:
                        Student student = sender.BytesToObject(bytes) as Student;
                        AddMsg(string.Format(" > [{0},OnReceive] -> Student({1},{2},{3})", sender.ConnectionId, student.Id, student.Name, student.GetSexString()));
                        studentType = StudentType.None;
                        break;
                }
            }
            else
            {
                AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", sender.ConnectionId, length));
            }

            return HandleResult.Ok;
        }
Exemple #2
0
        HandleResult OnReceive(IClient sender, byte[] bytes)
        {
            // 数据到达了
            if (isSendFile == true)
            {
                // 如果发送了文件,并接收到了返回数据
                isSendFile = false;

                MyFileInfo myFile  = client.BytesToStruct <MyFileInfo>(bytes);
                int        objSize = Marshal.SizeOf(myFile);
                // 因为没有附加尾数据,所以大小可以用length - objSize
                byte[] contentBytes = new byte[bytes.Length - objSize];
                Array.ConstrainedCopy(bytes, objSize, contentBytes, 0, contentBytes.Length);

                string txt = Encoding.Default.GetString(contentBytes);
                string msg = string.Empty;
                if (txt.Length > 100)
                {
                    msg = txt.Substring(0, 100) + "......";
                }
                else
                {
                    msg = txt;
                }

                AddMsg(string.Format(" > [{0},OnReceive] -> FileInfo(Path:\"{1}\",Size:{2})", client.ConnectionId, myFile.FilePath, myFile.FileSize));
                AddMsg(string.Format(" > [{0},OnReceive] -> FileContent(\"{1}\")", client.ConnectionId, msg));
            }
            else if (studentType != StudentType.None)
            {
                switch (studentType)
                {
                case StudentType.Array:
                    Student[] students = client.BytesToObject(bytes) as Student[];
                    foreach (var stu in students)
                    {
                        AddMsg(string.Format(" > [{0},OnReceive] -> Student({1},{2},{3})", client.ConnectionId, stu.Id, stu.Name, stu.GetSexString()));
                    }
                    break;

                case StudentType.List:
                    List <Student> stuList = client.BytesToObject(bytes) as List <Student>;
                    foreach (var stu in stuList)
                    {
                        AddMsg(string.Format(" > [{0},OnReceive] -> Student({1},{2},{3})", client.ConnectionId, stu.Id, stu.Name, stu.GetSexString()));
                    }
                    break;

                case StudentType.Single:
                    Student student = client.BytesToObject(bytes) as Student;
                    AddMsg(string.Format(" > [{0},OnReceive] -> Student({1},{2},{3})", client.ConnectionId, student.Id, student.Name, student.GetSexString()));
                    studentType = StudentType.None;
                    break;
                }
            }
            else
            {
                AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", client.ConnectionId, bytes.Length));
            }

            return(HandleResult.Ok);
        }