private string strBuffer = ""; //< String version of the buffer. #endregion Fields #region Constructors /** * Constructor for ConnectionState. * * @param newConnection * The TcpClient to base the ConnectionState object on. * @param readLength * The amount of data to attempt to read on each read. */ public ConnectionState(Sockets.TcpClient newConnection, int readLength) { bufferLength = readLength; clientConnection = newConnection; clientStream = newConnection.GetStream(); ClearBuffer(); }
unsafe public override int Receive (byte [] buffer, int offset, int size, sock.SocketFlags flags) { if (!connected) return 0; int value; fixed (byte* ptr = buffer) value = recv (socket, ptr + offset, size, (int) flags); if (value >= 0) return value; connected = false; throw GetException (); }
/// <summary> /// Create poll item for ZMQ and plain socket listening, for the supplied events /// </summary> /// <param name="events">Listening events</param> /// <param name="sysSocket">Raw Socket</param> /// <returns>Socket Poll item</returns> public PollItem CreatePollItem(IOMultiPlex events, SysSockets.Socket sysSocket) { if (sysSocket == null) { throw new ArgumentNullException("sysSocket"); } #if x86 || POSIX return new PollItem(new ZMQPollItem(Ptr, sysSocket.Handle.ToInt32(), (short)events), this); #elif x64 return new PollItem(new ZMQPollItem(Ptr, sysSocket.Handle.ToInt64(), (short)events), this); #endif }
/// <summary> /// Create poll item for ZMQ and plain socket listening, for the supplied events /// </summary> /// <param name="events">Listening events</param> /// <param name="sysSocket">Raw Socket</param> /// <returns>Socket Poll item</returns> public PollItem CreatePollItem(IOMultiPlex events, SysSockets.Socket sysSocket) { #if x86 || POSIX return new PollItem(new ZMQPollItem(Ptr, sysSocket.Handle.ToInt32(), (short)events), this); #elif x64 return new PollItem(new ZMQPollItem(Ptr, sysSocket.Handle.ToInt64(), (short)events), this); #endif }
// Method that is called when the onReceive event is triggered. public void ReceiveHandler(Sockets.TcpClient handleConnection, string strData) { Asterion.Out.Logger.WriteOutput("Received: " + strData); // Sends the same data back to the client. server.WriteData(handleConnection, strData); }
// Method that is called when the onDisconnect event is triggered. public void DisconnectHandler(Sockets.TcpClient handleConnection) { Asterion.Out.Logger.WriteOutput("Client has disconnected."); }
unsafe public override int Send (byte [] data, int offset, int size, sock.SocketFlags flags) { if (!connected) return 0; int value; fixed (byte* ptr = data) value = send (socket, ptr + offset, size, (int) flags); if (value >= 0) return value; connected = false; throw GetException (); }
/** * Handles a new client connecting to the server and starts reading from the client if the client limit has not been reached. * * @param newConnection * The new client. */ private void HandleNewClient(Sockets.TcpClient newConnection) { if(currentClients <= maximumClients || maximumClients == 0) { currentClients++; if(connectEvent != null) connectEvent(newConnection); ConnectionState connectionState = new ConnectionState(newConnection, readLength); BeginRead(connectionState); }else{ Out.Logger.WriteOutput("Server full, rejecting client.", Out.Logger.LogLevel.Warn); newConnection.Client.Shutdown(Sockets.SocketShutdown.Both); newConnection.Client.Disconnect(true); } }
/** * Writes data to a client. * * @param writeConnection * The client to write data to. * @param sendData * The data to send to the client. */ public void WriteData(Sockets.TcpClient writeConnection, string sendData) { try { byte[] arrWrite = Utils.StrToBytes(sendData + delimiterString); writeConnection.LingerState = new Sockets.LingerOption(true, 2); if(writeConnection.GetStream().CanWrite) writeConnection.GetStream().BeginWrite(arrWrite, 0, arrWrite.Length, WriteCallback, writeConnection); }catch(System.Exception writeEx){ Out.Logger.WriteOutput("Could not write to client: " + writeEx.Message + "."); } }
/** * Kicks a client off the server (Might leave the client with a sore butt). * * @param disconnectConnection * The connection to close. */ public void DisconnectClient(Sockets.TcpClient disconnectConnection) { if(disconnectConnection.Client.Connected) { disconnectConnection.Client.Shutdown(Sockets.SocketShutdown.Both); disconnectConnection.Client.Disconnect(true); } }