Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using System.Net.Sockets;
- using System.Net;
- using System;
- using System.Text;
- public class MinimalNet : MonoBehaviour
- {
- public bool isHost, isOnline;
- public List<IPEndPoint> clients;
- IPEndPoint ownPoint, hostPoint, broadPoint;
- public string ownAddr;
- public int ownPort = 35000, hostPort;
- public string hostAddr;
- public List<string> lastReceivedMSGs;
- UdpClient udp;
- string message;
- byte[] sendBuff, recvBuff;
- object hostLock;
- private void Start() {
- GetOwnAddr();
- hostAddr = ownAddr;
- hostPort = ownPort;
- sendBuff = new byte[128];
- recvBuff = new byte[128];
- hostLock = new object();
- lastReceivedMSGs = new List<string>();
- }
- void ClearData() {
- if ( clients == null ) clients = new List<IPEndPoint>(); else clients.Clear();
- }
- private void OnGUI() {
- GUILayout.BeginHorizontal();
- GUILayout.Label( "Own Port" );
- string port = GUILayout.TextField( ownPort.ToString() );
- int.TryParse( port, out ownPort );
- GUILayout.EndHorizontal();
- if ( !isHost ) {
- GUILayout.BeginHorizontal();
- GUILayout.Label( "Host Address" );
- hostAddr = GUILayout.TextField( hostAddr );
- GUILayout.EndHorizontal();
- GUILayout.BeginHorizontal();
- GUILayout.Label( "Host Port" );
- string hPort = GUILayout.TextField( hostPort.ToString() );
- int.TryParse( hPort, out hostPort );
- GUILayout.EndHorizontal();
- }
- GUILayout.Box( string.Format( "Own Address: {0}:{1}", ownAddr, ownPort ) );
- GUILayout.Box( string.Format( "Online: {0} Host: {1}", isOnline, isHost ) );
- if (!isOnline && GUILayout.Button( isHost ? "Become Client" : "Become Host")) { ToggleHost(); }
- GUILayout.Space( 30 );
- if ( GUILayout.Button( isOnline ? "Stop Online!" : "Go Online!" ) ) { ToggleOnline(); }
- GUILayout.Space( 30 );
- GUILayout.BeginHorizontal();
- message = GUILayout.TextField(message); if (GUILayout.Button("Send")) { SendUDPMessage( message ); message = ""; } if (isHost && isOnline && GUILayout.Button("Ping Broad")) { PingBroad(); }
- GUILayout.EndHorizontal();
- GUILayout.Space( 20 );
- string chat = "";
- for (int i = 0; i < lastReceivedMSGs.Count; i++ ) {
- chat += string.Format("{0}\n", lastReceivedMSGs[i]);
- }
- GUILayout.Box( chat );
- }
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- void StartUDP(int port) {
- GetOwnAddr();
- hostPoint = new IPEndPoint( IPAddress.Parse( hostAddr ), hostPort );
- broadPoint = new IPEndPoint( IPAddress.Parse( "192.168.255.255" ), hostPort );
- udp = new UdpClient(port);
- lastReceivedMSGs.Add( "Started UDP: " + ownAddr + ":" + port );
- udp.BeginReceive( isHost ? new AsyncCallback( ReceiveAsHost ) : new AsyncCallback( ReceiveAsClient ), udp );
- }
- void ToggleOnline() {
- isOnline = !isOnline;
- ClearData();
- if (isOnline) {
- StartUDP( ownPort );
- } else {
- if (udp != null) udp.Close();
- }
- }
- IPEndPoint sender;
- void ReceiveAsHost( IAsyncResult ar ) {
- lock ( hostLock ) {
- try {
- recvBuff = udp.EndReceive( ar, ref sender );
- if ( !sender.Address.Equals( ownPoint.Address ) || sender.Port != ownPort) {
- if ( !clients.Contains( sender ) ) {
- clients.Add( sender );
- lastReceivedMSGs.Add( "Added Client: " + sender.Address + ":" + sender.Port );
- }
- lastReceivedMSGs.Add( "Recv: " + Encoding.Unicode.GetString( recvBuff ) );
- }
- } catch ( Exception e ) {
- Debug.LogWarning( e.Message );
- }
- }
- udp.BeginReceive( new AsyncCallback( ReceiveAsHost ), udp );
- }
- void ReceiveAsClient( IAsyncResult ar ) {
- try {
- recvBuff = udp.EndReceive( ar, ref sender );
- lastReceivedMSGs.Add( "Recv: " + Encoding.Unicode.GetString( recvBuff ) );
- } catch (Exception e) {
- Debug.LogWarning( e.Message );
- }
- udp.BeginReceive( new AsyncCallback( ReceiveAsClient ), udp );
- }
- void SendUDPMessage( string txt ) {
- if ( !isOnline ) return;
- txt = string.Format( "{0:0.000}: {1}", Time.timeSinceLevelLoad, txt );
- sendBuff = Encoding.Unicode.GetBytes( txt );
- if (isHost) {
- for (int i = 0; i < clients.Count; i++ ) {
- udp.Send( sendBuff, sendBuff.Length, clients[i] );
- }
- } else {
- udp.Send( sendBuff, sendBuff.Length, hostPoint );
- txt += " to " + hostPoint.Address;
- }
- lastReceivedMSGs.Add( txt );
- }
- void PingBroad() {
- sendBuff = Encoding.Unicode.GetBytes( string.Format( "{0:0.000}: {1}", Time.timeSinceLevelLoad, "PING!" ) );
- udp.Send( sendBuff, sendBuff.Length, broadPoint );
- }
- //----------------------------------------------------------------------------------------------------------------
- void ToggleHost() {
- isHost = !isHost;
- ClearData();
- }
- void GetOwnAddr() {
- string hostName = Dns.GetHostName();
- IPHostEntry hostEn = Dns.GetHostEntry(hostName);
- ownPoint = null;
- for ( int i = 0; i < hostEn.AddressList.Length; i++ ) {
- if ( hostEn.AddressList[i].AddressFamily == AddressFamily.InterNetwork ) {
- print( hostEn.AddressList[i].ToString() );
- }
- }
- for ( int i = hostEn.AddressList.Length - 1; i >= 0; i-- ) {
- if ( hostEn.AddressList[i].AddressFamily == AddressFamily.InterNetwork ) {
- ownPoint = new IPEndPoint( hostEn.AddressList[i], ownPort );
- break;
- }
- }
- if (ownPoint != null) {
- ownAddr = ownPoint.Address.ToString();
- }
- }
- private void Update() {
- if ( lastReceivedMSGs.Count > 5 ) lastReceivedMSGs.RemoveAt( 0 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement