private void ConnetAsync() { IPEndPoint remote = NetUtility.GetIPEndPoint(setting.host, setting.port); if (null == remote) { NetDebug.Log("[TCPClient] remote can not be null."); return; } socket = new Socket(remote.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.Blocking = setting.blocking; socket.NoDelay = setting.noDelay; SocketAsyncEventArgs saea = new SocketAsyncEventArgs(); saea.Completed += new EventHandler <SocketAsyncEventArgs>(OnSAEACompleted); saea.AcceptSocket = socket; saea.RemoteEndPoint = remote; bool willRaiseEvent = socket.ConnectAsync(saea); if (!willRaiseEvent) { ConnectAsyncCallback(saea); } }
private KCPStateObject InitKCPObject(EndPoint remote) { lock (lockObject) { KCPStateObject kcp = null; string remoteKey = RemotePoint2Key(remote); if (map_remote2KCP.TryGetValue(remoteKey, out kcp)) { return(kcp); } NetDebug.Log("[S] InitKCPObject, remote:{0}", remote); kcp = new KCPStateObject(); kcp.Initialize(remote, setting.kcp); kcp.remote = remote; kcp.sendHandle = OnKCPSend; kcp.receiveHandle = OnKCPReceive; kcp.timeoutCallback = OnTimeoutCallback; map_remote2KCP.Add(remoteKey, kcp); return(kcp); } }
public void Connect() { NetDebug.Log("[TCPClient] Try to Connect {0}:{1}.", setting.host, setting.port); remote = NetUtility.FormatIPEndPoint(setting.host, setting.port); Reset(); ConnetAsync(); }
private void OnTimeoutCallback(KCPStateObject kcpObject) { NetDebug.Log("[OnTimeoutCallback] {0}", kcpObject.remote); if (null != kcpObject) { Close(kcpObject.remote); } }
private void Debug(object obj, params object[] args) { lock (_debugMutex) { Console.ForegroundColor = IsServer ? ConsoleColor.Cyan : ConsoleColor.Green; NetDebug.Log(obj.ToString(), args); Console.ResetColor(); } }
public void Send(string remote, byte[] data) { CSConnection connection = null; if (!map_remote2Connection.TryGetValue(remote, out connection)) { NetDebug.Log("[TCPServer] try send, but the remote:{0} is null.", remote); return; } connection.Send(data); }
public void Send(byte[] data, int offset, int count) { if (null != _kcpObject) { _kcpObject.Send(data, offset, count); } else { NetDebug.Log("KCP connection send error, the kcp object is not be null."); } }
public void Close() { NetDebug.Log("UDPDiscoverClient.Close"); if (client != null) { client.Close(); client.Dispose(); client = null; } if (sendThread != null) { sendThread.Abort(); sendThread = null; } }
private void OnConnectCallback(bool connect, Socket connectSocket) { NetDebug.Log("[TCPClient] connect to {0}:{1} result:{2}.", setting.host, setting.port, connect); if (connect) { ReceiveAsync(); onConnectCallback(null); return; } onConnectFailedCallback(null); }
public void Listen() { NetDebug.Log("[TCPServer] Listen."); IPEndPoint remote = NetUtility.GetIPEndPoint(setting.host, setting.port); if (null == remote) { NetDebug.Log("[TCPServer] ip port can not be null."); return; } socket = new Socket(remote.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); socket.Blocking = setting.blocking; socket.Bind(remote); socket.Listen(setting.backlog); AcceptAsync(); }
public void Connect(string address, int port) { Initialize(); IsServer = false; State = UDPConnectionState.OPENING; remoterPoint = NetUtility.GetIPEndPoint(address, port); if (null == remoterPoint) { NetDebug.Log("[TCPClient] remote can not be null."); return; } socket = new Socket(remoterPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp); socket.Connect(remoterPoint); ReceiveFromAsync(remoterPoint); //socket.Blocking = setting.blocking; //socket.NoDelay = setting.noDelay; Send(remoterPoint, UDPPacketType.SYN); }