public virtual void Broadcast(T data, int port) { this.Setup(SocketRole.Broadcast); var epTo = SocketData.Make(); epTo.endPoint.Address = IPAddress.Broadcast; epTo.endPoint.Port = port; var byteData = this.OnSerialize(data); this.SendByte(epTo, byteData); }
protected void ReceiveCallback(IAsyncResult ar) { try { var stateFrom = ar.AsyncState as ReceiveState; Assert.IsTrue(stateFrom == this.receiveState); EndPoint epFrom = stateFrom.remote.endPoint; int bytesReceived = stateFrom.remote.socket.EndReceiveFrom(ar, ref epFrom); var ipFrom = epFrom as IPEndPoint; stateFrom.remote.endPoint = ipFrom; if (bytesReceived > 0) { if (DebugLog) { Debug.LogFormat("RECV: {0}: {1}", epFrom.ToString(), bytesReceived); } T data = this.OnDeserialize(stateFrom.buffer, bytesReceived); this.OnMessage(stateFrom.remote, data); } if (this.connections[Connection.Incoming].Exists(c => c.endPoint.Address.Equals(ipFrom.Address)) == false) { this.connections[Connection.Incoming].Add(SocketData.Make(ipFrom)); if (DebugLog) { LogTool.LogFormat("Add in connection: {0}", LogLevel.Verbose, LogChannel.Network, ipFrom.ToString()); } } epFrom = this.receiveState.remote.endPoint; stateFrom.remote.socket.BeginReceiveFrom(this.receiveState.buffer, 0, this.receiveState.buffer.Length, SocketFlags.None, ref epFrom, this.ReceiveCallback, this.receiveState); } catch (Exception e) { if (!(e is ObjectDisposedException)) { LogTool.Log(e.ToString()); var se = e as SocketException; if (se != null) { LogTool.Log(se.ErrorCode.ToString()); LogTool.Log(se.Message); } } } }
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()); } }
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()); } }