private void Connection_PeerConnected(NetPeer peer, string token) { Console.WriteLine(peer.EndPoint + " peer connected: " + token); peer.PayloadReceived += Peer_PayloadReceived; peer.NotificationReceived += Peer_NotificationReceived; }
static void Main(string[] args) { NetConfig.LatencySimulation = true; Connector client = new Connector("Sample1.1", false); Clock fastClock = new Clock(0.02f); Clock slowClock = new Clock(1.0f); fastClock.OnFixedUpdate += SendPayload; slowClock.OnFixedUpdate += SendNotification; Program.peer = client.Connect("127.0.0.1:42324"); while (true) { fastClock.Tick(); slowClock.Tick(); client.Update(); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); switch (key.Key) { case ConsoleKey.F1: client.Stop(); return; default: break; } } } }
public void TestReliableCleanup() { NetPeer netPeer = new NetPeer(null, "Token", false, 0); for (int i = 0; i < 20; i++) netPeer.QueueNotification(new NetEvent()); netPeer.OnReceiveCarrier(0, 10, (x) => x = null); // First sequence number is 1, so we should have 10 remaining Assert.AreEqual(10, netPeer.Outgoing.Count()); }
/// <summary> /// Sends a request to connect to a remote peer. /// </summary> internal SocketError SendConnect( NetPeer peer, string version) { lock (this.sendLock) { int length = NetEncoding.PackConnectRequest( this.sendBuffer, version, peer.Token); return this.TrySend(peer.EndPoint, this.sendBuffer, length); } }
/// <summary> /// Accepts a remote request and sends an affirmative reply. /// </summary> internal SocketError SendAccept( NetPeer peer) { lock (this.sendLock) { int length = NetEncoding.PackProtocol( this.sendBuffer, NetPacketType.Accept, 0, 0); return this.TrySend(peer.EndPoint, this.sendBuffer, length); } }
/// <summary> /// Notifies a peer that we are disconnecting. May not arrive. /// </summary> internal SocketError SendKick( NetPeer peer, NetCloseReason reason, byte userReason = 0) { // Skip the packet if it's a bad reason (this will cause error output) if (NetUtil.ValidateKickReason(reason) == NetCloseReason.INVALID) return SocketError.Success; lock (this.sendLock) { int length = NetEncoding.PackProtocol( this.sendBuffer, NetPacketType.Kick, (byte)reason, userReason); return this.TrySend(peer.EndPoint, this.sendBuffer, length); } }
/// <summary> /// Sends a generic pong packet. /// </summary> internal SocketError SendPong( NetPeer peer, byte pingSeq, byte drop) { lock (this.sendLock) { int length = NetEncoding.PackProtocol( this.sendBuffer, NetPacketType.Pong, pingSeq, drop); return this.TrySend(peer.EndPoint, this.sendBuffer, length); } }
private void Connection_PeerClosed(NetPeer peer, NetCloseReason reason, byte userKickReason, SocketError error) { Console.WriteLine("Peer closed due to reason: " + reason); }
/// <summary> /// Reads payload data from the given buffer. /// </summary> internal static bool ReadPayload( Func<NetEventType, NetPeer, NetEvent> eventFactory, NetPeer peer, byte[] buffer, int length, out ushort sequence, out NetEvent evnt) { evnt = null; // Read header (already know the type) sequence = NetEncoding.ReadU16(buffer, 1); int position = NetEncoding.PAYLOAD_HEADER_SIZE; ushort dataLength = (ushort)(length - position); if ((position + dataLength) > length) return false; // We're reading past the end of the packet data evnt = eventFactory.Invoke(NetEventType.Payload, peer); return evnt.ReadData(buffer, position, dataLength); ; }
/// <summary> /// Sends a scheduled notification message. /// </summary> internal SocketError SendNotifications( NetPeer peer) { lock (this.sendLock) { int packedLength = NetEncoding.PackCarrier( this.sendBuffer, peer.NotificationAck, peer.GetFirstSequence(), peer.Outgoing); int length = packedLength; return this.TrySend(peer.EndPoint, this.sendBuffer, length); } }
/// <summary> /// Immediately sends out a payload to a peer. /// </summary> internal SocketError SendPayload( NetPeer peer, ushort sequence, byte[] data, ushort length) { return this.controller.SendPayload(peer, sequence, data, length); }
/// <summary> /// Immediately sends out a disconnect message to a peer. /// </summary> internal void SendKick(NetPeer peer, byte reason) { this.controller.SendKick(peer, reason); }
/// <summary> /// Adds an outgoing notification to the controller processing queue. /// </summary> internal void QueueNotification( NetPeer peer, byte[] buffer, ushort length) { this.controller.QueueNotification(peer, buffer, length); }
private void Peer_NotificationReceived(NetPeer peer, byte[] data, int dataLength) { Console.WriteLine(peer.EndPoint + " got notification: \"" + Encoding.UTF8.GetString(data, 0, dataLength) + "\""); Console.WriteLine( peer.Traffic.Ping + "ms " + (peer.Traffic.LocalLoss * 100.0f) + "% " + (peer.Traffic.RemoteLoss * 100.0f) + "% " + (peer.Traffic.LocalDrop * 100.0f) + "% " + (peer.Traffic.RemoteDrop * 100.0f) + "%"); }
private static NetEvent CreateEvent(NetEventType type, NetPeer peer) { NetEvent evnt = new NetEvent(); evnt.Initialize(type, peer); return evnt; }
/// <summary> /// Reads a collection of notifications packed in the buffer. /// </summary> internal static bool ReadCarrier( Func<NetEventType, NetPeer, NetEvent> eventFactory, NetPeer peer, byte[] buffer, int length, out ushort notificationAck, out ushort notificationSeq, Queue<NetEvent> destinationQueue) { // Read header (already know the type) notificationAck = NetEncoding.ReadU16(buffer, 1); notificationSeq = NetEncoding.ReadU16(buffer, 3); int position = NetEncoding.CARRIER_HEADER_SIZE; // Validate int maxDataPack = NetEncoding.MAX_NOTIFICATION_PACK; if ((position > length) || ((length - position) > maxDataPack)) return false; // Read notifications while (position < length) { NetEvent evnt = eventFactory.Invoke(NetEventType.Notification, peer); int bytesRead = NetEncoding.ReadNotification(buffer, length, position, evnt); if (bytesRead < 0) return false; destinationQueue.Enqueue(evnt); position += bytesRead; } return true; }
/// <summary> /// Immediately sends out a payload to a peer. /// </summary> internal SocketError SendPayload( NetPeer peer, ushort sequence, byte[] data, ushort dataLength) { lock (this.sendLock) { int size = NetEncoding.PackPayload(this.sendBuffer, sequence, data, dataLength); return this.TrySend(peer.EndPoint, this.sendBuffer, size); } }
/// <summary> /// Sends a generic ping packet. /// </summary> internal SocketError SendPing( NetPeer peer, long curTime) { lock (this.sendLock) { int length = NetEncoding.PackProtocol( this.sendBuffer, NetPacketType.Ping, peer.GeneratePing(curTime), peer.GenerateLoss()); return this.TrySend(peer.EndPoint, this.sendBuffer, length); } }
private void Peer_PayloadReceived(NetPeer peer, byte[] data, int dataLength) { //Console.WriteLine(peer.EndPoint + " got payload: \"" + Encoding.UTF8.GetString(data, 0, dataLength) + "\""); }