LTT Source에 들어가는 Module Dll
장정호
2020-12-15 dd4a016ef8fc5cae0cbd336a5bdec4651b1d87c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
 
namespace DIT.Framework.Module._00_Communication
{
    /// <summary>
    /// Socket Module을 제어하기 위한 클래스
    /// </summary>
    class SocketModule
    {
        /// <summary>
        /// Client Excpetion Log를 기록하기위한 파라미터.
        /// </summary>
        private ILog ExceptionLog = LogManager.GetLogger("ClientException");
 
        /// <summary>
        /// Client Send Log를 기록하기위한 파라미터.
        /// </summary>
        private ILog SendLog = LogManager.GetLogger("ClientSend");
 
        /// <summary>
        /// Client Receive Log를 기록하기위한 파라미터.
        /// </summary>
        private ILog ReceiveLog = LogManager.GetLogger("ClientReceive");
 
        /// <summary>
        /// 서버와 통신하기 위한 Client Socket
        /// </summary>
        public Socket Client { get; private set; }
 
        /// <summary>
        /// 서버와 통신하기 위한 IP
        /// </summary>
        public string ip { get; private set; }
 
        /// <summary>
        /// 서버와 통신하기 위한 Port
        /// </summary>
        public int port { get; private set;}
 
        /// <summary>
        /// Exception Log 사용유무 확인
        /// </summary>
        public bool UseExceptionLog { get; set; }
 
        /// <summary>
        /// Send Log 사용유무 확인
        /// </summary>
        public bool UseSendLog { get; set; }
 
        /// <summary>
        /// Receive Log 사용유무 확인
        /// </summary>
        public bool UseReceiveLog { get; set; }
 
        /// <summary>
        /// Socket Client 생성자
        /// </summary>
        /// <param name="ip">서버에 연결할 IP</param>
        /// <param name="port">서버에 연결할 Port</param>
        public SocketModule(string ip, int port)
        {
            this.ip = ip;
            this.port = port;
        }
 
        /// <summary>
        /// Exception Log를 기록하기 위한 메서드
        /// </summary>
        /// <param name="msg">Error Message</param>
        private void WriteExceptionLog(string msg)
        {
            if(UseExceptionLog)
                ExceptionLog.Debug(msg);
        }
 
        /// <summary>
        /// Send Log를 기록하기 위한 메서드
        /// </summary>
        /// <param name="msg">Send Message</param>
        private void WriteSendLog(string msg)
        {
            if (UseSendLog)
                SendLog.Debug(msg);
        }
 
        /// <summary>
        /// Receive Log를 기록하기 위한 메서드
        /// </summary>
        /// <param name="msg">Receive Message</param>
        private void WriteReceiveLog(string msg)
        {
            if (UseReceiveLog)
                ReceiveLog.Debug(msg);
        }
 
        /// <summary>
        /// 소켓을 초기화 하고, 서버와 연결하기 위한 메서드
        /// </summary>
        /// <returns></returns>
        public bool Connect()
        {
            try
            {
                if (Client == null)
                    Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), port);
 
                //Client.BeginConnect(ipep, new AsyncCallback(ConnectedCallback), Client);
                return true;
            }
            catch (Exception e)
            {
                WriteExceptionLog(e.Message);
                return false;
            }
        }
 
        /// <summary>
        /// 서버와의 통신을 끊기위한 메서드
        /// </summary>
        public void DisConnect()
        {
            Client.Close();
            Client.Dispose();
            Client = null;
        }
 
        public void Send(string data)
        {
            try
            {
                byte[] buffer = Encoding.Default.GetBytes(data);
                Client.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), Client);
            }
            catch (Exception e)
            {
                WriteExceptionLog(e.Message);
            }
        }
 
        public void SendCallback(IAsyncResult ar)
        {
            try
            {
                Socket sock = (Socket)ar.AsyncState;
 
                int byteSent = sock.EndSend(ar);
            }
            catch (Exception e)
            {
 
                WriteExceptionLog(e.Message);
            }
        }
    }
}