works better with server

This commit is contained in:
Dan 2008-05-07 18:36:06 -07:00
parent 763245732d
commit b815623214
2 changed files with 28 additions and 22 deletions

View File

@ -1,6 +1,6 @@
(in-package :gtp-handler) (in-package :gtp-handler)
(require :sb-bsd-sockets) ;(require :sb-bsd-sockets)
(defparameter *quit?* nil) (defparameter *quit?* nil)
@ -10,26 +10,30 @@
four element array, suitable for socket-connect. If HOSTNAME is four element array, suitable for socket-connect. If HOSTNAME is
not found, a host-not-found-error condition is thrown." not found, a host-not-found-error condition is thrown."
(if hostname (if hostname
(host-ent-address (get-host-by-name hostname)) (sb-bsd-sockets:host-ent-address (sb-bsd-sockets:get-host-by-name hostname))
nil)) nil))
(defun tcp-connect (server port &optional (timeout 10)) (defun tcp-connect (server port &optional (timeout 10))
(let ((socket (make-instance 'sb-bsd-sockets:inet-socket :type (handler-case
:stream :protocol :tcp))) (let ((socket (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp)))
(sb-bsd-sockets:socket-connect socket (nslookup server) port) (sb-bsd-sockets:socket-connect socket (nslookup server) port)
socket)) socket)
(sb-bsd-sockets:CONNECTION-REFUSED-ERROR ()
(progn
(format t "Error: Connection refused~%")
nil))))
(defun tcp-print-raw (socket line) (defun tcp-print-raw (socket line)
(when (and socket line) (when (and socket line)
(socket-send socket line nil))) (sb-bsd-sockets:socket-send socket line nil)))
(defun tcp-print (socket line) (defun tcp-print (socket line)
(tcp-print-raw socket (concatenate 'string (format nil "~04d" (length line)) line))) (tcp-print-raw socket (concatenate 'string (format nil "~04d" (length line)) line)))
(defun tcp-read-raw (socket &key (maxsize 65536) (timeout 10)) (defun tcp-read-raw (socket &key (maxsize 65536) (timeout 10))
(when socket (when socket
(values (socket-receive socket nil maxsize)))) (values (sb-bsd-sockets:socket-receive socket nil maxsize))))
;(if-timeout (timeout (format t "socket-receive timed out after ~A seconds.~%" timeout) (force-output) nil) ;(if-timeout (timeout (format t "socket-receive timed out after ~A seconds.~%" timeout) (force-output) nil)
@ -43,20 +47,20 @@
(defun gtp-net-client (server port) (defun gtp-net-client (server port)
(go-bot:init) (go-bot:init)
;(print "bot inited")
(setf *quit?* nil) (setf *quit?* nil)
;(print "get connection")
(let ((socket (tcp-connect server port))) (let ((socket (tcp-connect server port)))
;(print "got socket") (if (eql socket nil)
()
(progn
(format t "Connection establish, playing...~%")
(do () (do ()
((eql *quit?* t)) ((or (eql socket nil) (eql *quit?* t)))
(let ((cmd (tcp-read socket))) (let ((cmd (tcp-read socket)))
; (print cmd) ; (print cmd)
(let ((resp (dispatch-gtp-command cmd))) (let ((resp (dispatch-gtp-command cmd)))
; (print resp) ;(print resp)
(tcp-print socket (concatenate 'string "= " resp (string #\newline) (string #\newline)))))))) (tcp-print socket (concatenate 'string "= " resp (string #\newline) (string #\newline))))))))))
; (tcp-print socket (concatenate 'string "= " (dispatch-gtp-command (tcp-read socket)) "\n\n")))))
(defun gtp-client () (defun gtp-client ()
(go-bot:init) (go-bot:init)

View File

@ -1,18 +1,20 @@
(in-package :common-lisp) (in-package :common-lisp)
;(clc:clc-require :cl-ppcre) ;(clc:clc-require :cl-ppcre)
(asdf:oos 'asdf:load-op :cl-ppcre) ;(asdf:oos 'asdf:load-op :cl-ppcre)
(require :sb-bsd-sockets) (require :sb-bsd-sockets)
(defpackage gtp-handler (defpackage gtp-handler
(:use :common-lisp :sb-bsd-sockets) (:use :common-lisp)
(:export :gtp-client)) (:export :gtp-client
:gtp-net-client))
(defpackage go-bot (defpackage go-bot
(:use :common-lisp) (:use :common-lisp)
(:export :*name* (:export :*name*
:*version* :*version*
:*author* :*author*
:*player*
:set-komi :set-komi
:set-boardsize :set-boardsize
:init-board :init-board