Beispiel #1
0
        public static string MsgCheck_Winform(Message msg)
        {
            string str;

            str  = "<Header>\n";
            str += $"MSGID : {msg.Header.MSGID}\n";
            str += $"MSGTYPE : {msg.Header.MSGTYPE}\n";
            str += $"BODYLEN : {msg.Header.BODYLEN}\n";
            str += $"FRAGMENTED : {msg.Header.FRAGMENTED}\n";
            str += $"LASTMSG : {msg.Header.LASTMSG}\n";
            str += $"SEQ : {msg.Header.SEQ}\n";


            str += "<Body>\n";
            switch (msg.Header.MSGTYPE)
            {
            case CONSTANTS.REQ_FILE_SEND:
                BodyRequest reqBody = (BodyRequest)msg.Body;
                str += $"FileSize : {reqBody.FILESIZE}\n";
                str += $"FolderSize : {reqBody.FOLDERSIZE}\n";
                str += $"FileName : {System.Text.Encoding.Default.GetString(reqBody.FILENAME)}\n";
                str += $"SaveFolderNameLenght : {System.Text.Encoding.Default.GetString(reqBody.FOLDERNAME)}\n";
                break;

            case CONSTANTS.REP_FILE_SEND:
                BodyResponse resBody = (BodyResponse)msg.Body;
                str += $"MSGID : {resBody.MSGID}\n";
                str += $"Respone : {resBody.RESPONSE}\n";
                break;

            case CONSTANTS.FILE_SEND_DATA:
                break;

            case CONSTANTS.FILE_SEND_RES:
                BodyResult resultBody = (BodyResult)msg.Body;
                str += $"MSGID : {resultBody.MSGID}\n";
                str += $"Result : {resultBody.RESULT}\n";
                break;

            case CONSTANTS.CONNECT_STATE_CHECK:
                BodyCheck checkBody = (BodyCheck)msg.Body;
                str += $"State : {checkBody.STATE}\n";
                break;

            default:
                throw new Exception(String.Format($"Unknown MSGTYPE : {msg.Header.MSGTYPE}"));
            }

            return(str);
        }
