public override void OnStart() { this.server.SetJsonRPCFail("/", ActionRPCFail); this.server.AddJsonRPC("/", "help", ActionRPC_Help); this.server.AddJsonRPC("/", "listpeer", ActionRPC_ListPeer); this.server.AddJsonRPC("/", "sendrawtransaction", ActionRPC_SendRawTransaction); this.server.Start(this.config.HttpListenEndPoint.Port, this.config.HttpsListenEndPoint.Port, this.config.HttpsPFXFilePath, this.config.HttpsPFXFilePassword); logger.Warn("RPC start at port=" + this.config.HttpListenEndPoint.Port); }
public Node(string EndPoint = null, string initpeer = null, string ListenEndPoint = null, bool beEnableQueryPeers = true, string jsonFile = "config.json") { logger = new AllPet.Common.Logger(); logger.Warn("Allpet.Node v0.001 Peer 01"); var config = new Config(logger); //init current path. //把当前目录搞对,怎么启动都能找到dll了 var lastpath = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);; Console.WriteLine("exepath=" + lastpath); Environment.CurrentDirectory = lastpath; var system = AllPet.Pipeline.PipelineSystem.CreatePipelineSystemV1(new AllPet.Common.Logger()); var config_node = config.GetJson(jsonFile, ".ModulesConfig.Node") as JObject; //-----------------配置 publicendpoint /initpeer if (EndPoint != null) { config_node["PublicEndPoint"] = EndPoint; } if (initpeer != null) { config_node["InitPeer"] = new JArray(initpeer); } if (Config.IsOpen(config_node)) { this.actor = new AllPet.Module.Module_Node(logger, config_node); system.RegistModule("node", this.actor); this.actor.beEnableQueryPeers = beEnableQueryPeers; } else { logger.Error("cant find config for node"); return; } system.OpenNetwork(new AllPet.peer.tcp.PeerOption() { }); var endpoint = config.GetIPEndPoint(jsonFile, ".ListenEndPoint"); if (ListenEndPoint != null) { endpoint = ListenEndPoint.AsIPEndPoint(); } if (endpoint != null && endpoint.Port != 0) { try { system.OpenListen(endpoint); } catch (Exception err) { logger.Error("listen error:" + err.ToString()); } } system.Start(); //等待cli结束才退出 var pipeline = system.GetPipeline(null, "this/node"); //while (pipeline.IsVaild) //{ // var line = Console.ReadLine(); // if (string.IsNullOrEmpty(line) == false) // { // if (line == "exit") // { // break; // } // var cmds = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); // var dict = new MsgPack.MessagePackObjectDictionary(); // dict["cmd"] = (UInt16)AllPet.Module.CmdList.Local_Cmd; // var list = new MsgPack.MessagePackObject[cmds.Length]; // for (var i = 0; i < cmds.Length; i++) // { // list[i] = cmds[i]; // } // dict["params"] = list; // pipeline.Tell(new MsgPack.MessagePackObject(dict)); // } //} //system.Dispose(); this.sys = system; }
static void Main(string[] args) { logger = new AllPet.Common.Logger(); logger.Warn("Allpet.Node v0.001 Peer 01"); var config = new Config(logger); //init current path. //把当前目录搞对,怎么启动都能找到dll了 var lastpath = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);; Console.WriteLine("exepath=" + lastpath); Environment.CurrentDirectory = lastpath; var system = AllPet.Pipeline.PipelineSystem.CreatePipelineSystemV1(new AllPet.Common.Logger()); var config_node = config.GetJson("config.json", ".ModulesConfig.Node") as JObject; if (Config.IsOpen(config_node)) { system.RegistModule("node", new AllPet.Module.Module_Node(logger, config_node)); } else { logger.Error("cant find config for node"); return; } system.OpenNetwork(new AllPet.peer.tcp.PeerOption() { }); var endpoint = config.GetIPEndPoint("config.json", ".ListenEndPoint"); if (endpoint != null && endpoint.Port != 0) { try { system.OpenListen(endpoint); } catch (Exception err) { logger.Error("listen error:" + err.ToString()); } } system.Start(); //等待cli结束才退出 var pipeline = system.GetPipeline(null, "this/node"); while (pipeline.IsVaild) { var line = Console.ReadLine(); if (string.IsNullOrEmpty(line) == false) { if (line == "exit") { break; } var cmds = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); var dict = new MsgPack.MessagePackObjectDictionary(); dict["cmd"] = (UInt16)AllPet.Module.CmdList.Local_Cmd; var list = new MsgPack.MessagePackObject[cmds.Length]; for (var i = 0; i < cmds.Length; i++) { list[i] = cmds[i]; } dict["params"] = list; pipeline.Tell(new MsgPack.MessagePackObject(dict)); } } system.Dispose(); }
public unsafe void OpenNetwork(PeerOption option) { if (peer != null) { throw new Exception("already have init peer."); } peer = AllPet.peer.tcp.PeerV2.CreatePeer(logger); peer.Start(option); peer.OnClosed += (id) => { if (this.linkedIP.TryRemove(id, out string _remotestr)) { if (this.refSystems.TryRemove(_remotestr, out ISystemPipeline remote)) { (remote as RefSystemRemote).Close(); } Console.WriteLine("close line=" + id); } }; peer.OnLinkError += (id, err) => { Console.WriteLine("OnLinkError line=" + id + " ,err=" + err.ToString()); //var remotestr = linkedIP[id]; //if (this.refSystems.TryRemove(remotestr, out ISystemPipeline remote)) //{ // (remote as RefSystemRemote).Close(); ; //} }; peer.OnRecv += (id, data) => { //if (data.Length == 0) //{ // throw new Exception("err h01"); //} int seek = 0; var fromlen = data[seek]; seek++; string from = System.Text.Encoding.UTF8.GetString(data, seek, fromlen); seek += fromlen; var tolen = data[seek]; seek++; string to = System.Text.Encoding.UTF8.GetString(data, seek, tolen); seek += tolen; //if (from == "" || to =="") //{ // throw new Exception("err h02"); //} var remotestr = linkedIP[id]; var refsys = this.refSystems[remotestr]; var pipe = this.GetPipelineByFrom(refsys, "@" + id + "/" + from, "this/" + to) as PipelineRefLocal; //var pipe = this.GetPipeline(user, "this/" + to); var outbytes = new byte[data.Length - seek]; fixed(byte *pdata = data, pout = outbytes) { Buffer.MemoryCopy(pdata + seek, pout, outbytes.Length, outbytes.Length); } pipe.TellDirect(outbytes); }; peer.OnAccepted += (ulong id, IPEndPoint endpoint) => { var remotestr = endpoint.ToString(); linkedIP[id] = remotestr; RefSystemRemote remote = new RefSystemRemote(this, peer, endpoint, id, true); //remote.linked = true; (remote as RefSystemRemote).Linked(id, true, endpoint); this.refSystems[remotestr] = remote; Console.WriteLine("on accepted." + id + " = " + endpoint); }; peer.OnConnected += (ulong id, IPEndPoint endpoint) => { //lock (endpoint)//这个锁比较粗糙,临时增加,因为有可能Connect之后执行了一半,进入这里 // //后面考虑从更底层做操作 //{ // if (this.linkedIP.ContainsKey(id) == false) // { // var __remotestr = endpoint.ToString(); // this.linkedIP[id] = __remotestr; // RefSystemRemote __remote = new RefSystemRemote(this, peer, endpoint, id, false); // __remote.linked = true; // this.refSystems[__remotestr] = __remote; // __remote.Linked(id, false, endpoint); // logger.Info("==link now== systemid" + id); // } // else { var remotestr = this.linkedIP[id]; if (this.refSystems.ContainsKey(remotestr) == false) { logger.Warn("意外的值"); } var remote = this.refSystems[remotestr] as RefSystemRemote; //remote.linked = true; (remote as RefSystemRemote).Linked(id, false, endpoint); logger.Info("==link delay== systemid" + id); } //} //this.linkedIP[id] = endpoint.ToString(); ////主动连接成功,创建一个systemRef //var remotestr = this.linkedIP[id]; //RefSystemRemote remote = new RefSystemRemote(peer, remotestr, id); //remote.linked = true; //this.refSystems[remotestr] = remote; Console.WriteLine("on OnConnected." + id + " = " + endpoint); }; }
static void Main(string[] args) { logger = new AllPet.Common.Logger(); logger.Warn("Allpet.Node v0.001"); var config = new Config(logger); //init current path. //把当前目录搞对,怎么启动都能找到dll了 var lastpath = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);; Console.WriteLine("exepath=" + lastpath); Environment.CurrentDirectory = lastpath; var system = AllPet.Pipeline.PipelineSystem.CreatePipelineSystemV1(logger); var config_cli = config.GetJson("config.json", ".ModulesConfig.Cli") as JObject; var config_node = config.GetJson("config.json", ".ModulesConfig.Node") as JObject; var config_rpc = config.GetJson("config.json", ".ModulesConfig.RPC") as JObject; if (config_node.ContainsKey("Key_Nep2") && config_node.ContainsKey("Key_Password") == false) { Console.Write("input Key for Nep2>"); var pass = Console.ReadLine(); config_node["Key_Password"] = pass; } if (Config.IsOpen(config_cli)) { system.RegistModule("cli", new Module_Cli(logger, config_cli)); } if (Config.IsOpen(config_node)) { system.RegistModule("node", new AllPet.Module.Module_Node(logger, config_node)); } if (Config.IsOpen(config_rpc)) { system.RegistModule("rpc", new AllPet.Module.Module_RPC(logger, config_rpc)); } system.OpenNetwork(new AllPet.peer.tcp.PeerOption() { }); var endpoint = config.GetIPEndPoint("config.json", ".ListenEndPoint"); if (endpoint != null && endpoint.Port != 0) { try { system.OpenListen(endpoint); } catch (Exception err) { logger.Error("open listen err:" + err); } } //是不是开listen 这个事情可以留给Module system.Start(); //等待cli结束才退出 var pipeline = system.GetPipeline(null, "this/cli"); while (pipeline.IsVaild) { var line = Console.ReadLine(); if (line == "exit") { break; } System.Threading.Thread.Sleep(100); } system.Dispose(); }