본문 바로가기
C#

05# 소켓 비동기식 처리

by NaHyungMin 2016. 4. 18.

삼성 어딘가에서 쓰고 있을 내가 만든 소켓 비동기식 dll... 


점층적 생성자를 사용하고 있지만, 이펙티브 자바를 일찍 봤다면 builder 패턴을 여기다 적용 해봤을 수도 있었을 것 같다.

그나마 5달 지난 최근 코드 스타일인데.. 년수가 지날 수록 점점 코드 스타일이 변형되어 가는듯 ^^


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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*  151209 nhm, 경광등 제어용 라이브러리  경광등 아이피 및 포트는 중간에 변경할 수 없는 것이 초기 설정 */
using System;
using System.Collections.Generic;
using System.Linq;using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;using System.Text;
using System.Threading; 
 
public class LampManager
{    
    private string ip;    
    private int port;    
    private IPAddress ipAddress;    
    private byte[] sendByte = new byte[10];    
    private Socket socket = null;    
    private bool isConnect = false;     
 
    #region Property     
 
    public string IP    
    {        
        get        
        {            
            return ip;        
        }    
    }     
 
    public IPAddress IPAddress    
    {        
        get        
        {            
            return ipAddress;        
        }    
    }     
 
    public int Port    
    {        
        get        
        {            
            return port;        
        }    
    }     
 
    public bool IsConnect    
    {        
        get        
        {            
            return isConnect;        
        }    
    }     
 
    public int TimeOut    
    {        
        get;        
        set;    
    }     
 
    #endregion
 
    #region enum
    
    public enum LampColor
    {        
        Red = 2,
        Amber = 3,
        Green = 4,
        Blue = 5,
        White = 6
    }
 
    public enum LameColorState
    {
        Off = 0,
        On = 1
        OnOff = 2
    }
 
    public enum SoundGroup
    {        
        Off = 0,
        Fire = 1,
        Emergency = 2,
        Ambulance = 3,
        Pi = 4,
        PlContinune = 5
    }     
 
    #endregion     
 
    public LampManager(string ip, int port)    
    {        
        this.ip = ip;        
        this.port = port;        
        this.ipAddress = IPAddress.Parse(ip);         
        
        InitTImeOut();
        InitByteMessage();
    }
 
    public LampManager(string ip, int port, int timeOut)
    {        
        this.ip = ip;
        this.port = port;
        this.ipAddress = IPAddress.Parse(ip);
        this.TimeOut = timeOut;
        
        InitByteMessage();
    }
 
    public LampManager(IPAddress ipAddress, int port)
    {        
        this.ipAddress = ipAddress;
        this.port = port;
        this.ip = ipAddress.ToString();
 
        InitTImeOut();
        InitByteMessage();
    }
 
    public LampManager(IPAddress ipAddress, int port, int timeOut)
    {
        this.ipAddress = ipAddress;
        this.port = port;
        this.ip = ipAddress.ToString();
        this.TimeOut = timeOut;
         InitByteMessage();
    }
 
    #region Initialize
    private void ConnectSocket()
    {        
        IPEndPoint ipe = new IPEndPoint(IPAddress, port);
        socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        IAsyncResult asyncResult = socket.BeginConnect(ipe, null, socket);
        if (asyncResult.AsyncWaitHandle.WaitOne(this.TimeOut, true== true)
        {            
            socket.EndConnect(asyncResult);            
            isConnect = true;
        }
        else
        {
            isConnect = false;
        }
    }
 
     private void InitByteMessage()
     {
        sendByte[0= 0x57//Read
        sendByte[1= 0x00//Type
        sendByte[2= 0x00//Red
        sendByte[3= 0x00//Yellow
        sendByte[4= 0x00//Green
        sendByte[5= 0x00//Blue
        sendByte[6= 0x00//White
        sendByte[7= 0x00//Sound
        sendByte[8= 0x00//SPARE
        sendByte[9= 0x00//SPARE
    }
 
    private void InitTImeOut()
    {
        TimeOut = 5;
    }     
 
    #endregion
 
    #region Get Method
 
    #endregion
 
    #region Set Method
 
    public void Send()
    {
        ConnectSocket();
 
        if (IsConnect == true)
        {
            socket.Send(sendByte, sendByte.Length, 0);
 
            Thread.Sleep(10);
            socket.Disconnect(true);
        }
    }
 
    public void WirteLampColor(LampColor color, LameColorState state, bool isSend)
    {
        int index = Convert.ToInt32(color);
        int onOff = Convert.ToInt32(state);
        byte[] tempByte = BitConverter.GetBytes(onOff);
 
        if (tempByte != null)
        {
            sendByte[index] = tempByte[0];
 
            if (isSend == true)
            {
                Send();
            }
        }
     }
 
     public void WirteLampSound(SoundGroup sound, bool isSend)
     {
        int soundIndex = 7;
        byte[] tempByte = BitConverter.GetBytes(Convert.ToInt32(sound));
 
        if (tempByte != null)
        {
            sendByte[soundIndex] = tempByte[0];
 
            if (isSend == true)
            {
                Send();
            }
        }
     }
 
      #endregion
}
cs


'C#' 카테고리의 다른 글

07# C# Entity framework  (0) 2016.05.11
06# 네트워크 활성화/비활성화  (0) 2016.05.11
04# 속성(Property) 객체 생성 시 초기화  (0) 2016.04.18
03# 외부 dll 타입제어(dev express)  (0) 2016.04.18
02#INI 파일 읽기 쓰기  (0) 2016.04.18