internal NetController( string version, bool acceptConnections) { connectIn = new NetPipeline <NetPeer>(); notificationIn = new NetPipeline <NetEvent>(); eventOut = new NetPipeline <NetEvent>(); eventPool = new NetPool <NetEvent>(); peers = new Dictionary <IPEndPoint, NetPeer>(); timer = new Stopwatch(); socket = new NetSocket(); sender = new NetSender(socket); receiver = new NetReceiver(socket); reusableQueue = new Queue <NetEvent>(); reusableList = new List <NetPeer>(); reusableBuffer = new byte[NetConfig.SOCKET_BUFFER_SIZE]; nextTick = 0; nextLongTick = 0; isStarted = false; isRunning = false; this.acceptConnections = acceptConnections; this.version = version; }
internal void Update() { if (NetConfig.LatencySimulation) { for (int i = 0; i < NetConfig.MaxPacketReads; i++) { IPEndPoint source; int length; SocketError result = this.socket.TryReceive( out source, this.receiveBuffer, out length); if (NetSocket.Succeeded(result) == false) { return; } this.inQueue.Enqueue(source, this.receiveBuffer, length); } } else { this.inQueue.Clear(); } }
internal NetReceiver(NetSocket socket) { this.socket = socket; this.receiveBuffer = new byte[NetConfig.SOCKET_BUFFER_SIZE]; #if DEBUG this.inQueue = new NetDelay(); #endif }
internal NetSender(NetSocket socket) { this.sendLock = new object(); this.sendBuffer = new byte[NetConfig.SOCKET_BUFFER_SIZE]; this.socket = socket; #if DEBUG this.outQueue = new NetDelay(); #endif }
/// <summary> /// Polls the socket and receives all pending packet data. /// </summary> private void ReadPackets() { for (int i = 0; i < NetConfig.MaxPacketReads; i++) { IPEndPoint source; byte[] buffer; int length; SocketError result = this.receiver.TryReceive(out source, out buffer, out length); if (NetSocket.Succeeded(result) == false) { return; } NetPacketType type = NetEncoding.GetType(buffer); if (type == NetPacketType.Connect) { // We don't have a peer yet -- special case this.HandleConnectRequest(source, buffer, length); } else { NetPeer peer; if (this.peers.TryGetValue(source, out peer)) { switch (type) { case NetPacketType.Accept: this.HandleConnectAccept(peer, buffer, length); break; case NetPacketType.Kick: this.HandleKick(peer, buffer, length); break; case NetPacketType.Ping: this.HandlePing(peer, buffer, length); break; case NetPacketType.Pong: this.HandlePong(peer, buffer, length); break; case NetPacketType.Carrier: this.HandleCarrier(peer, buffer, length); break; case NetPacketType.Payload: this.HandlePayload(peer, buffer, length); break; } } } } }
internal NetSender(NetSocket socket) { this.socket = socket; }
internal NetReceiver(NetSocket socket) { this.socket = socket; }