Ejemplo n.º 1
0
        /*
         * Function to deserialize an array of byte into an object of type Container
         *      - Initialize binary formatter & memory stream.
         *      - Deserialize container (in array of byte format) and return it as object.
         * */
        public NetworkContainer DeserializeContainer(byte[] arrayOfBytes)
        {
            MemoryStream memoryStream = new MemoryStream();
            BinaryFormatter binaryFormater = new BinaryFormatter();
            NetworkContainer obj = new NetworkContainer();

            try
            {
                memoryStream.Write(arrayOfBytes, 0, arrayOfBytes.Length);
                memoryStream.Seek(0, SeekOrigin.Begin);
                obj = (NetworkContainer)binaryFormater.Deserialize(memoryStream);

            }
            catch (Exception e) { Console.WriteLine(e); }

            return obj;
        }
Ejemplo n.º 2
0
        private System.Net.Sockets.UdpClient _Socket; // The socket (for sending data / receiving data, depends on the context)

        #endregion Fields

        #region Constructors

        /*
         * Constructor of Network
         *      - Initialize variables.
         *      - Bind sockets.
         * */
        public Network(ref NetworkContainer container, Boolean isHost, Boolean forSending, Delegate del)
        {
            _Container = container;
            _IsHost = isHost;
            _HostIpAdress = "";
            _Continue = true;

            _Delegate = del;
            _NetworkDelegate = new networkDelegate(AbortConnection);

            _Socket = new System.Net.Sockets.UdpClient();
            _Socket.EnableBroadcast = false;

            if (forSending)
            {
                _Socket.Client.Bind(new System.Net.IPEndPoint(0, 5000));
                _EndPoint = new System.Net.IPEndPoint(0, 5000);
            }
            else if (!forSending)
            {
                _Socket.Client.Bind(new System.Net.IPEndPoint(0, 5001));
                _EndPoint = new System.Net.IPEndPoint(0, 5001);
            }
        }
Ejemplo n.º 3
0
        /*
         * Function initializing network elements
         *      - Initialize network objects/variables.
         *      - Initialize network threads.
         * */
        private void InitializeNetwork(Boolean isHost)
        {
            _SendingContainer = new NetworkContainer();
            _ReceptionContainer = new NetworkContainer();

            _Multiplayer = true;
            _EndGameHasBeenInvoked = false;
            _WallHasBeenAdded = false;

            _Sending = new Network(ref _SendingContainer, isHost, true, _CommandDispatcherDel);
            _Reception = new Network(ref _ReceptionContainer, isHost, false, _CommandDispatcherDel);

            InitializeNetworkThreads();
        }
Ejemplo n.º 4
0
        /*
         * Function called when _ReceptionThread starts
         *      - Receive & deserialize data.
         *      - Call command dispatcher.
         * */
        public void ReceiveLoop()
        {
            while (_Continue)
            {
                if (_IsHost)
                {
                    try
                    {
                        _Buffer = _Socket.Receive(ref _EndPoint);
                    }
                    catch (Exception e) { Console.WriteLine(e); }

                    _Container = _Container.DeserializeContainer(_Buffer);
                    Console.WriteLine("Server has received : " + _Container.Get_Msg() + " from : " + _EndPoint.Address.ToString().Split(':')[0]);
                }

                if (!_IsHost)
                {
                    try
                    {
                        _Buffer = _Socket.Receive(ref _EndPoint);
                    }
                    catch (Exception e) { Console.WriteLine(e); }

                    _Container = _Container.DeserializeContainer(_Buffer);
                    Console.WriteLine("Client has received : " + _Container.Get_Msg() + " from : " + _EndPoint.Address.ToString().Split(':')[0]);
                }

                _Delegate.DynamicInvoke();

                System.Threading.Thread.Sleep(35);
            }
        }