Beispiel #1
0
        public void SendHeartbeat()
        {
            MessageServer msg = CreateMessage <MessageServer>();

            msg.method       = MessageServer.ServerMethod.HEARTBEAT;
            latencyStartTime = NetworkTime.Milliseconds();
            SendMessage(server.GetEndpoint(), msg);
        }
Beispiel #2
0
        public override MessageServer ConnectToServer(string userName)
        {
            MessageServer message = base.ConnectToServer(userName);

            //SendMessage(server.GetEndpoint(), message);
            latencyStartTime = NetworkTime.Milliseconds();

            SendReliableMessage(server.GetEndpoint(), message);
            return(message);
        }
Beispiel #3
0
        private void OnStreamDataContent(object sender, NetworkMessage e)
        {
            NetworkMessageStream stream = (NetworkMessageStream)e;

            Console.WriteLine("Command: " + stream.command);
            string result = Encoding.UTF8.GetString(stream.byteData);

            latency = NetworkTime.Milliseconds() - latencyStartTime;
            Console.WriteLine("Stream took " + (latency) + " ms");
            Console.WriteLine("Text: " + result);
        }
Beispiel #4
0
        /**
         * Send Internal
         * Thread triggers send to remote point
         */
        public void SendFromThread(NetworkPacket packet)
        {
            try
            {
                packet.byteSent = socket4.SendTo(packet.ByteBuffer, packet.byteLength, SocketFlags.None, packet.remoteEndPoint);
                lock (this)
                {
                    packetSendCount++;
                    //if (packetSendCount % 500 == 0)
                    //{
                    //    Console.WriteLine("Sent Packets: " + packetSendCount);
                    //}
                }

                //if (packet.networkIPType == NetworkIPType.IPv4)
                //    packet.byteSent = socket4.SendTo(packet.ByteBuffer, packet.byteLength, SocketFlags.None, packet.remoteEndPoint);
                //else
                //    packet.byteSent = socket6.SendTo(packet.ByteBuffer, packet.byteLength, SocketFlags.None, packet.remoteEndPoint);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            bool hasReliable = false;

            for (int i = 0; i < packet.messages.Count; i++)
            {
                tempSendMessage = packet.messages[i];
                if (tempSendMessage.header.sendType == SendType.Message &&
                    tempSendMessage.header.isReliable)
                {
                    tempSendMessage.header.sentTime = NetworkTime.Milliseconds();
                    tempSendMessage.header.retryCount++;

                    lock (thread.RELIABLEQUEUE)
                    {
                        thread.RELIABLEQUEUE.Enqueue(packet);
                    }

                    hasReliable = true;
                }
            }

            if (!hasReliable)
            {
                channel.FreeMessage(packet.messages[0]);
                Free(packet);
            }

            //if (OnSend != null) //notify any event listeners
            //    OnSend.Invoke(this, packet);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            NetworkTime.Start();

            bool isServer = false;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].ToLower().Equals("--server"))
                {
                    isServer = true;
                }
                if (args[i].ToLower().StartsWith("--connectto"))
                {
                    if (i < args.Length - 1)
                    {
                        //connectToAddress = args[++i];
                    }
                }
                Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
            }

            InterfaceTrafficWatch.TestNetwork();

            if (isServer)
            {
                RunServer();
            }
            else
            {
                RunClient();
            }



            //NetworkRSAEncryption enc = new NetworkRSAEncryption();
            //enc.Test();

            //NetworkServer server = RunServer();
            //RunClient();
            //Thread t = new Thread(Test1);
            //t.Start();
            //t = new Thread(Test2);
            //t.Start();
            //Thread.Sleep(6000);

            //Console.WriteLine("Server PacketPool Count = " + server.socket.thread.PACKETPOOL.packetCount);
            //Console.WriteLine("Server Message Count = " + server.channel.MESSAGEPOOL.messageCount);
            ////Console.WriteLine("Server PacketPool Count = " + server.protocol.socket.thread.PACKETPOOL.packetCount);
            //Console.WriteLine("Server Receive Cnt: " + server.receiveCnt);
            //Console.WriteLine("Server bandwidth sent: " + server.socket.thread.sentBufferSize);
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
        }
Beispiel #6
0
        public void MainThread()
        {
            long difftime  = 0;
            long startTime = NetworkTime.Milliseconds();
            long curTime   = 0;

            while (true)
            {
                curTime  = NetworkTime.Milliseconds();
                difftime = curTime - startTime;
                if (difftime < NetworkConfig.NetworkSendRate)
                {
                    continue;
                }

                startTime = curTime;

                NetworkFrame();
            }
        }
Beispiel #7
0
 public void CalculateLatency()
 {
     latency = NetworkTime.Milliseconds() - latencyStartTime;
     // Console.WriteLine("Ping = " + (latency) + " ms");
     latencyStartTime = NetworkTime.Milliseconds();
 }
Beispiel #8
0
        public void ReliableThread()
        {
            NetworkPacket packet = null;
            int queueCount = 0;
            long difftime;
            bool isAcknowledged;
            long curtime = NetworkTime.Milliseconds();
            bool hasFailed = false;
            bool shouldResend = false;
            NetworkMessage message;
            while (true)
            {


                lock (RELIABLEQUEUE)
                {
                    queueCount = RELIABLEQUEUE.Count;
                    
                    if( queueCount > 0)
                    packet = RELIABLEQUEUE.Dequeue();
                }

                if (queueCount == 0)
                {
                    Thread.Sleep(NetworkConfig.ThreadReliableSleepTime);
                    continue;
                }
                    

                curtime = NetworkTime.Milliseconds();
                hasFailed = false;
                shouldResend = false;

                //for (int i = 0; i < packet.messages.Count; i++)
                {
                    message = packet.messages[0];

                    lock (ACKNOWLEDGED)
                    {
                        isAcknowledged = ACKNOWLEDGED.Remove(message.header.ackkey);
                    }

                    if (isAcknowledged)
                    {
                        packet.socket.Free(packet);
                        continue;
                    }

                    difftime = curtime - message.header.sentTime;
                    if (difftime > packet.retryDelay)
                    {
                        if (message.header.retryCount > NetworkConfig.SocketReliableRetryAttempts)
                        {
                            if (message.header.channelType == ChannelType.Server)
                            {
                                packet.socket.Failed(NetworkErrorType.ErrorConnectToServer, "Unable to connect to server.", packet);
                            }
                            else if (message.header.channelType == ChannelType.STUN)
                            {
                                packet.socket.Failed(NetworkErrorType.ErrorNoResponseSTUN, "Unable to connect to server.", packet);
                            }

                            failedReliableCount++;
                            packet.socket.Failed(NetworkErrorType.ErrorReliableFailed, "Failed to deliver " + message.header.retryCount + " packets (" + failedReliableCount + ") times.", packet);

                            hasFailed = true;
                            packet.socket.Free(packet);
                            continue;
                        }

                        shouldResend = true;
                        Console.WriteLine("Resending " + message.header.sequence + ", attempt #" + message.header.retryCount);
                        packet.socket.Send(packet);
                        continue;
                    }
                }


                if (hasFailed)
                {

                }
                else if (shouldResend)
                {

                }

                lock (RELIABLEQUEUE)
                {
                    RELIABLEQUEUE.Enqueue(packet);
                }


                //Thread.Sleep(MIN_RELIABLE_SLEEP_TIME);
            }
        }