/// <summary> /// Msgで送られたデータをファイルに書き込む /// 基本的に ([総情報長][Hash][型辞書長][型辞書配列(len:str)]) [型番号][[情報長]情報]です。 /// </summary> public void Write(string filename) { if (mThread != null || mDataStack.Count == 0) { return; } mThread = ThreadManager.Get(); string path = DataPath(filename); int data_first = mDataStack.Count; FileStream fs_w = new FileStream(path, FileMode.Create, FileAccess.Write); mThread.Work("InOut write", null, (e) => { if (mDataStack.Count == 0 || fs_w.CanWrite == false) { return(ThreadState.End); } var obj = mDataStack.Pop().Data; var bytes = Serializer.GetTypeList(); bytes.Add(Serializer.Serialize(obj)); bytes.AddHead(bytes.Count); fs_w.WriteAsync(bytes.ToArray(), 0, bytes.Count); return(ThreadState.Continue); }); }
/// <summary> /// サーバーを起動させたいときに呼ぶ /// </summary> /// <param name="port"></param> /// <param name="connectable"></param> /// <returns></returns> public bool StartServer() { var notReach = NetworkReachability.NotReachable; if (Application.internetReachability == notReach) { var msg = "application can not connect."; throw new NotSupportedException(msg); } if (mLoop.IsNotNull()) { return(true); } mLoop = ThreadManager.Get(); Debug.Log("StartServer:mObservePort" + mObservePort); try { mClient = new UdpClient(new IPEndPoint(IPAddress.Parse( NetworkUnit.GetLocalIPAddress()), mObservePort)); } catch (Exception e) { Msg.Gen().Set(Msg.TO, "Manager").Set(Msg.AS, "NetworkManager") .Set("type", "StartServer") .Set("result", "Fail") .Set("msg", e.ToString()) .Push(); return(false); } Msg.Gen().Set(Msg.TO, "Manager") .Set(Msg.AS, "NetworkManager") .Set("type", "StartServer") .Set("result", "Success").Push(); mLoop.Work("Udp client receiver", null, ReceiveLoop); return(true); }
/// <summary> /// 読み取ったデータを指定した先にMsgで送る /// 基本的に ([総情報長][Hash][型辞書長][型辞書配列(len:str)]) [型番号][[情報長]情報]です。 /// </summary> /// <param name="filename"></param> public void Read(string filename, string to, string a_s, string act) { if (mThread != null) { return; } mThread = ThreadManager.Get(); var data = ReadFromFile(filename); if (data == null || CheckHash(data) == false) { return; } ParseToDic(data); mThread.Work("InOut Read", null, (e) => { if (data.Count < 1) { return(ThreadState.End); } var type_data = Serializer.Deserialize(data); Msg.Gen().Set(Msg.TO, to) .Set(Msg.AS, a_s) .Set(Msg.ACT, act) .SetObjectData(type_data).Pool(); return(ThreadState.Continue); }); }
public void Setup() { mLoop = ThreadManager.Get(); mSendTo.Set(new IPEndPoint(IPAddress.Parse(mToAddress), mToPort)); mSendClient = new UdpClient(new IPEndPoint(IPAddress.Any, mUsePort)); mLoop.Work("Udp client send loop", null, SendLoop); Msg.Gen().Set(Msg.TO, "Manager") .Set(Msg.AS, "NetworkManager") .Set("type", "SenderSetup") .Set("result", "Success").Pool(); mIsSetuped = true; }
public void PoolFill(int num) { for (int i = 0; i < num; i++) { lock (mLock) { var h = Holding; var t = new LifedThread(h); mHolding.TrySet(h, t); mUnWorking.Push(t); } } }