예제 #1
0
        unsafe public void SendSerializedMsg(int cmd, NativeDataView ud, int channel = 0)
        {
            // The whole packet is sizeof(body) + sizeof(header), so no memory realloc
            IntPtr pdu = YASIO_NI.yasio_ob_new(ud.len + _packeter.GetHeaderSize());

            _packeter.EncodePDU(cmd, ud, pdu);
            if (channel < _sessions.Length)
            {
                IntPtr sid = _sessions[channel];
                if (sid != IntPtr.Zero)
                {
                    YASIO_NI.yasio_write_ob(_service, sid, pdu);
                }
                else
                {
                    Debug.LogFormat("Can't send message to the channel: {0}, the session not ok!", channel);
                }
            }
            else
            {
                Debug.LogFormat("Can't send message to the channel: {0}, the index is overflow, max allow value is:{1}", channel,
                                _sessions.Length);
            }

            YASIO_NI.yasio_ob_release(pdu);
        }
예제 #2
0
 unsafe public void EncodePDU(int cmd, NativeDataView ud, IntPtr ob)
 {
     YASIO_NI.yasio_ob_write_short(ob, (short)cmd);
     YASIO_NI.yasio_ob_write_int(ob, ud.len);             // packet size
     YASIO_NI.yasio_ob_write_int(ob, VERIFIED_MAGIC_NUM); // magic number
     YASIO_NI.yasio_ob_write_bytes(ob, ud.ptr, ud.len);   // ud
 }
예제 #3
0
        /// <summary>
        /// 业务只需要具体实现此函数的分发即可, 此处只是简单示例
        /// </summary>
        /// <param name="ev"></param>
        /// <param name="cmd"></param>
        /// <param name="ud"></param>
        /// <param name="channel"></param>
        public void HandleEvent(NetworkEvent ev, int cmd, NativeDataView ud, int channel)
        {
            UnityEngine.Debug.LogFormat("SampleNetworkPacketHandler.HandleEvent, event={0}, cmd={1}, channel={2}", ev, cmd, channel);
            if (cmd == AppProtocol.CMD_LOGIN_REQ)
            { // SampelScene应该是 channel:1 收到
                var msg = new AppProtocol.LoginReq();
                msg.decode(ud);
                msg.print();

                // 回应客户端
                var reply = new AppProtocol.LoginResp();
                reply.uid    = msg.uid;
                reply.status = 200; // 200 表示success
                (NativeDataView udReply, Stream hold) = reply.encode();
                NetworkServiceManager.Instance.SendSerializedMsg(AppProtocol.CMD_LOGIN_RESP, udReply, AppProtocol.SERVER_CHANNEL);
                hold.Dispose();
            }
            else if (cmd == AppProtocol.CMD_LOGIN_RESP)
            { // SampelScene应该是 channel:0 收到
                // 打印登录响应消息
                var msg = new AppProtocol.LoginResp();
                msg.decode(ud);
                msg.print();
            }
        }
예제 #4
0
 public unsafe void decode(NativeDataView ud)
 {
     using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream((byte *)ud.ptr, ud.len))
     {
         BinaryReader reader = new BinaryReader(ums);
         uid = IPAddress.NetworkToHostOrder(reader.ReadInt32());
         reader.Dispose();
     }
 }
예제 #5
0
        unsafe public (int, NativeDataView, Stream) DecodePDU(IntPtr bytes, int len)
        {
            Stream dataStream = new UnmanagedMemoryStream((byte *)bytes, len);

            try
            {
                using (var reader = new BinaryReader(dataStream, Encoding.ASCII, true))
                {
                    // 读取包头信息,magic number检验(这里也可以用hash摘要算法,计算消息体数据摘要)
                    int cmd      = IPAddress.NetworkToHostOrder(reader.ReadInt16());
                    int udLen    = IPAddress.NetworkToHostOrder(reader.ReadInt32());
                    int magicNum = IPAddress.NetworkToHostOrder(reader.ReadInt32());
                    reader.Dispose();

                    if (magicNum == VERIFIED_MAGIC_NUM)
                    {
                        // magic number校验正确
#if UNITY_EDITOR
                        if (DUMP_RECV_HEX)
                        {
                            int    bodyLen   = len - PROTO_HEADER_SIZE; // the udLen===bodyLen
                            string wholeHexs = YASIO_NI.DumpHex(bytes, len);
                            UnityEngine.Debug.LogFormat("cmd={0}, udLen/bodyLen={1}/{2}, wholeHexs: {3}\n", cmd, udLen, bodyLen, wholeHexs);
                            string bodyHexs = YASIO_NI.DumpHex(bytes + PROTO_HEADER_SIZE, bodyLen);
                            UnityEngine.Debug.LogFormat("cmd={0}, bodyHexs: {1}\n", cmd, bodyHexs);
                        }
#endif
                        NativeDataView ud = new NativeDataView {
                            ptr = bytes + PROTO_HEADER_SIZE, len = len - PROTO_HEADER_SIZE
                        };

                        return(cmd, ud, dataStream);
                    }
                    else
                    {
                        UnityEngine.Debug.LogErrorFormat("SampleNetworkPacketHandler.DecodePDU: check magic number failed, magicNum={0}, VERIFIED_MAGIC_NUM={1}", magicNum, VERIFIED_MAGIC_NUM);
                    }
                }
            }
            finally
            {
                dataStream?.Dispose();
            }

            return(-1, NativeDataView.NullValue, null);
        }
