public bool Disconnect() { if (m_socket != null) { lock (lockObj) { // 소켓 닫기. foreach (Socket socket in m_socket) { socket.Shutdown(SocketShutdown.Both); socket.Close(); } m_socket.Clear(); m_socket = null; } // 접속 종료를 알립니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Disconnect; state.result = NetEventResult.Success; m_handler(state); } } m_isConnected = false; Debug.Log("TransportTcp::Disconnect called."); return(true); }
void DispatchSend() { if (m_socket == null) { return; } try { // 송신 처리. //if (m_socket.Poll(0, SelectMode.SelectWrite)) { byte[] buffer = new byte[m_packetSize]; int sendSize = m_sendQueue.Dequeue(ref buffer, buffer.Length); while (sendSize > 0) { foreach (Socket socket in m_socket) { socket.Send(buffer, sendSize, SocketFlags.None); } sendSize = m_sendQueue.Dequeue(ref buffer, buffer.Length); } //} } catch { if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.SendError; state.result = NetEventResult.Failure; m_handler(state); } } }
public virtual bool Disconnect(int node) { if (node < 0) { return(false); } T transport = m_transports[node]; if (transport != null) { transport.Disconnect(); LeaveSession(node); } if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Disconnect; state.result = NetEventResult.Success; m_handler(node, state); } return(true); }
public void OnEventHandling(NetEventState state) { switch (state.type) { case NetEventType.Connect: if (m_transport.IsServer()) { AddMessage(ref m_message[1], "콩장수가 입장했습니다."); } else { AddMessage(ref m_message[0], "두부장수와 이야기할 수 있습니다."); } break; case NetEventType.Disconnect: if (m_transport.IsServer()) { AddMessage(ref m_message[0], "콩장수가 나갔습니다."); } else { AddMessage(ref m_message[1], "콩장수가 나갔습니다."); } break; } }
// public bool Connect(string ipAddress, int port) { if (m_socket == null) { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Debug.Log("Create new socket."); } try { m_localEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); m_isRequested = true; Debug.Log("Connection success"); } catch { m_isRequested = false; Debug.Log("Connect fail"); } string str = "TransportUDP connect:" + m_isRequested.ToString(); Debug.Log(str); if (m_handler != null) { // 접속 결과를 알립니다. NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = (m_isRequested == true)? NetEventResult.Success : NetEventResult.Failure; m_handler(this, state); Debug.Log("event handler called"); } return(m_isRequested); }
// 접속 요청 처리. public bool Connect(string ipAddress, int port) { try { IPAddress addr = IPAddress.Parse(ipAddress); m_endPoint = new IPEndPoint(addr, port); m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } catch { return(false); } // 접속을 알립니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(state); } m_icConnected = true; Debug.Log("TransportUdp::Connect called."); return(true); }
public void OnEventHandling(NetEventState state) { switch (state.type) { case NetEventType.Connect: if (transport.IsServer()) { Debug.Log("서버입장"); } else { Debug.Log("고객"); } break; case NetEventType.Disconnect: if (transport.IsServer()) { Debug.Log("서버가 나갔습니다."); } else { Debug.Log("고객이 나갔습니다."); } break; } }
// public virtual void OnEventHandling(ITransport itransport, NetEventState state) { int node = itransport.GetNodeId(); string str = "SignalNotification[" + node + "] :" + state.type.ToString() + " state:" + state.ToString(); Debug.Log(str); do { if (m_transports.ContainsKey(node) == false) { // 찾지 못했을 때. string msg = "NodeId[" + node + "] is not founded."; Debug.Log(msg); break; } switch (state.type) { case NetEventType.Connect: break; case NetEventType.Disconnect: LeaveSession(node); break; } } while (false); // 이벤트 통지 함수가 등록되어 있으면 콜백합니다. if (m_handler != null) { m_handler(node, state); } }
// 대기 시작. public bool StartServer(int port, int connectionNum) { Debug.Log("StartServer called.!"); // 리스닝 소켓을 생성합니다. try { // 소켓을 생성합니다. m_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 사용할 포트 번호를 할당합니다. m_listener.Bind(new IPEndPoint(IPAddress.Any, port)); // 대기합니다. m_listener.Listen(connectionNum); } catch { Debug.Log("StartServer fail"); return(false); } m_isServer = true; if (m_handler != null) { // 접속 결과를 통지합니다. NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(state); Debug.Log("event handler called"); } return(LaunchThread()); }
public void Disconnect() { //접속 종료 isConnected = false; if (socket != null) { socket.Shutdown(SocketShutdown.Both); /* Receive 받기에 대한 Socket을 비활성화합니다. 이 필드는 상수입니다. * Send 보내기에 대한 Socket을 비활성화합니다. 이 필드는 상수입니다. * Both 보내기와 받기 모두에 대한 Socket을 비활성화합니다. 이 필드는 상수입니다. */ socket.Close(); socket = null; if (handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Disconnect; state.result = NetEventResult.Success; handler(state); } } }
public override void AcceptClient() { if ((m_listener != null) && m_listener.Poll(0, SelectMode.SelectRead)) { Debug.Log("[TCP]AcceptClient"); // 접속 요청이 왔습니다. Socket socket = m_listener.Accept(); int node = -1; try { Debug.Log("[TCP]Create transport"); TransportTCP transport = new TransportTCP(); transport.Initialize(socket); transport.transportName = "serverSocket"; Debug.Log("[TCP]JoinSession"); node = JoinSession(transport); } catch { Debug.Log("[TCP]Connect fail."); return; } if (node >= 0 && m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(node, state); } Debug.Log("[TCP]Connected from client. [port:" + m_port + "]"); } }
// ================================================================ // public void NetEventHandling() { if (m_network == null) { return; } NetEventState state = m_network.GetEventState(); if (state == null) { return; } switch (state.type) { case NetEventType.Connect: Debug.Log("[CLIENT]connect event handling:" + state.node); break; case NetEventType.Disconnect: Debug.Log("[CLIENT]disconnect event handling:" + state.node); if (this.step < STEP.SERVER_START) { m_errorMessage = "서버와 연결이 끊어졌습니다.\n\n버튼을 누르세요."; this.step = STEP.ERROR; } break; } }
// ================================================================ // public void NetEventHandling() { if (network == null) { return; } NetEventState state = network.GetEventState(); if (state == null) { return; } switch (state.type) { case NetEventType.Connect: Debug.Log("[CLIENT]connect event handling:" + state.node); break; case NetEventType.Disconnect: Debug.Log("[CLIENT]disconnect event handling:" + state.node); DisconnectEventProc(state.node); break; } }
//------------------------------ // EventDelegate //------------------------------ public void EventCallback(NetEventState state) { Debug.Log("EventCallback!!!"); switch (state.type) { case NetEventType.Connect: if (state.result == NetEventResult.Success) { debugText.text = "Connected!!"; sendButton.interactable = true; disconnectButton.interactable = true; } else { debugText.text = "Connection Failed..."; connectButton.interactable = true; } break; case NetEventType.Disconnect: debugText.text = "Disconnect..."; connectButton.interactable = true; sendButton.interactable = false; disconnectButton.interactable = false; break; default: break; } }
public void SetReceiveData(byte[] data, int size, IPEndPoint endPoint) { string str = System.Text.Encoding.UTF8.GetString(data).Trim('\0'); if (str.Contains(m_requestData)) { // 접속 요청 패킷 수신. if (m_isConnected == false && m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(this, state); //IPEndPoint ep = m_localEndPoint; //Debug.Log("[UDP]Connected from client. [address:" + ep.Address.ToString() + " port:" + ep.Port + "]"); } m_isConnected = true; m_remoteEndPoint = endPoint; m_timeOutTicker = DateTime.Now; } else if (size > 0) { m_recvQueue.Enqueue(data, size); } }
//------------------------------- // Public Logic //------------------------------- public bool Connect(string address, int port) { bool ret = false; try { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.NoDelay = true; socket.Connect(address, port); ret = LaunchThread(); } catch { socket = null; } if (ret == true) { isConnected = true; } else { isConnected = false; } if (handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = (isConnected == true) ? NetEventResult.Success : NetEventResult.Failure; handler(state); } return isConnected; }
public void OnEventHandling(NetEventState state) { switch (state.type) { case NetEventType.Connect: if (m_transport.IsServer()) { AddMessage(ref m_message[1], "だいずやさんがやってきました"); } else { AddMessage(ref m_message[0], "とうふやさんとおでゃべりできます"); } break; case NetEventType.Disconnect: if (m_transport.IsServer()) { AddMessage(ref m_message[0], "だいずやさんがかえっていきました"); } else { AddMessage(ref m_message[1], "とうふやさんとがかえっていきました"); } break; } }
public void ServerSignalNotification(int node, NetEventState state) { string str = "Node:" + node + " type:" + state.type.ToString() + " State:" + state.ToString(); Debug.Log("ServerSignalNotification called"); Debug.Log(str); switch (state.type) { case NetEventType.Connect: { NodeInfo info = new NodeInfo(); info.node = node; m_nodes.Add(node, info); if (m_handler != null) { m_handler(node, state); } } break; case NetEventType.Disconnect: { if (m_handler != null) { m_handler(node, state); } m_nodes.Remove(node); } break; } }
public void OnEventHandling(NetEventState state) { switch (state.type) { case NetEventType.Connect: if (m_transport.IsServer()) { AddMessage(ref m_messages, "[System] Client Joinned!"); } else { AddMessage(ref m_messages, "[System] Start Chat!"); } break; case NetEventType.Disconnect: if (m_transport.IsServer()) { AddMessage(ref m_messages, "[System] Server Leaved"); } else { AddMessage(ref m_messages, "[System] Client leaved"); } break; } }
// 끊기. public void Disconnect() { Debug.Log("소켓 끊음"); m_isConnected = false; if (m_socket != null) { // 소켓 클로즈. m_socket.Shutdown(SocketShutdown.Both); m_socket.Close(); m_socket = null; } // 끊기를 통지합니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Disconnect; state.result = NetEventResult.Success; m_handler(state); } // 스레드 강제종료 시킴(2020 02 01 추가) m_thread.Abort(); }
public void OnEventHandling(NetEventState state) { if (m_handler != null) { m_handler(state); } }
// 이벤트 발생 시의 콜백 함수. public void EventCallback(NetEventState state) { switch (state.type) { case NetEventType.Disconnect: if (progress < GameProgress.Result && isGameOver == false) { progress = GameProgress.Disconnect; } break; } }
/// <summary> /// 연결 상태인지 확인하는 콜백함수 /// </summary> /// <param name="state"></param> public void EventCallback(NetEventState state) { switch (state.type) { case NetEventType.Disconnect: if (m_eState < eState.E_STATE_RESULT && m_bGameOver == false) { m_eState = eState.E_STATE_DISCONNECT; } break; } }
// 접속. public bool Connect(string address, int port) { Debug.Log("TransportTCP connect called."); if (m_listener != null) { return(false); } bool ret = false; try { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_socket.NoDelay = true; m_socket.Connect(address, port); ret = LaunchThread(); } catch { m_socket = null; } if (ret == true) { m_isConnected = true; Debug.Log("Connection success."); } else { m_isConnected = false; Debug.Log("Connect fail"); } if (m_handler != null) { // 접속 결과를 알립니다. NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = (m_isConnected == true) ? NetEventResult.Success : NetEventResult.Failure; m_handler(state); Debug.Log("event handler called"); } return(m_isConnected); }
// 이벤트 발생 시 콜백 함수. public void EventCallback(NetEventState state) { switch (state.type) { case NetEventType.Disconnect: if (m_gameState < GameState.EndGame && m_isGameOver == false) { m_gameState = GameState.Disconnect; } break; } }
// public bool Connect(string ipAddress, int port) { if (m_socket == null) { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Debug.Log("Create new socket."); } try { // 호스트 이름을 가져온다. string hostname = Dns.GetHostName(); // 호스트 이름에서 IP 주소를 가져온다. IPAddress[] adrList = Dns.GetHostAddresses(hostname); foreach (IPAddress address in adrList) { if (address.AddressFamily == AddressFamily.InterNetwork) { m_localEndPoint = new IPEndPoint(address, m_serverPort); break; } } m_remoteEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); m_isRequested = true; Debug.Log("Connection success"); Debug.Log("Local:" + m_localEndPoint.Address.ToString()); Debug.Log("Remote:" + m_remoteEndPoint.Address.ToString() + ":" + m_remoteEndPoint.Port); } catch { m_isRequested = false; Debug.Log("Connect fail"); } string str = "TransportUDP connect:" + m_isRequested.ToString(); Debug.Log(str); if (m_handler != null) { // 접속 결과를 알린다. NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = (m_isRequested == true)? NetEventResult.Success : NetEventResult.Failure; m_handler(this, state); Debug.Log("event handler called"); } m_keepAliveTicker = DateTime.Now; m_isFirst = true; return(m_isRequested); }
// 이벤트 통지 함수 등록. public NetEventState GetEventState() { if (m_eventQueue.Count == 0) { return(null); } NetEventState state = m_eventQueue[0]; m_eventQueue.RemoveAt(0); return(state); }
private void EventHandler(int node, NetEventState state) { switch (state.type) { case NetEventType.Connect: break; case NetEventType.Disconnect: Setting setting = Setting.GetInstance(); setting.state = State.Disconnect; break; } }
// 클라리언트 접속. void AcceptClient() { if (listener != null && listener.Poll(0, SelectMode.SelectRead)) { // 클라이언트가 접속했습니다. socket = listener.Accept(); isConnected = true; NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; handler(state); Debug.Log("Connected from client."); } }
// ================================================================ // public void OnEventHandling(NetEventState state) { switch (state.type) { case NetEventType.Connect: Debug.Log("[SERVER]NetEventType.Connect"); break; case NetEventType.Disconnect: Debug.Log("[SERVER]NetEventType.Disconnect"); DisconnectClient(); break; } }
// ================================================================ // public void OnEventHandling(NetEventState state) { switch (state.type) { case NetEventType.Connect: Debug.Log("connect event"); guestConnected = true; break; case NetEventType.Disconnect: guestDisconnected = true; break; } }
// クライアントとの接続. void AcceptClient() { if (m_listener != null && m_listener.Poll(0, SelectMode.SelectRead)) { // クライアントから接続されました. m_socket = m_listener.Accept(); m_isConnected = true; NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(state); Debug.Log("Connected from client."); } }
public void OnEventHandling(int node, NetEventState state) { string str = "Node:" + node + " type:" + state.type.ToString() + " State:" + state.type + "[" + state.result + "]"; Debug.Log("OnEventHandling called"); Debug.Log(str); switch (state.type) { case NetEventType.Connect: { MemberList member = new MemberList(); member.node = node; member.endPoint = network_.GetEndPoint(node); m_members.Add(node, member); } break; case NetEventType.Disconnect: { if (m_members.ContainsKey(node)) { int roomId = m_members[node].roomId; if (rooms_.ContainsKey(roomId)) { for (int i = 0; i < rooms_[roomId].members.Length; ++i) { if (rooms_[roomId].members[i] == node) { rooms_[roomId].members[i] = -1; break; } } } m_members.Remove(node); } } break; } }
// public bool Connect(string ipAddress, int port) { if (m_socket == null) { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Debug.Log("Create new socket."); } try { m_localEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); m_isRequested = true; Debug.Log("Connection success"); } catch { m_isRequested = false; Debug.Log("Connect fail"); } string str = "TransportUDP connect:" + m_isRequested.ToString(); Debug.Log(str); if (m_handler != null) { // 접속 결과를 알립니다. NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = (m_isRequested == true)? NetEventResult.Success : NetEventResult.Failure; m_handler(this, state); Debug.Log("event handler called"); } return m_isRequested; }
void AcceptClient() { if (m_isCommunicating == false && m_listener != null && m_listener.Poll(0, SelectMode.SelectRead)) { // 클라이언트가 접속했습니다. m_isCommunicating = true; // 통신 시작 시각을 기록. m_timeOutTicker = DateTime.Now; // 접속을 알립니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(state); } } }
// public bool Connect(string ipAddress, int port) { if (m_socket == null) { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Debug.Log("Create new socket."); } try { // 호스트 이름을 가져온다. string hostname = Dns.GetHostName(); // 호스트 이름에서 IP 주소를 가져온다. IPAddress[] adrList = Dns.GetHostAddresses(hostname); foreach (IPAddress address in adrList) { if (address.AddressFamily == AddressFamily.InterNetwork) { m_localEndPoint = new IPEndPoint(address, m_serverPort); break; } } m_remoteEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); m_isRequested = true; Debug.Log("Connection success"); Debug.Log("Local:" + m_localEndPoint.Address.ToString()); Debug.Log("Remote:" + m_remoteEndPoint.Address.ToString() + ":" + m_remoteEndPoint.Port); } catch { m_isRequested = false; Debug.Log("Connect fail"); } string str = "TransportUDP connect:" + m_isRequested.ToString(); Debug.Log(str); if (m_handler != null) { // 접속 결과를 알린다. NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = (m_isRequested == true)? NetEventResult.Success : NetEventResult.Failure; m_handler(this, state); Debug.Log("event handler called"); } m_keepAliveTicker = DateTime.Now; m_isFirst = true; return m_isRequested; }
public void Disconnect() { m_isConnected = false; if (m_socket != null) { // 소켓 닫기. m_socket.Shutdown(SocketShutdown.Both); m_socket.Close(); m_socket = null; } // 절단을 통지합니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Disconnect; state.result = NetEventResult.Success; m_handler(this, state); } }
public void OnEventHandling(NetEventState state) { switch (state.type) { case NetEventType.Connect: isConnected = true; Debug.Log("[NetworkController] Connected."); break; case NetEventType.Disconnect: isConnected = false; Debug.Log("[NetworkController] Disconnected."); break; } }
void DispatchSend() { if (m_socket == null) { return; } try { // 송신처리. if (m_socket.Poll(0, SelectMode.SelectWrite)) { byte[] buffer = new byte[m_packetSize]; int sendSize = m_sendQueue.Dequeue(ref buffer, buffer.Length); while (sendSize > 0) { try { int sendResult = m_socket.Send(buffer, sendSize, SocketFlags.None); if (sendResult < 0) { Debug.Log("Transport send error send size:" + sendResult); } } catch (SocketException e) { Debug.Log("Transport send error:" + e.Message); if (m_handler != null) { NetEventState state = new NetEventState(); state.node = m_nodeId; state.type = NetEventType.SendError; state.result = NetEventResult.Failure; m_handler(this, state); } } sendSize = m_sendQueue.Dequeue(ref buffer, buffer.Length); } } } catch { return; } }
void DispatchReceive() { if (m_socket == null) { return; } // 수신 처리. try { foreach (Socket socket in m_socket) { if (socket.Poll(0, SelectMode.SelectRead)) { byte[] buffer = new byte[m_packetSize]; int recvSize = socket.Receive(buffer, buffer.Length, SocketFlags.None); Debug.Log("TransportTcp Receive data [size:" + recvSize + "][port:" + m_port +"]"); if (recvSize == 0) { // 끊기. Disconnect(); } else if (recvSize > 0) { m_recvQueue.Enqueue(buffer, recvSize); } } } } catch { if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.ReceiveError; state.result = NetEventResult.Failure; m_handler(state); } } }
public bool Disconnect() { if (m_socket != null) { // 소켓 닫기. m_socket.Close(); m_socket = null; // 접속 끊김을 알립니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Disconnect; state.result = NetEventResult.Success; m_handler(state); } } m_isStarted = false; m_isConnected = false; Debug.Log("TransportTcp::Disconnect called."); return true; }
// ================================================================ // public void OnEventHandling(NetEventState state) { switch (state.type) { case NetEventType.Connect: Debug.Log("[SERVER]NetEventType.Connect"); SendGameSyncInfo(); break; case NetEventType.Disconnect: Debug.Log("[SERVER]NetEventType.Disconnect"); DisconnectClient(); break; } }
// 접속. public bool Connect(string address, int port) { Debug.Log("TransportTCP connect called."); if (m_listener != null) { return false; } bool ret = false; try { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_socket.NoDelay = true; m_socket.Connect(address, port); ret = LaunchThread(); } catch { m_socket = null; } if (ret == true) { m_isConnected = true; Debug.Log("Connection success."); } else { m_isConnected = false; Debug.Log("Connect fail"); } if (m_handler != null) { // 접속 결과를 알립니다. NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = (m_isConnected == true) ? NetEventResult.Success : NetEventResult.Failure; m_handler(state); Debug.Log("event handler called"); } return m_isConnected; }
// 클라이언트와 접속. void AcceptClient() { if (m_listener != null && m_listener.Poll(0, SelectMode.SelectRead)) { // 클라이언트로부터 접속되었습니다. m_socket = m_listener.Accept(); m_isConnected = true; if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(state); } Debug.Log("Connected from client."); } }
public void Disconnect() { Debug.Log("Disconnect()"); isConnected = false; if (socket != null) { try { socket.Shutdown(SocketShutdown.Both); } catch { } socket.Close(); socket = null; if (thread.IsAlive) { try { threadLoop = false; thread.Abort(); } catch { } } if (handler != null) { Debug.Log("handler is not null."); NetEventState state = new NetEventState(); state.type = NetEventType.Disconnect; state.result = NetEventResult.Success; handler(state); } else { Debug.Log("handler is null..."); } } }
public void Disconnect() { //접속 종료 isConnected = false; if(socket!=null) { socket.Shutdown(SocketShutdown.Both); /* Receive 받기에 대한 Socket을 비활성화합니다. 이 필드는 상수입니다. Send 보내기에 대한 Socket을 비활성화합니다. 이 필드는 상수입니다. Both 보내기와 받기 모두에 대한 Socket을 비활성화합니다. 이 필드는 상수입니다. */ socket.Close(); socket = null; if(handler!=null) { NetEventState state = new NetEventState(); state.type = NetEventType.Disconnect; state.result = NetEventResult.Success; handler(state); } } }
// public bool Connect(string address, int port) { Debug.Log("TransportUdp::Connect called.[Port:" + port + "]"); try { if (m_socket == null) { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } m_socket.Connect(address, port); } catch { Debug.Log("TransportUdp::Connect failed."); return false; } bool ret = true; if (m_isStarted == false) { ret = LaunchThread(); if (ret == true) { m_isConnected = true; } } // 접속을 알립니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(state); } Debug.Log("TransportUdp::Connect success."); return ret; }
// ================================================================ // public void OnEventHandling(NetEventState state) { switch (state.type) { case NetEventType.Connect: // 접속 이벤트는 이 함수가 등록되기 전에 발생하는 경우가 있으므로. // 잘 못 가져오는 일이 있습니다. // 이 때문에 접속 상태를 감시해서 접속 이벤트를 발생 시킵니다. break; case NetEventType.Disconnect: Debug.Log("Guest disconnected."); request_disconnet_event = true; break; } }
void AcceptClient() { if (m_isConnected == false && m_socket != null && m_socket.Poll(0, SelectMode.SelectRead)) { // 클라이언트로부터 접속되었습니다. m_isConnected = true; // 통신 시작 시각을 기록. m_ticker = DateTime.Now; // 접속을 알립니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(state); } } }
//SelectMode //Socket.Poll 메서드에 대한 폴링 모드를 정의합니다. /* * SelectError 오류 상태 모드입니다. SelectRead 읽기 상태 모드입니다. SelectWrite 쓰기 상태 모드입니다. */ void AcceptClient() { if(listener!=null && listener.Poll(0,SelectMode.SelectRead)) { //z클라이언트에 접속됨 socket = listener.Accept(); isConnected = true; if (handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; handler(state); } Debug.Log("Connected from client."); } }
void AcceptClient() { Console.WriteLine("AcceptClient."); if (m_listener != null && m_listener.Poll(0, SelectMode.SelectRead)) { // 클라이언가 접속했습니다. Socket socket = m_listener.Accept(); m_socket.Add(socket); m_isConnected = true; if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(state); } Debug.Log("Connected from client. [port:" + m_port + "]"); } }
// 접속 요청 처리. public bool Connect(string ipAddress, int port) { try { IPAddress addr = IPAddress.Parse(ipAddress); m_endPoint = new IPEndPoint(addr, port); m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } catch { return false; } // 접속을 알립니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(state); } m_icConnected = true; Debug.Log("TransportUdp::Connect called."); return true; }
// public bool Connect(string address, int port) { Debug.Log("Transport connect called"); if (m_socket != null) { return false; } try { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_socket.Connect(address, port); m_socket.NoDelay = true; m_port = port; m_isConnected = true; Debug.Log("Connection success"); } catch (SocketException e) { m_socket = null; m_isConnected = false; Debug.Log("Connect fail"); Debug.Log(e.ToString()); } string str = "TransportTCP connect:" + m_isConnected.ToString(); Debug.Log(str); if (m_handler != null) { // 접속 결과를 통지합니다. NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = (m_isConnected == true)? NetEventResult.Success : NetEventResult.Failure; m_handler(this, state); Debug.Log("event handler called"); } return m_isConnected; }
public bool Disconnect() { if (m_socket != null) { lock (lockObj) { // 소켓 닫기. foreach (Socket socket in m_socket) { socket.Shutdown(SocketShutdown.Both); socket.Close(); } m_socket.Clear(); m_socket = null; } // 접속 종료를 알립니다. if (m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Disconnect; state.result = NetEventResult.Success; m_handler(state); } } m_isConnected = false; Debug.Log("TransportTcp::Disconnect called."); return true; }
public void SetReceiveData(byte[] data, int size, IPEndPoint endPoint) { string str = System.Text.Encoding.UTF8.GetString(data).Trim('\0'); if (str.Contains(m_requestData)) { // 접속 요청 패킷 수신. if (m_isConnected == false && m_handler != null) { NetEventState state = new NetEventState(); state.type = NetEventType.Connect; state.result = NetEventResult.Success; m_handler(this, state); IPEndPoint ep = m_localEndPoint; Debug.Log("[UDP]Connected from client. [address:" + ep.Address.ToString() + " port:" + ep.Port + "]"); } m_isConnected = true; m_timeOutTicker = DateTime.Now; } else if (size > 0) { m_recvQueue.Enqueue(data, size); } }