283 lines
7.7 KiB
C#
283 lines
7.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace upper_limit_crawler
|
|
{
|
|
public static class ULUtil
|
|
{
|
|
//static string m_strLogServer = "http://mjjo53.us.to:8000";
|
|
|
|
static CPUTILLib.CpCybos m_CPUtil = new CPUTILLib.CpCybos();
|
|
static CPUTILLib.CpCodeMgr m_CPCodeMgr = new CPUTILLib.CpCodeMgr();
|
|
static TextBox m_tbLog = null;
|
|
static string m_strLogFileName;
|
|
static string m_strCSVFileName;
|
|
static TimeSpan m_TimeDiff = TimeSpan.Zero;
|
|
|
|
public static void Init(TextBox tbLog)
|
|
{
|
|
m_tbLog = tbLog;
|
|
|
|
if (Debugger.IsAttached == true)
|
|
{
|
|
m_strLogFileName = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "\\";
|
|
m_strCSVFileName = Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "\\";
|
|
}
|
|
|
|
m_strLogFileName += "log\\";
|
|
if (Directory.Exists(m_strLogFileName) == false)
|
|
Directory.CreateDirectory(m_strLogFileName);
|
|
m_strLogFileName += "log-" + GetCurTime().ToString("yyyy-MM-dd") + ".txt";
|
|
|
|
m_strCSVFileName += "log\\";
|
|
m_strCSVFileName += "log-" + GetCurTime().ToString("yyyy-MM-dd") + ".csv";
|
|
}
|
|
|
|
public static void SyncServerTime()
|
|
{
|
|
DateTime time = GetFastestNISTDate();
|
|
m_TimeDiff = time - DateTime.Now;
|
|
}
|
|
|
|
public static DateTime GetCurTime()
|
|
{
|
|
return DateTime.Now + m_TimeDiff;
|
|
}
|
|
|
|
public static void GetServerTime()
|
|
{
|
|
try
|
|
{
|
|
string strURL = string.Format("http://www.timeapi.org/utc/now?format={0}", (@"\Y-\m-\d \H:\M:\S"));
|
|
Uri uri = new Uri(strURL);
|
|
WebRequest wReq = (HttpWebRequest)WebRequest.Create(uri); // WebRequest 객체 형성 및 HttpWebRequest 로 형변환
|
|
wReq.Method = "GET"; // 전송 방법 "GET" or "POST"
|
|
|
|
using (HttpWebResponse wRes = (HttpWebResponse)wReq.GetResponse())
|
|
{
|
|
Stream respPostStream = wRes.GetResponseStream();
|
|
StreamReader readerPost = new StreamReader(respPostStream, Encoding.GetEncoding("EUC-KR"), true);
|
|
|
|
string resResult = readerPost.ReadToEnd();
|
|
}
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
|
|
{
|
|
var resp = (HttpWebResponse)ex.Response;
|
|
Trace("인터넷 시간 동기화 실패 ({0})", resp.StatusCode);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static DateTime GetFastestNISTDate()
|
|
{
|
|
var result = DateTime.MinValue;
|
|
// Initialize the list of NIST time servers
|
|
// http://tf.nist.gov/tf-cgi/servers.cgi
|
|
string[] servers = new string[] {
|
|
"time-c.nist.gov",
|
|
"nist1-macon.macon.ga.us",
|
|
"time.nist.gov",
|
|
"time-nw.nist.gov",
|
|
"nist-time-server.eoni.com"
|
|
};
|
|
|
|
// Try 5 servers in random order to spread the load
|
|
Random rnd = new Random();
|
|
foreach (string server in servers.OrderBy(s => rnd.NextDouble()).Take(5))
|
|
{
|
|
try
|
|
{
|
|
ULUtil.Trace("TimeSync trying [server:{0}]", server);
|
|
|
|
// Connect to the server (at port 13) and get the response
|
|
string serverResponse = string.Empty;
|
|
using (var reader = new StreamReader(new System.Net.Sockets.TcpClient(server, 13).GetStream()))
|
|
{
|
|
serverResponse = reader.ReadToEnd();
|
|
}
|
|
|
|
// If a response was received
|
|
if (!string.IsNullOrEmpty(serverResponse))
|
|
{
|
|
// Split the response string ("55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST) *")
|
|
string[] tokens = serverResponse.Split(' ');
|
|
|
|
// Check the number of tokens
|
|
if (tokens.Length >= 6)
|
|
{
|
|
// Check the health status
|
|
string health = tokens[5];
|
|
if (health == "0")
|
|
{
|
|
// Get date and time parts from the server response
|
|
string[] dateParts = tokens[1].Split('-');
|
|
string[] timeParts = tokens[2].Split(':');
|
|
|
|
// Create a DateTime instance
|
|
DateTime utcDateTime = new DateTime(
|
|
Convert.ToInt32(dateParts[0]) + 2000,
|
|
Convert.ToInt32(dateParts[1]), Convert.ToInt32(dateParts[2]),
|
|
Convert.ToInt32(timeParts[0]), Convert.ToInt32(timeParts[1]),
|
|
Convert.ToInt32(timeParts[2]));
|
|
|
|
// Convert received (UTC) DateTime value to the local timezone
|
|
result = utcDateTime.ToLocalTime();
|
|
|
|
return result;
|
|
// Response successfully received; exit the loop
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
catch
|
|
{
|
|
// Ignore exception and try the next server
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static string GetCurTimeString()
|
|
{
|
|
return GetCurTime().ToString("yyyy-MM-dd HH:mm:ss");
|
|
}
|
|
|
|
public static int GetCurTimeInt()
|
|
{
|
|
return int.Parse(GetCurTime().ToString("HHmmss"));
|
|
}
|
|
|
|
public static void WebLog(string strURL, string strMsg)
|
|
{
|
|
try
|
|
{
|
|
// 요청 String -> 요청 Byte 변환
|
|
byte[] byteDataParams = UTF8Encoding.UTF8.GetBytes(strMsg);
|
|
|
|
// HttpWebRequest 객체 생성, 설정
|
|
Uri reqURI = new Uri(strURL);
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reqURI);
|
|
request.Method = "POST"; // 기본값 "GET"
|
|
request.ContentType = "application/x-www-form-urlencoded";
|
|
request.ContentLength = byteDataParams.Length;
|
|
|
|
// 요청 Byte -> 요청 Stream 변환
|
|
Stream stDataParams = request.GetRequestStream();
|
|
stDataParams.Write(byteDataParams, 0, byteDataParams.Length);
|
|
stDataParams.Close();
|
|
|
|
// 요청, 응답 받기
|
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
|
|
|
// 응답 Stream 읽기
|
|
Stream stReadData = response.GetResponseStream();
|
|
StreamReader srReadData = new StreamReader(stReadData, Encoding.Default);
|
|
|
|
// 응답 Stream -> 응답 String 변환
|
|
string strResult = srReadData.ReadToEnd();
|
|
|
|
Console.WriteLine(strResult);
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
ex.ToString();
|
|
}
|
|
}
|
|
|
|
public static void Trace(string strMsg)
|
|
{
|
|
string strLog = "["+GetCurTimeString()+"] "+strMsg;
|
|
|
|
System.IO.File.AppendAllText(m_strLogFileName, strLog + Environment.NewLine);
|
|
Console.WriteLine(strLog);
|
|
|
|
bool bScrollToEnd = (m_tbLog.SelectionStart == m_tbLog.Text.Length);
|
|
m_tbLog.Text += strLog + Environment.NewLine;
|
|
if(bScrollToEnd == true)
|
|
{
|
|
m_tbLog.SelectionStart = m_tbLog.Text.Length;
|
|
m_tbLog.ScrollToCaret();
|
|
}
|
|
|
|
//WebLog(m_strLogServer, strLog);
|
|
}
|
|
|
|
public static void Trace(string strFormat, params object[] args)
|
|
{
|
|
Trace(string.Format(strFormat, args));
|
|
}
|
|
|
|
public static void TraceCSV(params object[] args)
|
|
{
|
|
string strLog = GetCurTimeString();
|
|
foreach (object arg in args)
|
|
strLog += ", " + arg;
|
|
|
|
System.IO.File.AppendAllText(m_strCSVFileName, strLog + Environment.NewLine, Encoding.UTF8);
|
|
}
|
|
|
|
public static int GetLimitRemainCountTrace()
|
|
{
|
|
return m_CPUtil.GetLimitRemainCount(CPUTILLib.LIMIT_TYPE.LT_TRADE_REQUEST);
|
|
}
|
|
|
|
public static int GetLimitRemainCountRQ()
|
|
{
|
|
return m_CPUtil.GetLimitRemainCount(CPUTILLib.LIMIT_TYPE.LT_NONTRADE_REQUEST);
|
|
}
|
|
|
|
public static int GetLimitRemainCountSB()
|
|
{
|
|
return m_CPUtil.GetLimitRemainCount(CPUTILLib.LIMIT_TYPE.LT_SUBSCRIBE);
|
|
}
|
|
|
|
public static bool IsConnected()
|
|
{
|
|
return (m_CPUtil.IsConnect==1);
|
|
}
|
|
|
|
public static string GetCodeName(string strCode)
|
|
{
|
|
return m_CPCodeMgr.CodeToName(strCode);
|
|
}
|
|
|
|
public static void Reset()
|
|
{
|
|
m_CPUtil = null;
|
|
System.GC.Collect(0, GCCollectionMode.Forced);
|
|
System.GC.WaitForFullGCComplete();
|
|
|
|
m_CPUtil = new CPUTILLib.CpCybos();
|
|
}
|
|
|
|
public static bool IsInTime(int iTime1, int iTime2, int iMinDiff)
|
|
{
|
|
int iHour1 = iTime1 / 10000;
|
|
int iMin1 = (iTime1 / 100) % 100;
|
|
int iSec1 = iTime1 % 100;
|
|
|
|
int iHour2 = iTime2 / 10000;
|
|
int iMin2 = (iTime2 / 100) % 100;
|
|
int iSec2 = iTime2 % 100;
|
|
|
|
int iTotalSec1 = iHour1 * 60 * 60 + iMin1 * 60 + iSec1;
|
|
int iTotalSec2 = iHour2 * 60 * 60 + iMin2 * 60 + iSec2;
|
|
|
|
return (iTotalSec2 >= iTotalSec1 && iTotalSec2 - iTotalSec1 <= iMinDiff * 60);
|
|
}
|
|
}
|
|
}
|