예제 #6
0
 /// <summary>
 /// 向远端发送数据
 /// </summary>
 /// <param name="bytes"></param>
 public void SendRaw(NativeDataView data, int channel)
 {
     if (channel < _sessions.Length)
     {
         IntPtr sid = _sessions[channel];
         if (sid != IntPtr.Zero)
         {
             YASIO_NI.yasio_write(_service, sid, data.ptr, data.len);
         }
         else
         {
             Debug.LogFormat("Can't send message to the channel: {0}, the session not ok!", channel);
         }
     }
     else
     {
         Debug.LogFormat("Can't send message to the channel: {0}, the index is overflow, max allow value is:{1}", channel,
                         _sessions.Length);
     }
 }
예제 #7
0
        private static void RunGraphCore(EnvironmentBlock *penv, IHostEnvironment env, string graphStr, int cdata, DataSourceBlock **ppdata)
        {
            Contracts.AssertValue(env);

            var     host = env.Register("RunGraph", penv->seed, null);
            JObject graph;

            try
            {
                graph = JObject.Parse(graphStr);
            }
            catch (JsonReaderException ex)
            {
                throw host.Except(ex, "Failed to parse experiment graph: {0}", ex.Message);
            }

            var runner = new GraphRunner(host, graph["nodes"] as JArray);

            var dvNative = new IDataView[cdata];

            try
            {
                for (int i = 0; i < cdata; i++)
                {
                    dvNative[i] = new NativeDataView(host, ppdata[i]);
                }

                // Setting inputs.
                var jInputs = graph["inputs"] as JObject;
                if (graph["inputs"] != null && jInputs == null)
                {
                    throw host.Except("Unexpected value for 'inputs': {0}", graph["inputs"]);
                }
                int iDv = 0;
                if (jInputs != null)
                {
                    foreach (var kvp in jInputs)
                    {
                        var pathValue = kvp.Value as JValue;
                        if (pathValue == null)
                        {
                            throw host.Except("Invalid value for input: {0}", kvp.Value);
                        }

                        var path    = pathValue.Value <string>();
                        var varName = kvp.Key;
                        var type    = runner.GetPortDataKind(varName);

                        switch (type)
                        {
                        case TlcModule.DataKind.FileHandle:
                            var fh = new SimpleFileHandle(host, path, false, false);
                            runner.SetInput(varName, fh);
                            break;

                        case TlcModule.DataKind.DataView:
                            IDataView dv;
                            if (!string.IsNullOrWhiteSpace(path))
                            {
                                var extension = Path.GetExtension(path);
                                if (extension == ".txt")
                                {
                                    dv = TextLoader.LoadFile(host, new TextLoader.Options(), new MultiFileSource(path));
                                }
                                else if (extension == ".dprep")
                                {
                                    dv = LoadDprepFile(BytesToString(penv->pythonPath), path);
                                }
                                else
                                {
                                    dv = new BinaryLoader(host, new BinaryLoader.Arguments(), path);
                                }
                            }
                            else
                            {
                                Contracts.Assert(iDv < dvNative.Length);
                                // prefetch all columns
                                dv = dvNative[iDv++];
                                var prefetch = new int[dv.Schema.Count];
                                for (int i = 0; i < prefetch.Length; i++)
                                {
                                    prefetch[i] = i;
                                }
                                dv = new CacheDataView(host, dv, prefetch);
                            }
                            runner.SetInput(varName, dv);
                            break;

                        case TlcModule.DataKind.PredictorModel:
                            PredictorModel pm;
                            if (!string.IsNullOrWhiteSpace(path))
                            {
                                using (var fs = File.OpenRead(path))
                                    pm = new PredictorModelImpl(host, fs);
                            }
                            else
                            {
                                throw host.Except("Model must be loaded from a file");
                            }
                            runner.SetInput(varName, pm);
                            break;

                        case TlcModule.DataKind.TransformModel:
                            TransformModel tm;
                            if (!string.IsNullOrWhiteSpace(path))
                            {
                                using (var fs = File.OpenRead(path))
                                    tm = new TransformModelImpl(host, fs);
                            }
                            else
                            {
                                throw host.Except("Model must be loaded from a file");
                            }
                            runner.SetInput(varName, tm);
                            break;

                        default:
                            throw host.Except("Port type {0} not supported", type);
                        }
                    }
                }
                runner.RunAll();

                // Reading outputs.
                using (var ch = host.Start("Reading outputs"))
                {
                    var jOutputs = graph["outputs"] as JObject;
                    if (jOutputs != null)
                    {
                        foreach (var kvp in jOutputs)
                        {
                            var pathValue = kvp.Value as JValue;
                            if (pathValue == null)
                            {
                                throw host.Except("Invalid value for input: {0}", kvp.Value);
                            }
                            var path    = pathValue.Value <string>();
                            var varName = kvp.Key;
                            var type    = runner.GetPortDataKind(varName);

                            switch (type)
                            {
                            case TlcModule.DataKind.FileHandle:
                                var fh = runner.GetOutput <IFileHandle>(varName);
                                throw host.ExceptNotSupp("File handle outputs not yet supported.");

                            case TlcModule.DataKind.DataView:
                                var idv = runner.GetOutput <IDataView>(varName);
                                if (path == CSR_MATRIX)
                                {
                                    SendViewToNativeAsCsr(ch, penv, idv);
                                }
                                else if (!string.IsNullOrWhiteSpace(path))
                                {
                                    SaveIdvToFile(idv, path, host);
                                }
                                else
                                {
                                    var infos = ProcessColumns(ref idv, penv->maxSlots, host);
                                    SendViewToNativeAsDataFrame(ch, penv, idv, infos);
                                }
                                break;

                            case TlcModule.DataKind.PredictorModel:
                                var pm = runner.GetOutput <PredictorModel>(varName);
                                if (!string.IsNullOrWhiteSpace(path))
                                {
                                    SavePredictorModelToFile(pm, path, host);
                                }
                                else
                                {
                                    throw host.Except("Returning in-memory models is not supported");
                                }
                                break;

                            case TlcModule.DataKind.TransformModel:
                                var tm = runner.GetOutput <TransformModel>(varName);
                                if (!string.IsNullOrWhiteSpace(path))
                                {
                                    using (var fs = File.OpenWrite(path))
                                        tm.Save(host, fs);
                                }
                                else
                                {
                                    throw host.Except("Returning in-memory models is not supported");
                                }
                                break;

                            case TlcModule.DataKind.Array:
                                var objArray = runner.GetOutput <object[]>(varName);
                                if (objArray is PredictorModel[])
                                {
                                    var modelArray = (PredictorModel[])objArray;
                                    // Save each model separately
                                    for (var i = 0; i < modelArray.Length; i++)
                                    {
                                        var modelPath = string.Format(CultureInfo.InvariantCulture, path, i);
                                        SavePredictorModelToFile(modelArray[i], modelPath, host);
                                    }
                                }
                                else
                                {
                                    throw host.Except("DataKind.Array type {0} not supported", objArray.First().GetType());
                                }
                                break;

                            default:
                                throw host.Except("Port type {0} not supported", type);
                            }
                        }
                    }
                }
            }
            finally
            {
                // The raw data view is disposable so it lets go of unmanaged raw pointers before we return.
                for (int i = 0; i < dvNative.Length; i++)
                {
                    var view = dvNative[i];
                    if (view == null)
                    {
                        continue;
                    }
                    host.Assert(view is IDisposable);
                    var disp = (IDisposable)dvNative[i];
                    disp.Dispose();
                }
            }
        }
예제 #8
0
 unsafe void decode(NativeDataView ud);