public bool Connect(string host, int port) { if (m_Socket != null) { return(false); } m_Host = host; m_Port = port; try { m_HostEndPoint = IPUtils.GetHostEndPoint(m_Host, m_Port); if (m_HostEndPoint == null) { Close(); return(false); } m_IsRunning = true; m_Socket = new KCPSocket(0, 1); m_Socket.AddReceiveListener(m_HostEndPoint, OnReceive); } catch (Exception e) { Close(); return(false); } return(true); }
//================================================================================= //发送逻辑 //================================================================================= private void SendMessage(int dst, byte[] bytes, int len) { int dstPort = IPCConfig.GetIPCInfo(dst).port; IPEndPoint ep = IPUtils.GetHostEndPoint("127.0.0.1", dstPort); m_SystemSocket.SendTo(bytes, 0, len, SocketFlags.None, ep); }
/// <summary> /// 加入房间 /// </summary> /// <param name="ip"></param> /// <param name="port"></param> public void JoinRoom(string ip, int port) { m_roomAddress = IPUtils.GetHostEndPoint(ip, port); PlayerData pd = new PlayerData (); pd.userId = m_mainUserId; pd.name = m_mainUserName; byte[] customPlayerData = PBSerializer.NSerialize(pd); RPC(m_roomAddress, FSPRoom.RPC_JoinRoom, m_mainUserId, m_mainUserName, customPlayerData); }
//================================逻辑============================= public void JoinRoom(string ip, int port) { m_roomAddress = IPUtils.GetHostEndPoint(ip, port); PlayerData player = new PlayerData(); player.userID = m_mainUserId; player.userName = m_mainUserName; byte[] customPlayerData = PBSerializer.Serialize(player); RPC(m_roomAddress, RoomRPC.RPC_JoinRoom, m_mainUserId, m_mainUserName, customPlayerData); }
public void Test() { string str1 = "str1"; string str2 = "str2"; byte[] buff1 = UTF8Encoding.Default.GetBytes(str1); byte[] buff2 = UTF8Encoding.Default.GetBytes(str2); IPEndPoint target = IPUtils.GetHostEndPoint("127.0.0.1", 10002); RPC(target, "_RPC_Test", 1, 2, "abc", buff1, buff2); }
public bool Connect(string ip, int port) { if (m_SystemSocket != null) { Debuger.LogError("无法建立连接,需要先关闭上一次连接!"); return(false); } Debuger.Log("建立基础连接, host = {0}, port = {1}", ip, port); m_ip = ip; m_port = port; m_lastRecvTimestamp = (uint)TimeUtils.GetTotalMillisecondsSince1970(); try { m_RemoteEndPoint = IPUtils.GetHostEndPoint(m_ip, m_port); if (m_RemoteEndPoint == null) { Debuger.LogError("无法将Host解析为IP!"); Close(); return(false); } Debuger.Log("HostEndPoint = {0}", m_RemoteEndPoint.ToString()); //创建Socket Debuger.Log("创建Socket, AddressFamily = {0}", m_RemoteEndPoint.AddressFamily); m_SystemSocket = new Socket(m_RemoteEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp); m_SystemSocket.Bind(IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0)); m_IsRunning = true; m_ThreadRecv = new Thread(Thread_Recv) { IsBackground = true }; m_ThreadRecv.Start(); } catch (Exception e) { Debuger.LogError(e.Message + e.StackTrace); Close(); return(false); } return(true); }
public bool Connect(string host, int port) { if (m_Socket != null) { Debuger.LogError(LOG_TAG_MAIN, "Connect() 无法建立连接,需要先关闭上一次连接!"); return(false); } Debuger.Log(LOG_TAG_MAIN, "Connect() 建立基础连接, host = {0}, port = {1}", (object)host, port); m_Host = host; m_Port = port; try { //获取Host对应的IPEndPoint Debuger.Log(LOG_TAG_MAIN, "Connect() 获取Host对应的IPEndPoint"); m_HostEndPoint = IPUtils.GetHostEndPoint(m_Host, m_Port); if (m_HostEndPoint == null) { Debuger.LogError(LOG_TAG_MAIN, "Connect() 无法将Host解析为IP!"); Close(); return(false); } Debuger.Log(LOG_TAG_MAIN, "Connect() HostEndPoint = {0}", m_HostEndPoint.ToString()); m_IsRunning = true; //创建Socket Debuger.Log(LOG_TAG_MAIN, "Connect() 创建UdpSocket, AddressFamily = {0}", m_HostEndPoint.AddressFamily); m_Socket = new KCPSocket(0, 1); //m_Socket.Connect(m_HostEndPoint); m_Socket.AddReceiveListener(m_HostEndPoint, OnReceive); } catch (Exception e) { Debuger.LogError(LOG_TAG_MAIN, "Connect() " + e.Message + e.StackTrace); Close(); return(false); } return(true); }
public bool Connect(string host, int port) { if (m_Socket != null) { MyLogger.LogError(LOG_TAG_MAIN, "Connect()", " cannot build connection, please shut down last connection!"); return(false); } MyLogger.Log(LOG_TAG_MAIN, "Connect() start initial connection, host = {0}, port = {1}", port.ToString(), (object)host); m_Host = host; m_Port = port; try { //get Host's IPEndPoint MyLogger.Log(LOG_TAG_MAIN, "Connect() get Host's IPEndPoint"); m_HostEndPoint = IPUtils.GetHostEndPoint(m_Host, m_Port); if (m_HostEndPoint == null) { MyLogger.LogError(LOG_TAG_MAIN, "Connect()", "cannot convert host name to IP!"); Close(); return(false); } MyLogger.Log(LOG_TAG_MAIN, "Connect() HostEndPoint = {0}", m_HostEndPoint.ToString()); isRunning = true; //create a Socket MyLogger.Log(LOG_TAG_MAIN, "Connect() create UdpSocket, AddressFamily = {0}", "" + m_HostEndPoint.AddressFamily); m_Socket = new KCPSocket(0, 1); //m_Socket.Connect(m_HostEndPoint); m_Socket.AddReceiveListener(m_HostEndPoint, OnReceive); } catch (Exception e) { MyLogger.LogError(LOG_TAG_MAIN, "Connect() ", e.Message + e.StackTrace); Close(); return(false); } return(true); }
public void Connect(string ip, int port) { Debuger.Log("ip:{0}, port:{1}", ip, port); m_ip = ip; m_port = port; m_RemoteEndPoint = IPUtils.GetHostEndPoint(ip, port); m_SystemSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_SystemSocket.Bind(new IPEndPoint(IPAddress.Any, bindPort)); m_Kcp = new KCP(0, HandleKcpSend); m_Kcp.NoDelay(1, 10, 2, 1); m_Kcp.WndSize(128, 128); Connected = true; m_ThreadRecv = new Thread(Thread_Recv) { IsBackground = true }; m_ThreadRecv.Start(); }
public bool Connect(string host, int port) { if (m_Socket != null) { Debuger.LogError(LOG_TAG_MAIN, "Connect() 无法建立连接,需要先关闭上一次连接!"); return(false); } Debuger.Log(LOG_TAG_MAIN, "Connect() 尝试建立基础连接, host = {0}, port = {1}", host, port); try { //获取Host对应的IPEndPoint Debuger.Log(LOG_TAG_MAIN, "Connect() 获取Host对应的IPEndPoint"); m_HostEndPoint = IPUtils.GetHostEndPoint(host, port); if (m_HostEndPoint == null) { Debuger.LogError(LOG_TAG_MAIN, "Connect() 无法将Host解析为IP!"); Close(); return(false); } Debuger.Log(LOG_TAG_MAIN, "Connect() HostEndPoint = {0}", m_HostEndPoint.ToString()); // 创建socket m_Socket = new KCPSocket(0, 1); m_Socket.Connect(m_HostEndPoint); m_Socket.AddReceiveListener(m_HostEndPoint, OnReceive); } catch (System.Exception e) { Debuger.LogError(LOG_TAG_MAIN, "Connect() " + e.Message + e.StackTrace); Close(); return(false); } return(true); }