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);
|
}
|
}
|
}
|
}
|