첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

WCF 송/수신기 (C#/NETWORK)

 

WCF 기술을 사용해 Point-to-Point 방식으로 데이터를 송수신할 수 있는 기능을 클래스 라이브러리로 만들었습니다.

TCP와 HTTP 프로토콜을 지원합니다.



사용법

 

 

Server

 

 

using System;

using System.Threading;

 

using DSCORE.NETWORK.MESSAGE;

using DSCORE.NETWORK.MESSAGE.WCF;

 

namespace TEST.NETWORK.MESSAGE.WCF.SERVER

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 로그 헬퍼

        /// </summary>

        private static ILogHelper _logHelper = new ConsoleLogHelper();

 

        /// <summary>

        /// 송신기

        /// </summary>

        private static ISender _sender = null;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 수신기 메시지 수신시 처리하기 - receiver_MessageReceived(sender, e)

 

        /// <summary>

        /// 수신기 메시지 수신시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private static void receiver_MessageReceived(object sender, MessageReceivedEventArgs e)

        {

            try

            {

                WCFReceiver receiver = sender as WCFReceiver;

 

                if(e.Message.SendType == SendType.ASYNC)

                {

                    try

                    {

                        if(e.Message is MessageRequest)

                        {

                            #region 메시지 요청을 처리한다.

 

                            MessageRequest request = e.Message as MessageRequest;

 

                            //////////////////////////////////////////////////

                            _logHelper.WriteLog("ASYNC MESSAGE REQUEST RECEIVED : {0}", request.Content);

 

                            string resultContent = null;

 

                            for(int i = 1; i <= 10; i++)

                            {

                                _logHelper.WriteLog("ASYNC MESSAGE REQUEST {0} PROCESSED {1} JOB...", request.Content, i);

 

                                Thread.Sleep(1000);

 

                                if(request.Canceled)

                                {

                                    _logHelper.WriteLog("ASYNC MESSAGE REQUEST {0} CANCELD...", request.Content);

 

                                    break;

                                }

                            }

 

                            resultContent = request.Content;

                            //////////////////////////////////////////////////

 

                            if(!request.Canceled)

                            {

                                MessagePoint remotePoint = new MessagePoint(receiver.LocalPoint.TransportMode,

                                    request.SendAddress, request.SendPort);

 

                                MessageResult result = new MessageResult(request);

 

                                result.Content = resultContent;

 

                                try

                                {

                                    _sender.SendMessageResultAsync(remotePoint, result);

 

                                    _logHelper.WriteLog("ASYNC MESSAGE RESULT SENT : {0}", request.Content);

                                }

                                catch(Exception exception)

                                {

                                    _logHelper.WriteLog("ERROR OCCURRED WHEN SENDING ASYNC MESSAGE RESULT : {0}", request.Content);

 

                                    Console.WriteLine("--------------------------------------------------");

                                    Console.WriteLine(exception.Message);

                                    Console.WriteLine("--------------------------------------------------");

                                }

                            }

 

                            #endregion

                        }

                    }

                    finally

                    {

                        receiver.ReceiveMessageDictionary.RemoveMessage(e.Message.ID);

                    }

                }

                else if(e.Message.SendType == SendType.SYNC)

                {

                    if(e.Message is MessageRequest)

                    {

                        #region 메시지 요청을 처리한다.

 

                        MessageRequest request = e.Message as MessageRequest;

 

                        _logHelper.WriteLog("SYNC MESSAGE REQUEST RECEIVED : {0}", request.Content);

 

                        e.ReplyContent = "SUCCESS";

 

                        #endregion

                    }

                }

            }

            catch(Exception exception)

            {

                Console.WriteLine(exception.Message);

                Console.WriteLine(e.Message.Content);

            }

        }

 

        #endregion

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            ThreadPool.SetMinThreads(500 ,500 );

            ThreadPool.SetMaxThreads(5000,5000);

 

            #region 수신기를 설정한다.

 

            IReceiver receiver = new WCFReceiver();

 

            receiver.SetEndpoint(TransportMode.TCP, "127.0.0.1", 8010, 100);

 

            (receiver as IWCFReceiver).SetTimeout(60, 60, 1800, 1800);

 

            receiver.MessageReceived += receiver_MessageReceived;

 

            #endregion

            #region 송신기를 설정한다.

 

            _sender = new WCFSender();

 

            _sender.SetEndpoint(TransportMode.TCP, "127.0.0.1", 8010);

 

            #endregion

 

            receiver.Start();

 

            _logHelper.WriteLog("PRESS ANY KEY TO EXIT PROGRAM...");

 

            Console.ReadKey(true);

 

            receiver.Stop();

 

            _logHelper.WriteLog("RECEIVER STOPPED...");

        }

 

        #endregion

    }

}

 

 