Beispiel #2
0
        public static Message Receive(Stream reader)
        {
            int totalRecv  = 0;
            int sizeToRead = 16;

            byte[] hBuffer = new byte[sizeToRead];

            while (sizeToRead > 0)
            {
                byte[] buffer = new byte[sizeToRead];
                int    recv   = reader.Read(buffer, 0, sizeToRead);
                if (recv == 0)
                {
                    return(null);
                }

                buffer.CopyTo(hBuffer, totalRecv);
                totalRecv  += recv;
                sizeToRead -= recv;
            }

            Header header = new Header(hBuffer);

            totalRecv = 0;
            byte[] bBuffer = new byte[header.BODYLEN];
            sizeToRead = (int)header.BODYLEN;

            while (sizeToRead > 0)
            {
                byte[] buffer = new byte[sizeToRead];
                int    recv   = reader.Read(buffer, 0, sizeToRead);
                if (recv == 0)
                {
                    return(null);
                }

                buffer.CopyTo(bBuffer, totalRecv);
                totalRecv  += recv;
                sizeToRead -= recv;
            }

            ISerializable body = null;

            switch (header.MSGTYPE)
            {
            case CONSTANTS.REQ_FILE_SEND:
                body = new BodyRequest(bBuffer);
                break;

            case CONSTANTS.REP_FILE_SEND:
                body = new BodyResponse(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_DATA:
                body = new BodyData(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_RES:
                body = new BodyResult(bBuffer);
                break;

            case CONSTANTS.CONNECT_STATE_CHECK:
                body = new BodyCheck(bBuffer);
                break;

            default:
                throw new Exception(String.Format($"Unknown MSGTYPE : {header.MSGTYPE}"));
            }

            return(new Message()
            {
                Header = header, Body = body
            });
        }
Beispiel #3
0
        public static Message Receive(Stream reader)
        {
            int totalRecv  = 0;
            int sizeToRead = CONSTANTS.MAXSIZE;

            byte[] hBuffer = new byte[sizeToRead];

            while (sizeToRead > 0)
            {
                //버퍼를 할당하고
                byte[] buffer = new byte[sizeToRead];
                //스트림에서 sizeToRead 크기만큼 읽어서 recv에 전달한다.
                int recv = reader.Read(buffer, 0, sizeToRead);

                if (recv == 0)
                {
                    return(null);
                }

                //hBuffer에 buffer의 0번부터 모두 복사 한다.
                buffer.CopyTo(hBuffer, totalRecv);

                //totalRecv에 recv를 추가하고 sizeToRead에서 recv를 뺀다.
                //메시지를 읽었으니 총량을 증가하고 읽은양은 줄어든다.
                totalRecv  += recv;
                sizeToRead -= recv;
            }

            Header header = new Header(hBuffer);

            totalRecv = 0;
            byte[] bBuffer = new byte[header.BODYLEN];
            sizeToRead = (int)header.BODYLEN;

            while (sizeToRead > 0)
            {
                byte[] buffer = new byte[sizeToRead];
                int    recv   = reader.Read(buffer, 0, sizeToRead);

                if (recv == 0)
                {
                    return(null);
                }

                buffer.CopyTo(bBuffer, totalRecv);
                totalRecv  += recv;
                sizeToRead -= recv;
            }

            //Header의 MSGTYPE 프로퍼티를 통해 어떤Body 클래스의 생성자를 호출할지를 결정한다.
            ISerializable body = null;

            switch (header.MSGTYPE)
            {
            case CONSTANTS.REQ_FILE_SEND:
                body = new BodyRequest(bBuffer);
                break;

            case CONSTANTS.REP_FILE_SEND:
                body = new BodyResponse(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_DATA:
                body = new BodyData(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_RES:
                body = new BodyResult(bBuffer);
                break;

            default:
                throw new Exception(String.Format("Unknown MSGTYPE : {0}", header.MSGTYPE));
            }

            return(new Message()
            {
                Header = header, Body = body
            });
        }
Beispiel #4
0
        public static Message Receive(Stream reader)
        {
//             byte[] testBytes = new byte[1024];
//             int recvSize1 = reader.Read(testBytes, 0, 16);
//             int recvSize2 = reader.Read(testBytes, recvSize1, testBytes.Length - recvSize1);

            // Header
            int totalRecv  = 0;
            int sizeToRead = 16;

            byte[] hBuffer = new byte[sizeToRead];

            while (sizeToRead > 0)
            {
                byte[] buffer = new byte[sizeToRead];

                int recv = reader.Read(buffer, 0, sizeToRead);
                if (0 == recv)
                {
                    return(null);
                }

                buffer.CopyTo(hBuffer, totalRecv);
                totalRecv  += recv;
                sizeToRead -= recv;
            }

            Header header = new Header(hBuffer);

            // Body
            totalRecv = 0;
            byte[] bBuffer = new byte[header.BODYLEN];
            sizeToRead = (int)header.BODYLEN;

            while (sizeToRead > 0)
            {
                byte[] buffer = new byte[sizeToRead];

                int recv = reader.Read(buffer, 0, sizeToRead);
                if (0 == recv)
                {
                    return(null);
                }

                buffer.CopyTo(bBuffer, totalRecv);
                totalRecv  += recv;
                sizeToRead -= recv;
            }

            ISerializable body = null;

            switch (header.MSGTYPE)
            {
            case CONSTANTS.REQ_FILE_SEND:
                body = new BodyRequest(bBuffer);
                break;

            case CONSTANTS.REP_FILE_SEND:
                body = new BodyResponse(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_DATA:
                body = new BodyData(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_RES:
                body = new BodyResult(bBuffer);
                break;

            default:
                throw new Exception(String.Format($"Unknown MSGTYPE : {header.MSGTYPE}"));
            }

            return(new Message()
            {
                Header = header,
                Body = body
            });
        }
Beispiel #5
0
        public static Message Receive(Stream reader)
        {
            int totalRecev = 0;
            int sizetoRead = 16;                   //초기화값 16 : 헤더의 사이즈는 16바이트로 설정했으므로

            byte[] hBuffer = new byte[sizetoRead]; //헤더에 대한 버퍼

            //sizetoRead를 사용하는 의미는 무엇이지??
            while (sizetoRead > 0)
            {
                byte[] buffer = new byte[sizetoRead];
                int    recv   = reader.Read(buffer, 0, sizetoRead);
                if (recv == 0)
                {
                    return(null);
                }

                buffer.CopyTo(hBuffer, totalRecev);
                totalRecev += recv;
                sizetoRead -= recv;
            }

            Header header = new Header(hBuffer);

            totalRecev = 0;
            byte[] bBuffer = new byte[header.BODYLEN]; //body의 길이만큼 받는다, 바디에 대한 버퍼
            sizetoRead = (int)header.BODYLEN;

            while (sizetoRead > 0)
            {
                byte[] buffer = new byte[sizetoRead];
                int    recv   = reader.Read(buffer, 0, sizetoRead);
                if (recv == 0)
                {
                    return(null);
                }

                buffer.CopyTo(bBuffer, totalRecev);
                totalRecev += recv;
                sizetoRead -= recv;
            }

            ISerializable body = null;

            //헤더 타입에 따라 바디 객체를 세팅
            switch (header.MSGTYPE)
            {
            case CONSTANTS.REQ_FILE_SEND:
                body = new BodyRequest(bBuffer);
                break;

            case CONSTANTS.REP_FILE_SEND:
                body = new BodyResponse(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_DATA:
                body = new BodyData(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_RES:
                body = new BodyResult(bBuffer);
                break;

            default:
                throw new Exception(
                          String.Format("Unknown MSGTYPE : {0}", header.MSGTYPE));
            }

            return(new Message()
            {
                Header = header, Body = body
            });
        }