private void _onBeginConnect(IAsyncResult ar) { if (m_stopSignal) { return; } var socket = (Socket)ar.AsyncState; try { socket.EndConnect(ar); server = new SoketinUser() { _ipAddress = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), _port = ((IPEndPoint)socket.RemoteEndPoint).Port, _socket = socket, }; _execute((Action)onEvent.OnServerConnected); if (!m_stopSignal) { m_socket.BeginReceive(m_buffer, 0, m_buffer.Length, 0, new AsyncCallback(_onBeginRecieve), m_socket); } } catch (Exception e) { Console.WriteLine(e); _execute((Action <Exception, object>)onEvent.OnError, e, null); if (autoReconnect) { socket.BeginConnect(m_ip, m_port, new AsyncCallback(_onBeginConnect), socket); } } }
public void Send(SoketinUser client, byte[] data) { var user = m_clients.Find((c) => { return(c._socket == client._socket); }); if (user != null) { var packedData = SoketinUtility.PackRawData(data); client._socket.BeginSend(packedData, 0, packedData.Length, 0, new AsyncCallback(_onBeginSend), client); } }
private void _onBeginDisconnect(IAsyncResult ar) { var socket = (Socket)ar.AsyncState; try { socket.EndDisconnect(ar); _execute((Action)onEvent.OnServerDisconnected); socket.Shutdown(SocketShutdown.Both); } catch (Exception e) { Console.WriteLine(); _execute((Action <Exception, object>)onEvent.OnError, e, null); } finally { socket.Close(); _execute((Action)onEvent.OnServiceStop); server = null; } }
private void _onBeginAccept(IAsyncResult ar) { m_signalAccept.Set(); if (m_signalStop) { return; } var client = ((Socket)ar.AsyncState).EndAccept(ar); var newClient = new SoketinUser() { _id = m_userID, _ipAddress = ((IPEndPoint)client.RemoteEndPoint).Address.ToString(), _port = ((IPEndPoint)client.RemoteEndPoint).Port, _socket = client, }; m_clients.Add(newClient); client.BeginReceive(m_buffer, 0, m_buffer.Length, 0, new AsyncCallback(_onBeginReceive), client); _execute((Action <SoketinUser>)onEvent.OnClientConnected, newClient); m_userID++; }
public void Send(SoketinUser client, SoketinData data) { Send(client, data.GetBytes()); }
public virtual void OnDataRecieved(SoketinUser user, byte[] data) { onDataRecieved?.Invoke(user, data); }
public virtual void OnClientDisconnected(SoketinUser user) { onClientDisconnected?.Invoke(user); }