Esempio n. 1
0
        public virtual void StartReceive(int port = 0)
        {
            this.receiveState.remote        = SocketData.Make(port);
            this.receiveState.remote.socket = this.socket;

            this.Setup(SocketRole.Receiver, this.receiveState.remote);

            EndPoint epFrom = this.receiveState.remote.endPoint;

            try
            {
                this.socket.BeginReceiveFrom(this.receiveState.buffer, 0, this.receiveState.buffer.Length, SocketFlags.None, ref epFrom, this.ReceiveCallback, this.receiveState);
            }
            catch (Exception e)
            {
                //check flag
                Debug.Log(e.ToString());
            }
        }
Esempio n. 2
0
        public void Setup(SocketRole role, SocketData data = null)
        {
            if (this.roleState[role])
            {
                return;
            }
            try
            {
                switch (role)
                {
                case SocketRole.Sender:
                {
                    //connect is optional
                    //socket.Connect(IPAddress.Parse(address), port);
                }
                break;

                case SocketRole.Receiver:
                {
                    this.socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
                    //this.socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
                    Assert.IsNotNull(data);
                    this.socket.Bind(data.endPoint);

                    if (DebugLog)
                    {
                        LogTool.Log("Start Receiver on " + data.endPoint);
                    }
                }
                break;

                case SocketRole.Broadcast:
                {
                    this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
                    this.socket.EnableBroadcast = true;
                    //this.socket.Bind(this.socketConfigure.endPoint);
                }
                break;
                }
            }
            catch (SocketException e) { LogTool.Log(data.endPoint.ToString() + " " + e.ToString()); }
            this.roleState[role] = true;
        }
Esempio n. 3
0
        IEnumerator Broadcast()
        {
            var socket  = SocketData.Make("localhost", 12345);
            var socket1 = SocketData.Make("localhost", 12346);
            //socket.endPoint.Address = IPAddress.Broadcast;
            //this.socket.Setup(UDPSocket<GPUData>.SocketRole.Broadcast);
            var data = new GPUData();

            while (true)
            {
                data.deltaTime  = Time.deltaTime;
                data.serverTime = Serilization.ConvertFrom2019();
                //this.socket.Send(socket, data);
                //this.socket.Send(socket1, data);
                this.socket.Broadcast(data, 12347);
                this.socket.Broadcast(data, 12348);
                yield return(new WaitForEndOfFrame());
            }
        }
Esempio n. 4
0
        protected void SendByte(SocketData epTo, byte[] byteData)
        {
            epTo.socket = this.socket;

            Assert.IsNotNull(epTo);
            Assert.IsNotNull(byteData);
            Assert.IsTrue(byteData.Length > 0);
            Assert.IsTrue(byteData.Length < 64 * 1024, "Data length " + byteData.Length + " exceeds max 64k");
            Assert.IsNotNull(epTo.socket);

            if (byteData.Length > 64 * 1024)
            {
                LogTool.Log("Data exceeded 64K", LogLevel.Warning);
                return;
            }

            if (epTo.reachable)
            {
                try
                {
                    this.socket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epTo.endPoint, this.SendCallback, epTo);
                }
                catch (Exception e)
                {
                    LogTool.Log(epTo.endPoint.ToString());
                    LogTool.Log(e.ToString());
                }
                finally
                {
                    byteData = null;
                }
            }
            else
            {
                LogTool.Log(epTo.endPoint.ToString() + " is unreachable", LogLevel.Warning);
            }
        }
Esempio n. 5
0
 public override void OnMessage(SocketData socket, ImageFile.FileData data)
 {
     this.fileQueue.Enqueue(data);
 }
Esempio n. 6
0
 public virtual void OnError(SocketData socket)
 {
 }
Esempio n. 7
0
 public virtual void OnDisconnect(SocketData socket)
 {
 }
Esempio n. 8
0
 public virtual void OnMessage(SocketData socket, T data)
 {
 }
Esempio n. 9
0
 public override void OnMessage(SocketData socket, UDPServer.Data data)
 {
     deltaQueue.Enqueue(data);
 }
Esempio n. 10
0
        public override void OnMessage(SocketData socket, UDPServer.GPUData data)
        {
            //Task.Run(() => UDPServer.DebugReport(data));

            deltaQueue.Enqueue(data);
        }