Client

 

 

using System;

using System.Threading;

 

using DSCORE.NETWORK.MESSAGE;

using DSCORE.NETWORK.MESSAGE.WCF;

 

namespace TEST.NETWORK.MESSAGE.WCF.CLIENT

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 로그 헬퍼

        /// </summary>

        private static ILogHelper _logHelper = new ConsoleLogHelper();

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 수신기 메시지 수신시 처리하기 - receiver_MessageReceived(sender, e)

 

        /// <summary>

        /// 수신기 메시지 수신시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private static void receiver_MessageReceived(object sender, MessageReceivedEventArgs e)

        {

            try

            {

                if(e.Message.SendType == SendType.ASYNC)

                {

                    if(e.Message is MessageResult)

                    {

                        MessageResult messageResult = e.Message as MessageResult;

 

                        _logHelper.WriteLog("ASYNC MESSAGE RESULT RECEIVED : {0}", messageResult.Content);

 

                        Thread.Sleep(1000);

 

                        Environment.Exit(0);

                    }

                }

            }

            catch(Exception exception)

            {

                Console.WriteLine(exception.Message);

            }

        }

 

        #endregion

 

        #region 프로그램 시작하기 - Main(argumentArray)

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        /// <param name="argumentArray">인자 배열</param>

        /// <returns>처리 결과</returns>

        private static int Main(string[] argumentArray)

        {

            string number = null;

            

            if(argumentArray.Length > 0)

            {

                number = argumentArray[0];

            }

            else

            {

                number = "100";

            }

 

            int returnCode = 0;

            

            _logHelper.WriteLog("START MESSAGE TEST : NUMBER={0}", number);

 

            MessagePoint localPoint = new MessagePoint(TransportMode.TCP, "127.0.0.1", NetworkHelper.GetAvailablePort());

 

            #region 수신기를 설정한다.

 

            IReceiver receiver = new WCFReceiver();

 

            receiver.SetEndpoint(localPoint, 10);

 

            receiver.MessageReceived += receiver_MessageReceived;

 

            #endregion

            #region 송신기를 설정한다.

 

            ISender sender = new WCFSender();

 

            sender.SetEndpoint(localPoint);

 

            #endregion

 

            receiver.Start();

 

            #region 메시지 요청을 제출합니다.

 

            MessagePoint remotePoint = new MessagePoint(TransportMode.TCP, "127.0.0.1", 8010);

 

            MessageRequest request = new MessageRequest();

 

            request.Group   = "AAA";

            request.Content = number;

 

            sender.SendMessageRequestAsync(remotePoint, request);

 

            _logHelper.WriteLog("MSSAGE REQUEST SENT...");

 

            #endregion

 

            Console.WriteLine("PRESS ANY KEY TO EXIT PROGRAM...");

 

            Console.ReadKey(true);

 

            return returnCode;

        }

 

        #endregion

    }

}

 

 

 

▶ 개발 환경

 

  • 개발 도구 : Visual Studio 2017

  • 프로그래밍 언어 : C# (.NET Framework 4.5)

  • 사용 3자 컴포넌트 : (없음)

 

▶ 소스 코드 구성

 

 

▶ 구입 문의

 

소스 코드 및 구입 방법에 대해 icodebroker@naver.com 메일로 문의하시면 답변을 드리도록 하겠습니다.

 

Posted by 사용자 icodebroker

댓글을 달아 주세요