void OnMessage(byte[] msg, Agent agent) { mutAgent.WaitOne(); agentReceiveCounter++; mutAgent.ReleaseMutex(); agent.action.Set((Dictionary <System.Object, System.Object>)packer.Unpack(msg)); if (agent.state.endEpisode) { agent.SetGene((Dictionary <System.Object, System.Object>)packer.Unpack(msg)); // add Naka } if (agentReceiveCounter == agents.Count) { received.Set(); } }
//instance methods public Connection(string url) { _url = url; _sock = this.NewSocket(url); _serde = this.NewSerde(); _stream = new NetworkStream(_sock); _stream.ReadTimeout = 10; _sendbuf = new MemoryStream(); _reader = _serde.Unpack(_stream); }
public virtual ServerException Error(Serde sr) { if (Success) { return(null); } var ext = Args <Ext>(0); var m = new MemoryStream(ext.Data); //run iterator and skip first yield return var it = sr.Unpack(m); it.MoveNext(); it.MoveNext(); string name = (string)it.Current; it.MoveNext(); string bt = (string)it.Current; it.MoveNext(); object[] args = (object[])it.Current; return(new ServerException(name, bt, args)); }
void Update() { if (communicationMode == CommunicationMode.ASYNC) { Application.targetFrameRate = (int)Mathf.Max(60.0f, 60.0f * timeScale); for (int i = 0; i < agents.Count; i++) { byte[] msg = clients [i].PopAIMessage(); if (msg != null) { var packer = new MsgPack.BoxingPacker(); agents [i].action.Set((Dictionary <System.Object, System.Object>)packer.Unpack(msg)); agents [i].ResetState(); Time.timeScale = timeScale; } } if (lastSendTime + cycleTimeStepSize <= Time.time) { lastSendTime = Time.time; if (Time.time - episodeStartTime > episodeTimeLength) { TimeOver(); } // TODO all agents have same value if (agents [0].state.endEpisode) { StartNewEpisode(); } for (int i = 0; i < agents.Count; i++) { agents [i].UpdateState(); clients [i].PushAgentState(agents [i].state); } Time.timeScale = 0.0f; } if (agents[0].state.reachedEnd) { Debug.Log("reached end"); StartNewEpisode(); } } }
void Update() { if (communicationMode == CommunicationMode.ASYNC) { Application.targetFrameRate = (int)Mathf.Max(60.0f, 60.0f * timeScale); for (int i = 0; i < agents.Count; i++) { byte[] msg = clients [i].PopAIMessage(); if (msg != null) { var packer = new MsgPack.BoxingPacker(); agents [i].action.Set((Dictionary <System.Object, System.Object>)packer.Unpack(msg)); agents [i].ResetState(); Time.timeScale = timeScale; } } if (lastSendTime + cycleTimeStepSize <= Time.time) { lastSendTime = Time.time; if (Time.time - episodeStartTime > episodeTimeLength) { TimeOver(); } // TODO all agents have same value if (agents [0].state.endEpisode) { StartNewEpisode(); } for (int i = 0; i < agents.Count; i++) { agents [i].UpdateState(); // <!> Must Get Parametar (add Naka) agents [i].state.rewards = new float[3]; // 3 -> number of parametar for (int j = 0; j < 3; j++) // same { agents [i].state.rewards[j] = agents[j].state.reward; } clients [i].PushAgentState(agents [i].state); } Time.timeScale = 0.0f; } } }
private static object[] UnpackScorePayload(byte[] payload) { MsgPack.BoxingPacker packer = new MsgPack.BoxingPacker(); object[] result = (object [])packer.Unpack(payload, 0, payload.Length); return(result); }
//Yay, fun method! public Dictionary<object, object> Execute(string method, object[] args) { if (string.IsNullOrEmpty(_host)) throw new Exception("Host null or empty"); if (method != "auth.login" && string.IsNullOrEmpty(_token)) throw new Exception("Not authenticated."); BoxingPacker boxingPacker = new BoxingPacker(); CompiledPacker compiledPacker = new CompiledPacker(true); ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {return true;}; //dis be bad, no ssl check HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_host); request.ContentType = "binary/message-pack"; request.Method = "POST"; Stream requestStream = request.GetRequestStream(); MsgPackWriter msgpackWriter = new MsgPackWriter(requestStream); msgpackWriter.WriteArrayHeader(args.Length + 1 + (string.IsNullOrEmpty(_token) ? 0 : 1)); msgpackWriter.Write(method); if (!string.IsNullOrEmpty(_token)) msgpackWriter.Write(_token); foreach (object arg in args) { if (arg is string) msgpackWriter.Write(arg as string); else if (arg is Dictionary<object, object>) msgpackWriter.Write(compiledPacker.Pack<Dictionary<object, object>>(arg as Dictionary<object, object>)); } requestStream.Close(); Stream responseStream = request.GetResponse().GetResponseStream(); //everything is a bunch of bytes, needs to be typed Dictionary<object, object> resp = boxingPacker.Unpack(responseStream) as Dictionary<object, object>; //This is me trying to type the response for the user.... Dictionary<object, object> returnDictionary = new Dictionary<object, object>(); System.Text.Encoding enc = System.Text.Encoding.UTF8; foreach (KeyValuePair<object, object> pair in resp) { string keyType = pair.Key.GetType().ToString(); string valueType = pair.Value.GetType().ToString(); if (pair.Value.GetType() == typeof(bool)) returnDictionary.Add(enc.GetString(pair.Key as byte[]), ((bool)pair.Value).ToString()); else if (pair.Value.GetType() == typeof(byte[])) returnDictionary.Add(enc.GetString(pair.Key as byte[]), enc.GetString(pair.Value as byte[])); else if (pair.Value.GetType() == typeof(object[])) returnDictionary.Add(enc.GetString(pair.Key as byte[]), pair.Value); else if (pair.Value.GetType() == typeof(UInt32)) returnDictionary.Add(enc.GetString(pair.Key as byte[]), ((UInt32)pair.Value).ToString()); else if (pair.Value.GetType() == typeof(Int32)) returnDictionary.Add(enc.GetString(pair.Key as byte[]), ((Int32)pair.Value).ToString()); else throw new Exception("key type: " + keyType + ", value type: " + valueType); } return returnDictionary; }
public Dictionary<object, object> Execute(string method, params object[] args) { if (string.IsNullOrEmpty (_host)) throw new Exception ("Host null or empty"); if (method != "auth.login" && string.IsNullOrEmpty (_token)) throw new Exception ("Not authenticated."); BoxingPacker boxingPacker = new BoxingPacker (); ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {return true;}; //dis be bad, no ssl check HttpWebRequest request = (HttpWebRequest)WebRequest.Create (_host); request.ContentType = "binary/message-pack"; request.Method = "POST"; Stream requestStream = request.GetRequestStream (); MsgPackWriter msgpackWriter = new MsgPackWriter (requestStream); msgpackWriter.WriteArrayHeader (args.Length + 1 + (string.IsNullOrEmpty (_token) ? 0 : 1)); msgpackWriter.Write (method); if (!string.IsNullOrEmpty (_token)) msgpackWriter.Write (_token); foreach (object arg in args) Pack(msgpackWriter, arg); requestStream.Close (); Stream responseStream = request.GetResponse ().GetResponseStream (); //everything is a bunch of bytes, needs to be typed Dictionary<object, object > resp = boxingPacker.Unpack (responseStream) as Dictionary<object, object>; //This is me trying to type the response for the user.... Dictionary<object, object > returnDictionary = TypifyDictionary(resp); return returnDictionary; }