Since raw sockets are thread safe, we use a global socket singleton between the two threads for the sake of convenience.
예제 #1
0
        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;
        }
예제 #2
0
 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();
     }
 }
예제 #3
0
        internal NetReceiver(NetSocket socket)
        {
            this.socket        = socket;
            this.receiveBuffer = new byte[NetConfig.SOCKET_BUFFER_SIZE];

#if DEBUG
            this.inQueue = new NetDelay();
#endif
        }
예제 #4
0
        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
        }
예제 #5
0
파일: NetSender.cs 프로젝트: ztxyzu/MiniUDP
        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
        }
예제 #6
0
        /// <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;
                        }
                    }
                }
            }
        }
예제 #7
0
 internal NetSender(NetSocket socket)
 {
     this.socket = socket;
 }
예제 #8
0
 internal NetReceiver(NetSocket socket)
 {
     this.socket = socket;
 }