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()); } }
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; }
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()); } }
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); } }
public override void OnMessage(SocketData socket, ImageFile.FileData data) { this.fileQueue.Enqueue(data); }
public virtual void OnError(SocketData socket) { }
public virtual void OnDisconnect(SocketData socket) { }
public virtual void OnMessage(SocketData socket, T data) { }
public override void OnMessage(SocketData socket, UDPServer.Data data) { deltaQueue.Enqueue(data); }
public override void OnMessage(SocketData socket, UDPServer.GPUData data) { //Task.Run(() => UDPServer.DebugReport(data)); deltaQueue.Enqueue(data); }