[Cherbot-commit] SF.net SVN: cherbot: [75] trunk/src/net/sf/cherbot/DaimoninConnection.java
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-06-17 14:38:03
|
Revision: 75
http://svn.sourceforge.net/cherbot/?rev=75&view=rev
Author: christianhujer
Date: 2007-06-17 07:37:59 -0700 (Sun, 17 Jun 2007)
Log Message:
-----------
Committing current state of DaimoninConnection (doesn't work yet).
Added Paths:
-----------
trunk/src/net/sf/cherbot/DaimoninConnection.java
Added: trunk/src/net/sf/cherbot/DaimoninConnection.java
===================================================================
--- trunk/src/net/sf/cherbot/DaimoninConnection.java (rev 0)
+++ trunk/src/net/sf/cherbot/DaimoninConnection.java 2007-06-17 14:37:59 UTC (rev 75)
@@ -0,0 +1,208 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.BufferedOutputStream;
+import java.io.EOFException;
+import java.net.Socket;
+import java.util.List;
+import net.sf.japi.io.args.ArgParser;
+import net.sf.japi.io.args.BasicCommand;
+import net.sf.japi.io.args.Option;
+import net.sf.japi.io.args.OptionType;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/** A DaimoninConnection represents a connection to a Daimonin server.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class DaimoninConnection extends BasicCommand {
+
+ /** Default server host. */
+ //@NotNull public static final String DEFAULT_DAIMONIN_HOST = "daimonin.game-server.cc";
+ @NotNull public static final String DEFAULT_DAIMONIN_HOST = "127.0.0.1";
+
+ /** Default server port. */
+ public static final int DEFAULT_DAIMONIN_PORT = 13327;
+
+ /** The host to connect to. */
+ @NotNull private String host = DEFAULT_DAIMONIN_HOST;
+
+ /** The port to connect to. */
+ private int port = DEFAULT_DAIMONIN_PORT;
+
+ /** The username to authenticate with. */
+ @NotNull private String username;
+
+ /** The password to authenticate with. */
+ @NotNull private String password;
+
+ /** The Socket for communication. */
+ @Nullable private Socket socket;
+
+ /** The PrintStream to write to. */
+ @Nullable private OutputStream out;
+
+ /** The BufferedReader to read from. */
+ @Nullable private InputStream in;
+
+ /** Creates a DaimoninConnection. */
+ public DaimoninConnection() {
+ }
+
+ /** Main program.
+ * @param args Command line arguments.
+ */
+ public static void main(final String... args) {
+ ArgParser.simpleParseAndRun(new DaimoninConnection(), args);
+ }
+
+ /** Establishes a connection.
+ * @throws IOException In case of connection problems.
+ */
+ private void connect() throws IOException {
+ final Socket socket = new Socket(host, port);
+ this.socket = socket;
+ final OutputStream out = new BufferedOutputStream(socket.getOutputStream());
+ this.out = out;
+ final InputStream in = socket.getInputStream();
+ this.in = in;
+
+ // read server version
+ readToNull();
+
+ // send client version
+ //textMsg("version 991023 991023 CherBot");
+ textMsg("version 991023 991023 Daimonin SDL Client"); // we have to cheat or the server won't let us in.
+
+ //textMsg("setup");
+ //readToNull();
+ textMsg("addme");
+ readToNull();
+ textMsg("reply L" + username);
+ readToNull();
+ textMsg("reply " + password);
+ // write 0x00 0x29 "version 991023 991023 Daimonin SDL Client"
+ // TODO
+ // read 0x00 0x1e 0x02 "991023 991023 Daimonin Server"
+ // write 0x00 0x99 "setup sound 1 map2 cmd 1 mapsize 17x17 darkness 1 facecache 1 skf 3386|dd257ea spf 2737|404f6ead bpf 227483|8ab2a06 stf 2253|16d49f0f amf 262790|ec2627ee"
+ // read 0x00 0x75 0x17 " sound 1 map2cmd 1 mapsize 17x17 darkness 1 facecache 1 skf OK spf OK bpf 227639|117ae13a stf OK amf 263415|ec0d5f79"
+ // write 0x00 0x04 "rf 4"
+ // read lots of data
+ // write 0x00 0x04 "rf 3"
+ // read lots of data
+ // write 0x00 0x05 "addme"
+ // read 0x00 0x06 0x18 "4 QN0" 0x00 0x01 0x15
+ // write 0x00 0x12 "reply LFoobuzzer49" // L not part of name
+ // read 0x00 0x06 0x18 "4 QP0"
+ // write 0x00 0x0E "reply xxxxxxxx"
+ }
+
+ /** Reads and discards a single message.
+ * @throws IOException In case of I/O problems.
+ */
+ private void readToNull() throws IOException {
+ receivePacket();
+ }
+
+ /** Reads the next data packet.
+ * @throws IOException In case of I/O problems.
+ * @return The data of the next data packet.
+ */
+ private byte[] receivePacket() throws IOException {
+ final InputStream in = this.in;
+ assert in != null;
+ final int h1 = in.read();
+ final int length = h1 != -1 && (h1 & 0x80) == 0x80 ? h1 << 16 | in.read() << 8 | in.read() : h1 << 8 | in.read();
+ if (length == -1) {
+ throw new EOFException();
+ }
+ final byte[] data = new byte[length];
+ for (int read = 0; read < length;) {
+ final int bytesRead = in.read(data, read, length - read);
+ if (bytesRead == -1) {
+ throw new EOFException();
+ }
+ read += bytesRead;
+ }
+ return data;
+ }
+
+ /** Sends a simple text message.
+ * @param msg Message to send
+ * @throws IOException In case of I/O problems.
+ */
+ private void textMsg(@NotNull final String msg) throws IOException {
+ final OutputStream out = this.out;
+ assert out != null;
+ final byte[] data = msg.getBytes("utf-8");
+ out.write(0xFF & data.length >> 8);
+ out.write(0xFF & data.length);
+ out.write(data);
+ out.flush();
+ }
+
+ /** Closes a connection.
+ * @throws IOException In case of connection problems.
+ */
+ private void disconnect() throws IOException {
+ try {
+ final Socket socket = this.socket;
+ if (socket != null) {
+ socket.close();
+ }
+ } finally {
+ socket = null;
+ in = null;
+ out = null;
+ }
+ }
+
+ /** {@inheritDoc} */
+ public int run(@NotNull final List<String> args) throws Exception {
+ connect();
+ Thread.sleep(10000);
+ disconnect();
+ return 0;
+ }
+
+ /** Sets the username for connecting to this Daimonin server.
+ * @param username Username for connecting to this Daimonin server.
+ */
+ @Option(type = OptionType.REQUIRED, value = {"username"})
+ public void setUsername(@NotNull final String username) {
+ this.username = username;
+ }
+
+ /** Sets the password for connecting to this Daimonin server.
+ * @param password Password for connecting to this Daimonin server.
+ */
+ @Option(type = OptionType.REQUIRED, value = {"password"})
+ public void setPassword(@NotNull final String password) {
+ this.password = password;
+ }
+
+ /** Sets the host to connect to.
+ * @param host Host to connect to
+ */
+ @Option({"host"})
+ public void setHost(@NotNull final String host) {
+ this.host = host;
+ }
+
+ /** Sets the port to connect to.
+ * @param port Port to connect to
+ */
+ @Option({"port"})
+ public void setPort(@NotNull final Integer port) {
+ this.port = port;
+ }
+
+} // class DaimoninConnection
Property changes on: trunk/src/net/sf/cherbot/DaimoninConnection.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|