public void Setup() { var own_ip = NetowrkUtil.GetOwnIP(); var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Debug.Log("TCP Setup"); socket.Bind(new IPEndPoint(IPAddress.Parse(own_ip), mUsePort)); var to = new IPEndPoint(IPAddress.Parse(mToIP), mToPort); mIsSetuped = true; ThreadManager.Get().Work("SocketTCPSender Setup", null, (obj) => { byte [] buffer = mMsgList.Pop(); if (buffer == null || buffer.Length < 1) { System.Threading.Thread.Sleep(8); return(ThreadState.Continue); } socket.SendTo(buffer, buffer.Length, SocketFlags.None, to); Msg.Gen().Set(Msg.TO, "Debug").Set(Msg.ACT, "log") .Set(Msg.MSG, "sended success").Pool(); return(ThreadState.Continue); }); }
private static bool OnRecieve(byte [] receved, IPEndPoint sender) { var test = Msg.Gen().Set(Msg.TO, "Debug") .Set(Msg.ACT, "log").Set("what", "get 798789789--------"); test.Pool(); if (receved.Length < 1) { return(false); } CheckedRet <Msg> obj = null; try { Serializer.SetDatatype(Serializer.SerialType.Binary); obj = Serializer.Deserialize <Msg>(ByteList.Zero.Add(receved)); if (obj.Key == false) { throw new UnDeserializableException(); } } catch (Exception e) { DebugLog.Log.Print(e.ToString()); Msg.Gen().Set(Msg.TO, "Debug"). Set(Msg.ACT, "log") .Set(Msg.MSG, "UnDeserializableException").Pool(); throw new UnDeserializableException(); } obj.Value.Set("From", "" + NetowrkUtil.GetOwnIP()).Pool(); Msg.Gen().Set(Msg.TO, "Debug").Set(Msg.ACT, "log") .Set(Msg.MSG, "receice ok:" + obj.Value.ToJson()).Pool(); DebugLog.Log.Print("test1 => " + obj.Value.ToJson()); return(true); }
public void Setup() { mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); var recv_ip = NetowrkUtil.GetOwnIP(); Debug.Log("Observe IP = " + recv_ip); Debug.Log("Observe Port = " + mReceivePort); mSocket.Bind(new IPEndPoint(IPAddress.Any, mReceivePort)); var sender = new IPEndPoint(IPAddress.Any, 0) as EndPoint; ThreadManager.Get().Work("SocketUDPReceiver Setup", null, (e) => { if (mSocket == null) { System.Threading.Thread.Sleep(10); return(ThreadState.Continue); } Msg.Gen().Set(Msg.TO, "Debug").Set(Msg.ACT, "log") .Set(Msg.MSG, "Recv").Pool(); byte [] buffer = new byte [2500]; int size = mSocket .ReceiveFrom(buffer, 0, 2499, SocketFlags.None, ref sender); if (size < 1) { return(ThreadState.Continue); } Serializer.SetDatatype(Serializer.SerialType.Binary); CheckedRet <Msg> ret = null; try { var buf = ByteList.Zero.Add(buffer); var count = buffer.Length - size; buf.RemoveRangeBase(size, count); ret = Serializer.Deserialize <Msg>(buf); } catch { Msg.Gen().Set(Msg.TO, "Debug").Set(Msg.ACT, "log") .Set(Msg.MSG, "get FAIL").Pool(); } Debug.Log("ret.Key" + ret.Key); if (ret.Key == false) { return(ThreadState.Continue); } ret.Value.Set("From", sender.AddressFamily.ToString()).Pool(); return(ThreadState.Continue); }); }