Example #1
0
        /// <summary>
        /// Accept an object from the server to be run on the client
        /// </summary>
        /// <param name="controlCode"></param>
        /// <param name="bytes"></param>
        private void RunOnClient(NetworkControlCode controlCode, byte[] bytes)
        {
            object o = NetSerialize.Deserialize(bytes);

            //If object is a ping, respond immediately, to minimize lag
            //This should be replaced with raw data and custom control codes that are faster to deserialize to help minimize lag from object deserialization
            if (o is Ping p)
            {
                float time = NetTime.now;
                p.clientNumber = clientNumber;
                p.clientTime   = time;
                udp.Send(NetworkControlCode.runOnServer, NetSerialize.Serialize(p));
            }
            else
            {
                lock (objectQueue)
                {
                    objectQueue.Enqueue(o);
                }

#if UNITY_EDITOR
                AddToObjectList("FromServer: " + o.ToString());
#endif
            }
        }
Example #2
0
        /// <summary>
        /// Initialize the UDP client, and send connection request to the server
        /// </summary>
        /// <param name="serverIP"></param>
        /// <returns></returns>
        private UdpClient InitializeUDPClient(string serverIP)
        {
            LogFile.WriteLineToLog(LogFile.FileNames.client, "UDP Connecting");
            //create udp listener to get port
            UdpClient udpClient = new UdpClient();

            udpClient.Connect(serverIP, NetworkServer.UDP_PORT);

            LogFile.WriteLinesToLog(LogFile.FileNames.client, new string[] {
                "UDP Connected",
                "Local End Point: " + udpClient.Client.LocalEndPoint.ToString(),
                "Remote End Point: " + udpClient.Client.RemoteEndPoint.ToString()
            }
                                    );

            UdpConnectionIn udpIn = new UdpConnectionIn(udpClient);

            //create UDPOut connection
            UdpConnectionOut udpOut = new UdpConnectionOut(udpClient);

            //Encapsulate udp connections
            udp = new UdpConnection(udpIn, udpOut);

            udp.Send(NetworkControlCode.runOnServer, NetSerialize.Serialize(new NetworkServer.UdpConnectServerEvent()));

            LogFile.WriteLineToLog(LogFile.FileNames.client, "UDP Connection Request Sent");
            return(udpClient);
        }
Example #3
0
        public static (NetworkEventType eventType, object o) ReadObject(NetworkStream stream)
        {
            NetworkEventType eventType = (NetworkEventType)ReadInt(stream);

            byte[] arr = ReadArr(stream);
            object o   = NetSerialize.Deserialize(arr);

            return(eventType, o);
        }
Example #4
0
        /// <summary>
        /// Serialize an object and send it to the server over TCP
        /// </summary>
        /// <param name="controlCode"></param>
        /// <param name="o"></param>
        private void _SendObjectToServerTCP(NetworkControlCode controlCode, object o)
        {
            LogFile.WriteLineToLog(LogFile.FileNames.clientObjectLog, "Sending Object To Server TCP: " + o.GetType().ToString());

#if UNITY_EDITOR
            objectList.Enqueue("ToServer: " + o.ToString());
#endif
            byte[] bytes = NetSerialize.Serialize(o);
            instance.tcp.Send(controlCode, bytes);
        }
Example #5
0
        /// <summary>
        /// Send an object to the server over UDP
        /// </summary>
        /// <param name="controlCode"></param>
        /// <param name="o"></param>
        public void _SendObjectToServerUdp(NetworkControlCode controlCode, object o)
        {
            LogFile.WriteLineToLog(LogFile.FileNames.clientObjectLog, "Sending Object To Server UDP: " + o.GetType().ToString());

#if UNITY_EDITOR
            lock (objectList)
            {
                AddToObjectList("ToServerUdp: " + o.ToString());
            }
#endif

            byte[] arr = NetSerialize.Serialize(o);
            instance.udp.Send(controlCode, arr);
        }
Example #6
0
 public static void WriteObject(NetworkStream stream, NetworkEventType type, object o)
 {
     byte[] byteArr = NetSerialize.Serialize(o);
     WriteArr(stream, type, byteArr);
 }