add tcp client/server
This commit is contained in:
parent
dad620f74e
commit
b0c551bd07
|
@ -3,6 +3,8 @@ a.out
|
||||||
|
|
||||||
*.mod
|
*.mod
|
||||||
*.log
|
*.log
|
||||||
|
*.~undo-tree~
|
||||||
|
*.*~
|
||||||
|
|
||||||
build/
|
build/
|
||||||
fortran/client/client
|
fortran/client/client
|
||||||
|
|
Binary file not shown.
|
@ -27,7 +27,7 @@ program main
|
||||||
call getarg(2, password)
|
call getarg(2, password)
|
||||||
|
|
||||||
me = player(username, vector3( 0.0_c_float, 1.0_c_float, 2.0_c_float ), PURPLE)
|
me = player(username, vector3( 0.0_c_float, 1.0_c_float, 2.0_c_float ), PURPLE)
|
||||||
!players = me%login(password)
|
players = me%login(password)
|
||||||
|
|
||||||
camera%position = vector3(0.0_c_float, 10.0_c_float, 10.0_c_float) !Camera position
|
camera%position = vector3(0.0_c_float, 10.0_c_float, 10.0_c_float) !Camera position
|
||||||
camera%target = vector3(0.0_c_float, 0.0_c_float, 0.0_c_float) !Camera looking at point
|
camera%target = vector3(0.0_c_float, 0.0_c_float, 0.0_c_float) !Camera looking at point
|
||||||
|
@ -58,9 +58,9 @@ program main
|
||||||
call me%sync_camera(camera)
|
call me%sync_camera(camera)
|
||||||
|
|
||||||
if (player_updated) then
|
if (player_updated) then
|
||||||
!players = me%move()
|
players = me%move()
|
||||||
else
|
else
|
||||||
!players = me%ping()
|
players = me%ping()
|
||||||
end if
|
end if
|
||||||
|
|
||||||
call begin_drawing()
|
call begin_drawing()
|
||||||
|
@ -71,15 +71,15 @@ program main
|
||||||
call draw_grid(30_c_int, 1.0_c_float)
|
call draw_grid(30_c_int, 1.0_c_float)
|
||||||
|
|
||||||
call draw_cube(me%position, 0.5_c_float, 0.5_c_float, 0.5_c_float, me%apperance)
|
call draw_cube(me%position, 0.5_c_float, 0.5_c_float, 0.5_c_float, me%apperance)
|
||||||
!do i=1,size(players)
|
do i=1,size(players)
|
||||||
! call draw_cube(players(i)%position, 0.5_c_float, 0.5_c_float, 0.5_c_float, players(i)%apperance)
|
call draw_cube(players(i)%position, 0.5_c_float, 0.5_c_float, 0.5_c_float, players(i)%apperance)
|
||||||
!end do
|
end do
|
||||||
call end_mode_3d()
|
call end_mode_3d()
|
||||||
call end_drawing()
|
call end_drawing()
|
||||||
|
|
||||||
end do
|
end do
|
||||||
|
|
||||||
!players = me%logout()
|
players = me%logout()
|
||||||
if (allocated(players)) then
|
if (allocated(players)) then
|
||||||
deallocate(players)
|
deallocate(players)
|
||||||
end if
|
end if
|
||||||
|
|
|
@ -16,3 +16,4 @@ library = false
|
||||||
implicit-typing = false
|
implicit-typing = false
|
||||||
implicit-external = false
|
implicit-external = false
|
||||||
source-form = "free"
|
source-form = "free"
|
||||||
|
[dependencies]
|
|
@ -37,6 +37,8 @@ contains
|
||||||
character(24) :: password
|
character(24) :: password
|
||||||
type(player), dimension(:), allocatable :: players
|
type(player), dimension(:), allocatable :: players
|
||||||
|
|
||||||
|
print *, password
|
||||||
|
|
||||||
players = send_packet(this, 1)
|
players = send_packet(this, 1)
|
||||||
end function login
|
end function login
|
||||||
|
|
||||||
|
@ -85,24 +87,31 @@ contains
|
||||||
integer(c_size_t) :: message_size, msglen = 256
|
integer(c_size_t) :: message_size, msglen = 256
|
||||||
type(ipaddr) :: addr
|
type(ipaddr) :: addr
|
||||||
character(c_char) :: message(256) = ''
|
character(c_char) :: message(256) = ''
|
||||||
|
character(len=256) :: f_message
|
||||||
character(len=*), parameter :: TCP_SUFFIX = c_carriage_return//c_new_line//c_null_char
|
character(len=*), parameter :: TCP_SUFFIX = c_carriage_return//c_new_line//c_null_char
|
||||||
|
|
||||||
rc = ipaddr_remote(addr, '127.0.0.1' // c_null_char, 35565_c_int, IPADDR_IPV4, -1_c_int64_t)
|
rc = ipaddr_remote(addr, '127.0.0.1'//c_null_char, 5555_c_int, IPADDR_IPV4, -1_c_int64_t)
|
||||||
connection = tcp_connect(addr, -1_c_int64_t)
|
connection = tcp_connect(addr, -1_c_int64_t)
|
||||||
connection = suffix_attach(connection, TCP_SUFFIX, 2_c_size_t)
|
connection = suffix_attach(connection, TCP_SUFFIX, 2_c_size_t)
|
||||||
|
|
||||||
write(message, "(i3, 1x, a24, 1x, f8.2, 1x, f8.2)") request_type, &
|
write (f_message, "(f8.2, f8.2, i3, a24)") &
|
||||||
this%username, this%position%x, this%position%y
|
this%position%x, this%position%y, request_type, this%username
|
||||||
|
|
||||||
rc = msend(connection, message(Len_Trim(message)), transfer(Len_Trim(message), 0_c_size_t), -1_c_int64_t)
|
print *, f_c_string(f_message, .true.)
|
||||||
|
|
||||||
|
rc = msend(connection, f_c_string(f_message, .true.), &
|
||||||
|
transfer(Len_Trim(f_c_string(f_message, .true.)), 0_c_size_t), -1_c_int64_t)
|
||||||
|
|
||||||
message_size = mrecv(connection, message, msglen, -1_c_int64_t)
|
message_size = mrecv(connection, message, msglen, -1_c_int64_t)
|
||||||
|
print *, 'recv message: ', message(:message_size)
|
||||||
read (message(:message_size), '(i3)') count
|
read (message(:message_size), '(i3)') count
|
||||||
|
|
||||||
|
print *, count
|
||||||
do i = 0, count, 1
|
do i = 0, count, 1
|
||||||
message_size = mrecv(connection, message, msglen, -1_c_int64_t)
|
message_size = mrecv(connection, message, msglen, -1_c_int64_t)
|
||||||
|
|
||||||
read(message, "(a, i3, i3, i3, f8.2, f8.2)") username, &
|
read (message(:message_size), "(i3, i3, i3, f8.2, f8.2, a24)") &
|
||||||
apperance_r, apperance_g, apperance_b, x_pos, y_pos
|
apperance_r, apperance_g, apperance_b, x_pos, y_pos, username
|
||||||
|
|
||||||
if (allocated(players)) then
|
if (allocated(players)) then
|
||||||
players = [players, player(username, vector3(x_pos, 1.0_c_float, y_pos), &
|
players = [players, player(username, vector3(x_pos, 1.0_c_float, y_pos), &
|
||||||
|
@ -114,4 +123,27 @@ contains
|
||||||
end do
|
end do
|
||||||
end function send_packet
|
end function send_packet
|
||||||
|
|
||||||
|
! from ivanpribec `https://fortran-lang.discourse.group/t/best-practices-for-passing-c-strings/104/12`
|
||||||
|
function f_c_string(string, trim)
|
||||||
|
use, intrinsic :: iso_c_binding, only: c_char, c_null_char
|
||||||
|
implicit none
|
||||||
|
character(len=*), intent(in) :: string
|
||||||
|
logical, intent(in), optional :: trim
|
||||||
|
|
||||||
|
character(kind=c_char, len=:), allocatable :: f_c_string
|
||||||
|
logical :: trim_
|
||||||
|
|
||||||
|
trim_ = .true.
|
||||||
|
if (present(trim)) trim_ = trim
|
||||||
|
|
||||||
|
block
|
||||||
|
intrinsic trim
|
||||||
|
if (trim_) then
|
||||||
|
f_c_string = trim(string)//c_null_char
|
||||||
|
else
|
||||||
|
f_c_string = string//c_null_char
|
||||||
|
end if
|
||||||
|
end block
|
||||||
|
end function
|
||||||
|
|
||||||
end module player_mod
|
end module player_mod
|
||||||
|
|
Binary file not shown.
|
@ -1,6 +1,10 @@
|
||||||
program main
|
program main
|
||||||
use iso_fortran_env
|
use iso_fortran_env
|
||||||
use, intrinsic :: iso_c_binding
|
use iso_c_binding, only: c_char, c_double, c_int, c_int64_t, c_null_char, &
|
||||||
|
c_size_t, c_carriage_return, c_new_line
|
||||||
|
use mod_dill, only: ipaddr, ipaddr_local, ipaddr_port, ipaddr_str, &
|
||||||
|
IPADDR_MAXSTRLEN, IPADDR_IPV4, tcp_accept, tcp_close, &
|
||||||
|
tcp_listen, mrecv, msend, suffix_attach, suffix_detach
|
||||||
use db
|
use db
|
||||||
implicit none
|
implicit none
|
||||||
|
|
||||||
|
@ -9,15 +13,15 @@ program main
|
||||||
character(len=24):: username
|
character(len=24):: username
|
||||||
real(kind=c_double) :: x_pos
|
real(kind=c_double) :: x_pos
|
||||||
real(kind=c_double) :: y_pos
|
real(kind=c_double) :: y_pos
|
||||||
integer :: command, rc
|
integer :: command
|
||||||
logical :: exist
|
logical :: exist
|
||||||
|
|
||||||
inquire (file="debug.log", exist=exist)
|
integer(c_int) :: connection, rc, socket
|
||||||
if (exist) then
|
integer(c_size_t) :: message_size, msglen = 256
|
||||||
open (12, file="debug.log", status="old", position="append", action="write")
|
character(c_char) :: message(256) = ''
|
||||||
else
|
type(ipaddr) :: addr, addr_remote
|
||||||
open (12, file="debug.log", status="new", action="write")
|
character(kind=c_char, len=IPADDR_MAXSTRLEN) :: address_string = ''
|
||||||
end if
|
character(len=*), parameter :: TCP_SUFFIX = c_carriage_return//c_new_line//c_null_char
|
||||||
|
|
||||||
call getarg(1, db_path)
|
call getarg(1, db_path)
|
||||||
|
|
||||||
|
@ -27,34 +31,49 @@ program main
|
||||||
end if
|
end if
|
||||||
|
|
||||||
rc = db_open(db, db_path(:Len_Trim(db_path)))
|
rc = db_open(db, db_path(:Len_Trim(db_path)))
|
||||||
|
|
||||||
|
rc = ipaddr_local(addr, '127.0.0.1'//c_null_char, 5555_c_int, IPADDR_IPV4)
|
||||||
|
call ipaddr_str(addr, address_string)
|
||||||
|
|
||||||
|
print *, 'Listening on socket:'
|
||||||
|
print *, ' IP address: ', address_string
|
||||||
|
print *, ' Port: ', ipaddr_port(addr)
|
||||||
|
print *, ' Opened DB : ', db_path(:Len_Trim(db_path))
|
||||||
|
|
||||||
|
socket = tcp_listen(addr, 0_c_int)
|
||||||
|
|
||||||
! do while not logged out
|
! do while not logged out
|
||||||
server_loop: do
|
server_loop: do
|
||||||
|
|
||||||
! read message from stdin
|
connection = tcp_accept(socket, addr_remote, -1_c_int64_t)
|
||||||
read(input_unit, "(i3, 1x, a24, 1x, f8.2, 1x, f8.2)") command, username, x_pos, y_pos
|
call ipaddr_str(addr, address_string)
|
||||||
write(12, "(i3, 1x, a24, 1x, f8.2, 1x, f8.2)") command, username, x_pos, y_pos
|
print *, 'New connection from '//trim(address_string)
|
||||||
call flush(12)
|
connection = suffix_attach(connection, TCP_SUFFIX, 2_c_size_t)
|
||||||
|
message_size = mrecv(connection, message, msglen, -1_c_int64_t)
|
||||||
|
print *, message_size, message
|
||||||
|
|
||||||
|
read (message, "(f8.2, f8.2, i3, a24)") x_pos, y_pos, command, username
|
||||||
|
print *, 'username=', username, ' x_pos=', x_pos, ' y_pos=', y_pos, ' command=', command
|
||||||
|
|
||||||
if (command .eq. 0) then ! get all logged in users and return their positions to client
|
if (command .eq. 0) then ! get all logged in users and return their positions to client
|
||||||
rc = db_count_logged_in_users(db)
|
rc = db_count_logged_in_users(db, connection)
|
||||||
rc = db_get_logged_in_users(db)
|
rc = db_get_logged_in_users(db, connection)
|
||||||
else if (command .eq. 1) then
|
else if (command .eq. 1) then
|
||||||
rc = db_login_user(db, username)
|
rc = db_login_user(db, username)
|
||||||
rc = db_count_logged_in_users(db)
|
rc = db_count_logged_in_users(db, connection)
|
||||||
rc = db_get_logged_in_users(db)
|
rc = db_get_logged_in_users(db, connection)
|
||||||
else if (command .eq. 3) then ! update new pos to database
|
else if (command .eq. 3) then ! update new pos to database
|
||||||
rc = db_move_user(db, username, x_pos, y_pos)
|
rc = db_move_user(db, username, x_pos, y_pos)
|
||||||
rc = db_count_logged_in_users(db)
|
rc = db_count_logged_in_users(db, connection)
|
||||||
rc = db_get_logged_in_users(db)
|
rc = db_get_logged_in_users(db, connection)
|
||||||
else ! (2) if logout update logged_in to database
|
else ! (2) if logout update logged_in to database
|
||||||
rc = db_logout_user(db, username)
|
rc = db_logout_user(db, username)
|
||||||
rc = db_count_logged_in_users(db)
|
rc = db_count_logged_in_users(db, connection)
|
||||||
rc = db_get_logged_in_users(db)
|
rc = db_get_logged_in_users(db, connection)
|
||||||
exit server_loop
|
exit server_loop
|
||||||
end if
|
end if
|
||||||
end do server_loop
|
end do server_loop
|
||||||
|
|
||||||
rc = db_close(db)
|
rc = db_close(db)
|
||||||
close(12)
|
|
||||||
|
|
||||||
end program main
|
end program main
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
! db.f90
|
! db.f90
|
||||||
module db
|
module db
|
||||||
use iso_fortran_env
|
use iso_fortran_env
|
||||||
use, intrinsic :: iso_c_binding
|
use iso_c_binding
|
||||||
|
use mod_dill, only: ipaddr, ipaddr_remote, IPADDR_IPV4, mrecv, msend, tcp_connect, &
|
||||||
|
suffix_attach
|
||||||
use :: sqlite3
|
use :: sqlite3
|
||||||
implicit none
|
implicit none
|
||||||
private
|
private
|
||||||
|
@ -27,7 +29,6 @@ module db
|
||||||
private :: db_exec
|
private :: db_exec
|
||||||
contains
|
contains
|
||||||
|
|
||||||
|
|
||||||
integer function db_open(db, path) result(rc)
|
integer function db_open(db, path) result(rc)
|
||||||
!! Opens database.
|
!! Opens database.
|
||||||
type(db_type), intent(inout) :: db
|
type(db_type), intent(inout) :: db
|
||||||
|
@ -118,7 +119,6 @@ contains
|
||||||
call db_error(rc, 'sqlite3_finalize()')
|
call db_error(rc, 'sqlite3_finalize()')
|
||||||
end function db_add_user
|
end function db_add_user
|
||||||
|
|
||||||
|
|
||||||
integer function db_delete_user(db, username) result(rc)
|
integer function db_delete_user(db, username) result(rc)
|
||||||
!! Adds student to database.
|
!! Adds student to database.
|
||||||
type(db_type), intent(inout) :: db
|
type(db_type), intent(inout) :: db
|
||||||
|
@ -141,10 +141,12 @@ contains
|
||||||
call db_error(rc, 'sqlite3_finalize()')
|
call db_error(rc, 'sqlite3_finalize()')
|
||||||
end function db_delete_user
|
end function db_delete_user
|
||||||
|
|
||||||
integer function db_count_logged_in_users(db) result(rc)
|
integer function db_count_logged_in_users(db, connection) result(rc)
|
||||||
!! Prints number of courses per student to standard output.
|
!! Prints number of courses per student to standard output.
|
||||||
type(db_type), intent(inout) :: db
|
type(db_type), intent(inout) :: db
|
||||||
|
integer(c_int), intent(inout) :: connection
|
||||||
|
|
||||||
|
character(len=256) :: message
|
||||||
type(c_ptr) :: stmt
|
type(c_ptr) :: stmt
|
||||||
integer :: count
|
integer :: count
|
||||||
|
|
||||||
|
@ -157,7 +159,11 @@ contains
|
||||||
select case (rc)
|
select case (rc)
|
||||||
case (SQLITE_ROW)
|
case (SQLITE_ROW)
|
||||||
count = sqlite3_column_int(stmt, 0)
|
count = sqlite3_column_int(stmt, 0)
|
||||||
write(output_unit, "(i0)") count
|
print *, 'logged in: ', count
|
||||||
|
|
||||||
|
write (message, "(i3)") count
|
||||||
|
rc = msend(connection, f_c_string(message, .true.), &
|
||||||
|
transfer(Len_Trim(f_c_string(message, .true.)), 0_c_size_t), -1_c_int64_t)
|
||||||
|
|
||||||
case (SQLITE_DONE)
|
case (SQLITE_DONE)
|
||||||
exit step_loop
|
exit step_loop
|
||||||
|
@ -172,14 +178,16 @@ contains
|
||||||
call db_error(rc, 'sqlite3_finalize()')
|
call db_error(rc, 'sqlite3_finalize()')
|
||||||
end function db_count_logged_in_users
|
end function db_count_logged_in_users
|
||||||
|
|
||||||
integer function db_get_logged_in_users(db) result(rc)
|
integer function db_get_logged_in_users(db, connection) result(rc)
|
||||||
!! Prints number of courses per student to standard output.
|
!! Prints number of courses per student to standard output.
|
||||||
type(db_type), intent(inout) :: db
|
type(db_type), intent(inout) :: db
|
||||||
|
integer(c_int), intent(inout) :: connection
|
||||||
|
|
||||||
|
character(len=256) :: message
|
||||||
type(c_ptr) :: stmt
|
type(c_ptr) :: stmt
|
||||||
character(len=24) :: username
|
character(len=24) :: username
|
||||||
integer :: apperance_r, apperance_g, apperance_b
|
integer :: apperance_r, apperance_g, apperance_b
|
||||||
real :: x_pos, y_pos
|
real(real64) :: x_pos, y_pos
|
||||||
|
|
||||||
rc = sqlite3_prepare_v2(db%ptr, &
|
rc = sqlite3_prepare_v2(db%ptr, &
|
||||||
"SELECT username, apperance_r, apperance_g, apperance_b, x_pos, y_pos "// &
|
"SELECT username, apperance_r, apperance_g, apperance_b, x_pos, y_pos "// &
|
||||||
|
@ -197,9 +205,12 @@ contains
|
||||||
apperance_b = sqlite3_column_int(stmt, 3)
|
apperance_b = sqlite3_column_int(stmt, 3)
|
||||||
x_pos = sqlite3_column_double(stmt, 4)
|
x_pos = sqlite3_column_double(stmt, 4)
|
||||||
y_pos = sqlite3_column_double(stmt, 5)
|
y_pos = sqlite3_column_double(stmt, 5)
|
||||||
write(output_unit, "(a24, 1x, i3, 1x, i3, 1x, i3, 1x, f8.2, 1x, f8.2)") username(:Len_Trim(username)), &
|
|
||||||
apperance_r, apperance_g, apperance_b, x_pos, y_pos
|
|
||||||
|
|
||||||
|
write (message, "(i3, i3, i3, f8.2, f8.2, a24)") &
|
||||||
|
apperance_r, apperance_g, apperance_b, x_pos, y_pos, username(:Len_Trim(username))
|
||||||
|
|
||||||
|
rc = msend(connection, f_c_string(message, .true.), &
|
||||||
|
transfer(Len_Trim(f_c_string(message, .true.)), 0_c_size_t), -1_c_int64_t)
|
||||||
case (SQLITE_DONE)
|
case (SQLITE_DONE)
|
||||||
exit step_loop
|
exit step_loop
|
||||||
|
|
||||||
|
@ -285,15 +296,38 @@ contains
|
||||||
if (code == SQLITE_OK .or. code == SQLITE_DONE) return
|
if (code == SQLITE_OK .or. code == SQLITE_DONE) return
|
||||||
|
|
||||||
if (present(proc) .and. present(err_msg)) then
|
if (present(proc) .and. present(err_msg)) then
|
||||||
write(12, '(a, ": ", a, " (", i0, ")")') proc, err_msg, code
|
print *, proc, err_msg, code
|
||||||
return
|
return
|
||||||
end if
|
end if
|
||||||
|
|
||||||
if (present(proc)) then
|
if (present(proc)) then
|
||||||
write(12, '(a, ": ", i0)') proc, code
|
print *, proc, code
|
||||||
return
|
return
|
||||||
end if
|
end if
|
||||||
|
|
||||||
write(12, '("unknown error: ", i0)') code
|
print *, 'unknown error: ', code
|
||||||
end subroutine db_error
|
end subroutine db_error
|
||||||
|
|
||||||
|
! from ivanpribec `https://fortran-lang.discourse.group/t/best-practices-for-passing-c-strings/104/12`
|
||||||
|
function f_c_string(string, trim)
|
||||||
|
use, intrinsic :: iso_c_binding, only: c_char, c_null_char
|
||||||
|
implicit none
|
||||||
|
character(len=*), intent(in) :: string
|
||||||
|
logical, intent(in), optional :: trim
|
||||||
|
|
||||||
|
character(kind=c_char, len=:), allocatable :: f_c_string
|
||||||
|
logical :: trim_
|
||||||
|
|
||||||
|
trim_ = .true.
|
||||||
|
if (present(trim)) trim_ = trim
|
||||||
|
|
||||||
|
block
|
||||||
|
intrinsic trim
|
||||||
|
if (trim_) then
|
||||||
|
f_c_string = trim(string)//c_null_char
|
||||||
|
else
|
||||||
|
f_c_string = string//c_null_char
|
||||||
|
end if
|
||||||
|
end block
|
||||||
|
end function
|
||||||
end module db
|
end module db
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
listen1 'tcp!*!35565' ~/.local/bin/fortran-mmo-server ../../common/sql/test.db3
|
#listen1 'tcp!*!35565'
|
||||||
|
~/.local/bin/fortran-mmo-server ../../common/sql/test.db3
|
||||||
|
|
Loading…
Reference in New Issue