net.petterroea.starterkit
Class ServerSideConnection

java.lang.Object
  extended by net.petterroea.starterkit.ServerSideConnection

public class ServerSideConnection
extends java.lang.Object

Used for a server side connection that can handle tonnes of connections. Use this if you want a game option to host a game, or if you are making a dedicated server

Author:
petterroea

Field Summary
private  java.nio.charset.CharsetDecoder asciiDecoder
          Char decoder
(package private)  java.util.LinkedList<ServerClient> clients
          The clients connected to the server
(package private)  java.util.LinkedList<Packet> in
          List of packets going in
(package private)  java.lang.Thread listener
          The thread that listens to the network
(package private)  java.util.LinkedList<Packet> out
          List of packets going out
private  java.nio.ByteBuffer readBuffer
          The read buffer
private  java.nio.channels.Selector readSelector
          Some random stuff.
private  boolean running
          True if we are monitoring for commections and listening to packets
private  java.nio.channels.ServerSocketChannel sSChannel
          The server socket channel
static java.lang.Object sync
          Synchronization object
private  java.nio.ByteBuffer writeBuffer
          The writing-buffer
 
Constructor Summary
ServerSideConnection(int port)
          Constructor for a serverside connection
 
Method Summary
private  void acceptNewPeeps()
          Used by the seperate thread to accept new connections
private  void channelWrite(java.nio.channels.SocketChannel channel, java.nio.ByteBuffer writeBuffer)
          Writes to a channel
private  void prepWriteBuffer(java.lang.String mesg)
          Used for preparing writing to the client
private  void readIncomingMessages()
          Reads incoming packets
private  void sendMessage(java.nio.channels.SocketChannel channel, java.lang.String mesg)
          Sends a message to the client.
private  void sendPackets()
          Used by the seperate thread to send packets in the queue
 void start()
          Runs the server network stuff
 void stop()
          Stops the server network thingie.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

writeBuffer

private java.nio.ByteBuffer writeBuffer
The writing-buffer


asciiDecoder

private java.nio.charset.CharsetDecoder asciiDecoder
Char decoder


readBuffer

private java.nio.ByteBuffer readBuffer
The read buffer


listener

java.lang.Thread listener
The thread that listens to the network


clients

java.util.LinkedList<ServerClient> clients
The clients connected to the server


readSelector

private java.nio.channels.Selector readSelector
Some random stuff. I dont know what it is, i just use it.


running

private boolean running
True if we are monitoring for commections and listening to packets


sSChannel

private java.nio.channels.ServerSocketChannel sSChannel
The server socket channel


in

java.util.LinkedList<Packet> in
List of packets going in


out

java.util.LinkedList<Packet> out
List of packets going out


sync

public static java.lang.Object sync
Synchronization object

Constructor Detail

ServerSideConnection

public ServerSideConnection(int port)
Constructor for a serverside connection

Parameters:
port - The port to bind to
Method Detail

start

public void start()
Runs the server network stuff


sendPackets

private void sendPackets()
Used by the seperate thread to send packets in the queue


sendMessage

private void sendMessage(java.nio.channels.SocketChannel channel,
                         java.lang.String mesg)
Sends a message to the client. Again, dont touch this

Parameters:
channel - The channel from the client
mesg - The message to be sendt

readIncomingMessages

private void readIncomingMessages()
Reads incoming packets


channelWrite

private void channelWrite(java.nio.channels.SocketChannel channel,
                          java.nio.ByteBuffer writeBuffer)
Writes to a channel

Parameters:
channel - The channel
writeBuffer - The buffer

prepWriteBuffer

private void prepWriteBuffer(java.lang.String mesg)
Used for preparing writing to the client

Parameters:
mesg -

acceptNewPeeps

private void acceptNewPeeps()
Used by the seperate thread to accept new connections


stop

public void stop()
          throws java.lang.InterruptedException,
                 java.io.IOException
Stops the server network thingie. Use this before disposing the network code

Throws:
java.lang.InterruptedException - If faied to join thread
java.io.IOException - If failed to close the socket