Beispiel #1
0
        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);
            });
        }
Beispiel #2
0
        /// <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);
        }
Beispiel #3
0
        /// <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);
            });
        }
Beispiel #4
0
        /// <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);
            });
        }
Beispiel #5
0
        private void Setup()
        {
            //var recv_ip = NetowrkUtil.GetOwnIP();
            var socket = new Socket(AddressFamily.InterNetwork,
                                    SocketType.Stream, ProtocolType.Tcp);

            socket.NoDelay        = true;
            socket.SendBufferSize = 0;
            Debug.Log("TCP Setup");

            ThreadManager.Get().Work("SocketTCPSender Setup1", null, (obj) =>
            {
                try {
                    socket.Connect(mSendToIp, mSendToPort);
                } catch {
                    return(ThreadState.Continue);
                }
                mIsSetuped = true;
                Debug.Log("TCP ConnectSuccess");
                return(ThreadState.End);
            });

            ThreadManager.Get().Work("SocketTCPSender Setup2", null, (obj) =>
            {
                byte [] buffer = mMsgList.Pop();
                if (buffer == null || buffer.Length < 1)
                {
                    System.Threading.Thread.Sleep(NetworkUnit.INTERVAL);
                    return(ThreadState.Continue);
                }
                socket.Send(buffer, buffer.Length, SocketFlags.None);
                return(ThreadState.Continue);
            });
        }
Beispiel #6
0
        private void Setup()
        {
            var listen = new Socket(AddressFamily.InterNetwork,
                                    SocketType.Stream, ProtocolType.Tcp);

            listen.Bind(new IPEndPoint(IPAddress.Any, mReceivePort));
            listen.Listen(1);

            ThreadManager.Get().Work("SocketTCPReceiver Setup1", listen, MakeConnection);
            ThreadManager.Get().Work("SocketTCPReceiver Setup2", null, Receive);
        }
Beispiel #7
0
 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;
 }
Beispiel #8
0
        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);
            });
        }