public static ByteStream GetSetFieldCommand(string _FieldName, object _Value) { ByteStream stream = new ByteStream(); stream.Write(2); Converter.Write(_FieldName, stream); Converter.Write(_Value, stream); return stream; }
public static void Write(object _Object, ByteStream _TargetStream) { Initialize(); Type t = _Object.GetType(); if (Converters.ContainsKey(t)) { Converters[typeof(string)].WriteObject(t.FullName, _TargetStream); Converters[t].WriteObject(_Object, _TargetStream); } else Log.Write("Converter", "Cant find converter for Type {0}", t.Name); }
public static ByteStream GetInvokeCommand(string _Name, params object[] _Parameters) { ByteStream stream = new ByteStream(); stream.Write(1); Converter.Write(_Name, stream); Converter.Write((ushort)_Parameters.Length, stream); foreach (object o in _Parameters) { Converter.Write(o, stream); } return stream; }
public static object Read(ByteStream _Stream) { Initialize(); if (_Stream.Length > 0) { string typename = (string)Converters[typeof(string)].ReadObject(_Stream); Type t = Type.GetType(typename); if (t != null) { if (Converters.ContainsKey(t)) return Converters[t].ReadObject(_Stream); } else { Log.Write("Converter", "Cant find Type {0}", typename); } } else { Log.Write("Converter", "Cant read from stream, stream is empty"); } return null; }
public virtual void Send(ByteStream _RawData) { }
public void RequestNetworkMap() { Log.Write("Networking", "NetworkMap request received"); //send connections foreach (Connection connection in NetworkManager.LocalNode.Connections) { SendToAll("MapConnection", NetworkManager.LocalNode.NodeID, connection.RemoteNodeID); } //send classes foreach (NetworkClassMaster networkclass in NetworkManager.LocalNode.NetworkClasses.Values) { SendToAll("MapClass", NetworkManager.LocalNode.NodeID, networkclass.Name, networkclass.TypeName); //send methods foreach (string methodname in networkclass.GetNetworkMethods()) { string parameters = ""; foreach (ParameterInfo i in networkclass.GetNetworkMethodInfo(methodname).GetParameters()) parameters += i.Name + " "; SendToAll("MapClassMethod", NetworkManager.LocalNode.NodeID, networkclass.Name, methodname, parameters); } //send fields foreach (string fieldname in networkclass.GetNetworkFields()) { FieldInfo info = networkclass.GetNetworkFieldInfo(fieldname); object value = networkclass.GetField(fieldname); if (value != null) { ByteStream valuestream = new ByteStream(); Converter.Write(value, valuestream); SendToAll("MapClassField", NetworkManager.LocalNode.NodeID, networkclass.Name, fieldname, info.FieldType.FullName, valuestream); } else { Log.Write("Networking", Log.Line.Type.Error, "cant send field : {0}", fieldname); } } } }
public void ExecuteRemoteCommand(string _TargetClass, ByteStream _Command) { bool found = false; foreach (NetworkNodeRemote node in NetworkManager.RemoteNodes.Values) { foreach (NetworkClassSlave clas in node.LocalDevices.Values) { if (clas.Name == _TargetClass) { clas.EnqueueCommand(_Command); found = true; } } } if (!found) Log.Write("Networking", Log.Line.Type.Warning, "not NetworkClassSlave found with name {0}, try to refresh network map", _TargetClass); }
public void ExecuteLocalCommand(string _TargetClass, ByteStream _Command) { if (NetworkManager.LocalNode.NetworkClasses.ContainsKey(_TargetClass)) NetworkManager.LocalNode.NetworkClasses[_TargetClass].EnqueueCommand(_Command); else Log.Write("Networking", Log.Line.Type.Warning, "Network class {0} not found", _TargetClass); }
internal virtual void ExecuteCommand(ByteStream _Stream) { byte command = _Stream.Read(); switch (command) { case 1://invoke method { string methodname = (string)Converter.Read(_Stream); MethodInfo info = GetNetworkMethodInfo(methodname); if (info != null) { int paramcount = (ushort)Converter.Read(_Stream); object[] parameters = new object[paramcount]; for (int i = 0; i < paramcount; i++) parameters[i] = Converter.Read(_Stream); InvokeMethod(methodname, parameters); } else Log.Write("NetworkClass", Log.Line.Type.Error, "Cant find method with name {0}", methodname); } break; case 2://set field { string fieldname = (string)Converter.Read(_Stream); FieldInfo info = GetNetworkFieldInfo(fieldname); if (info != null) { object value = Converter.Read(_Stream); SetField(fieldname, value); } else Log.Write("NetworkClass", Log.Line.Type.Error, "Cant find field with name {0}", fieldname); } break; } }
public override void Update() { while (true) { if (currentpacketsize == 0) { //read new packetype if (ReceiveBuffer.Length < 2) return; //there is no data available so we cant read any currentpacketsize = BitConverter.ToUInt16(ReceiveBuffer.Read(2), 0); } if (currentpacketsize != 0) { if (ReceiveBuffer.Length >= currentpacketsize) { byte[] buffer = ReceiveBuffer.Read(currentpacketsize); ByteStream packet = new ByteStream(buffer); bool broadcast = packet.Read() != 0; uint action = (uint)Converter.Read(packet); if (!NetworkManager.ActionBlackList.Contains(action)) { NetworkManager.ActionBlackList.Add(action); if (broadcast) { foreach (Connection c in NetworkManager.LocalNode.Connections) { if (c != this) c.Send(new ByteStream(buffer)); } } methods.ExecuteCommand(packet); } currentpacketsize = 0; } else return; } } }
public override void Send(ByteStream _RawData) { byte[] data = _RawData.ReadAll(); mutex.WaitOne(); SendBuffer.Write(BitConverter.GetBytes((ushort)data.Length)); SendBuffer.Write(data); mutex.ReleaseMutex(); }
public override void Send(bool _BroadCast, uint _ActionID, string _FunctionName, params object[] _Parameters) { ByteStream s = new ByteStream(); s.Write((byte)(_BroadCast ? 255 : 0)); Converter.Write(_ActionID, s); s.Write(NetworkClassMaster.GetInvokeCommand(_FunctionName, _Parameters)); Send(s); }
public void Write(ByteStream _Stream) { Write(_Stream.ReadAll()); }
public abstract object ReadObject(ByteStream _Stream);
public void AddClassField(string _NodeID, string _ClassName, string _FieldName, string _FieldType, ByteStream _Value) { NetworkManager.AddRemoteNode(_NodeID); NetworkNodeRemote node = NetworkManager.RemoteNodes[_NodeID]; if (node.LocalDevices.ContainsKey(_ClassName)) { NetworkClassSlave parent = node.LocalDevices[_ClassName]; if (!parent.Fields.ContainsKey(_FieldName)) { object value = Converter.Read(_Value); NetworkClassSlave.Field field = new NetworkClassSlave.Field(_FieldName, parent, value); parent.Fields.Add(_FieldName, field); Log.Write("Networking", "Network field added {0}", field.ToString()); } else Log.Write("Networking", Log.Line.Type.Warning, "{0} already has a field : {1}", _ClassName, _FieldName); } else Log.Write("Networking", Log.Line.Type.Warning, "Cant find NetworkClass {0}", _ClassName); }
public abstract void WriteObject(object _Object, ByteStream _TargetStream);
internal void EnqueueCommand(ByteStream _Stream) { lock (CommandQueue) { CommandQueue.Enqueue(_Stream); } }