public static void Test() { RPCDispatcher dispatch = new RPCDispatcher(); dispatch.Functions[typeof(Serialization.TestStruct)] = (RPCData data) => { Serialization.TestStruct obj = (Serialization.TestStruct)data.DeserializedObject; Console.WriteLine("RPC Test Successful."); }; // add a rpc call Serialization.TestStruct serTest = new Serialization.TestStruct(); serTest.a = 10; serTest.b = 20; serTest.c = "hello"; MemoryStream memStream = new MemoryStream(100); RPCStream.AddCall(memStream, serTest); // execute rpc calls MemoryStream memStreamIn = new MemoryStream(memStream.ToArray()); RPCData template = new RPCData(); Stopwatch st = new Stopwatch(); st.Start(); for (int i = 0; i < 100000; i++) { memStreamIn.Position = 0; try { RPCStream.Execute(memStreamIn, dispatch, template); } catch { } } st.Stop(); Console.WriteLine("RPC performance: " + st.ElapsedMilliseconds); }
public NetWrapOutgoingMessage CreateRPCPacket(params object[] objs) { NetWrapOutgoingMessage msg = new NetWrapOutgoingMessage(); List <byte> packet = new List <byte>(); packet.AddRange(RPCHeader); packet.Add(255); int count = 0; foreach (var obj in objs) { var stream = new MemoryStream(2048); RPCStream.AddCall(stream, obj); var array = stream.ToArray(); if (array.Length > 512) { packet.AddRange(BitConverter.GetBytes(-array.Length)); packet.AddRange(NetCompressByteArray.Wrap(array)); } else { packet.AddRange(BitConverter.GetBytes(array.Length)); packet.AddRange(array); } count++; } // set count to correct count (255 -> count) packet[4] = (byte)count; msg.Message = packet.ToArray(); msg.MessageCount = packet.Count; msg.MessageOffset = 0; return(msg); }
public bool Update() { if (m_valid == false) { return(false); } i_checkStartENetThread(); using (MonitorLock.CreateLock(m_outgoingPacketListLocks[m_currentOutgoingPacketListIndex])) { using (MonitorLock.CreateLock(m_outgoingPacketListLocks[(m_currentOutgoingPacketListIndex + 1) % 2])) { if (m_outgoingPacketLists[(m_currentOutgoingPacketListIndex + 1) % 2].Count > 0 && m_outgoingPacketLists[m_currentOutgoingPacketListIndex].Count == 0) { m_currentOutgoingPacketListIndex = (m_currentOutgoingPacketListIndex + 1) % 2; } } } using (MonitorLock.CreateLock(m_enetEventListLocks[m_currentENetEventListIndex])) { if (m_enetEventLists[m_currentENetEventListIndex].Count > 0) { m_currentENetEventListIndex = (m_currentENetEventListIndex + 1) % 2; m_enetEventLists[m_currentENetEventListIndex].Clear(); } } using (MonitorLock.CreateLock(m_enetEventListLocks[m_currentENetEventListIndex])) { foreach (var enetEvent in m_enetEventLists[(m_currentENetEventListIndex + 1) % 2]) { if (enetEvent.Type == ENet.EventType.None) { continue; } NetWrapConnection connection; if (enetEvent.Peer.UserData == IntPtr.Zero) { var peer = enetEvent.Peer; connection = new NetWrapConnection { ConnectionId = connectionIDCounter++, Peer = enetEvent.Peer }; m_connections.TryAdd(connection.ConnectionId, connection); peer.UserData = GCHandle.ToIntPtr(GCHandle.Alloc(connection)); } else { var gcHandle = GCHandle.FromIntPtr(enetEvent.Peer.UserData); connection = (NetWrapConnection)gcHandle.Target; m_connections.GetOrAdd(connection.ConnectionId, connection); } switch (enetEvent.Type) { case ENet.EventType.Connect: { // set timeouts enetEvent.Peer.SetTimeouts(5, 30000, 90000); NumConnections++; var wrappedStatus = new NetWrapConnectionStatus() { Status = NetWrapConnectionStatusEnum.Connected, EndPoint = enetEvent.Peer.GetRemoteAddress(), ConnectionId = connection.ConnectionId }; OnStatusChanged(this, wrappedStatus); } break; case ENet.EventType.Disconnect: { NumConnections--; var wrappedStatus = new NetWrapConnectionStatus() { Status = NetWrapConnectionStatusEnum.Disconnected, EndPoint = enetEvent.Peer.GetRemoteAddress(), ConnectionId = connection.ConnectionId }; OnStatusChanged(this, wrappedStatus); } break; case ENet.EventType.Receive: byte[] data = enetEvent.Packet.GetBytes(); if (data.Length >= 5 && data[0] == RPCHeader[0] && data[1] == RPCHeader[1] && data[2] == RPCHeader[2] && data[3] == RPCHeader[3]) { #if RELEASE // Needs to be #if-ed out in debug to let errors fall into the debugger try { #endif var rpcDataTemplate = new RPCData(); var msg = new NetWrapIncomingMessage(); msg.SenderConnectionId = connection.ConnectionId; rpcDataTemplate.OriginalMessage = msg; int offset = 5; for (int i = 0; i < data[4]; i++) { int blockbytes = BitConverter.ToInt32(data, offset); offset += 4; if (blockbytes < 0) { // compressed RPC call blockbytes = -blockbytes; var buffer = NetCompressByteArray.Unwrap(data, offset); var stream = new MemoryStream(buffer); RPCStream.Execute(stream, RpcDispatcher, rpcDataTemplate); } else { // uncompressed RPC call var stream = new MemoryStream(data, offset, blockbytes); RPCStream.Execute(stream, RpcDispatcher, rpcDataTemplate); } offset += blockbytes; } #if RELEASE } catch (Exception ex) { try { using (var stream = System.IO.File.AppendText("ignored-net-errors.txt")) stream.WriteLine(DateTime.Now.ToString() + " : " + ex.ToString()); } catch {} Console.WriteLine(ex.ToString()); break; } #endif } else { NetWrapIncomingMessage msg = new NetWrapIncomingMessage() { Message = data, MessageCount = data.Length, MessageOffset = 0, SenderConnectionId = connection.ConnectionId }; OnData(this, msg); } enetEvent.Packet.Dispose(); break; default: Console.WriteLine(enetEvent.Type); break; } } m_enetEventLists[(m_currentENetEventListIndex + 1) % 2].Clear(); } return(true); }